4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <functional> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: typedef complex<double> CMP; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const LL MODVAL = 1000000009; // must fit in 32-bits 4fd800b3a8 2011-02-23 kinaba: LL ADD(LL x, LL y) { return (x+y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL MUL(LL x, LL y) { return (x*y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: LL POW(LL x, LL e) { 4fd800b3a8 2011-02-23 kinaba: LL v = 1; 4fd800b3a8 2011-02-23 kinaba: for(;e;x=MUL(x,x),e>>=1) 4fd800b3a8 2011-02-23 kinaba: if(e&1) 4fd800b3a8 2011-02-23 kinaba: v = MUL(v, x); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RabbitIncreasing { public: 4fd800b3a8 2011-02-23 kinaba: int getNumber(vector <int> leaving, int k) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( leaving.front() <= 1 ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: int li = 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL y[] = {1,0,0}; 4fd800b3a8 2011-02-23 kinaba: LL p[] = {1,0,0}; 4fd800b3a8 2011-02-23 kinaba: LL PP = (1LL << 55); 4fd800b3a8 2011-02-23 kinaba: for(int i=2; i<=k; ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: LL a=y[0], b=y[1], c=y[2]; 4fd800b3a8 2011-02-23 kinaba: y[0]=y[2]=ADD(b,c); y[1]=a; 4fd800b3a8 2011-02-23 kinaba: LL pa=p[0], pb=p[1], pc=p[2]; 4fd800b3a8 2011-02-23 kinaba: p[0]=p[2]=(pb+pc)%PP; p[1]=pa; 4fd800b3a8 2011-02-23 kinaba: if( li<leaving.size() && i==leaving[li] ) { 4fd800b3a8 2011-02-23 kinaba: ++li; 4fd800b3a8 2011-02-23 kinaba: y[2] = 0; 4fd800b3a8 2011-02-23 kinaba: p[2] = 0; 4fd800b3a8 2011-02-23 kinaba: if( p[1]&1 ) { 4fd800b3a8 2011-02-23 kinaba: y[1]=DIV(SUB(y[1],1),2); 4fd800b3a8 2011-02-23 kinaba: } else { 4fd800b3a8 2011-02-23 kinaba: y[1]=DIV(y[1],2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: p[1]=p[1]/2; 4fd800b3a8 2011-02-23 kinaba: PP >>= 1; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return ADD(y[0],ADD(y[1],y[2])); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time; string timer() 4fd800b3a8 2011-02-23 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 4fd800b3a8 2011-02-23 kinaba: { os << "{ "; 4fd800b3a8 2011-02-23 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 4fd800b3a8 2011-02-23 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 4fd800b3a8 2011-02-23 kinaba: void verify_case(const int& Expected, const int& Received) { 4fd800b3a8 2011-02-23 kinaba: bool ok = (Expected == Received); 4fd800b3a8 2011-02-23 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 4fd800b3a8 2011-02-23 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 4fd800b3a8 2011-02-23 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 4fd800b3a8 2011-02-23 kinaba: #define END verify_case(_, RabbitIncreasing().getNumber(leaving, k));} 4fd800b3a8 2011-02-23 kinaba: int main(){ 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: CASE(0) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 3 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 5, 9 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 10; 4fd800b3a8 2011-02-23 kinaba: int _ = 6; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 5, 10, 15 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 19; 4fd800b3a8 2011-02-23 kinaba: int _ = 212; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(3) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 2 }; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 10000000; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 195, 211, 227, 230, 260, 4fd800b3a8 2011-02-23 kinaba: 297, 346, 350, 403, 411, 4fd800b3a8 2011-02-23 kinaba: 428, 485, 594, 606, 876 } 4fd800b3a8 2011-02-23 kinaba: ; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 1000; 4fd800b3a8 2011-02-23 kinaba: int _ = 975206486; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 1; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = {2}; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 2; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = {1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 1; 4fd800b3a8 2011-02-23 kinaba: int _ = 0; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = {999}; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 10000000; 4fd800b3a8 2011-02-23 kinaba: int _ = 536491299; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: int leaving_[] = { 4fd800b3a8 2011-02-23 kinaba: 100,200,300,400,500,600,700,800,900,1000, 4fd800b3a8 2011-02-23 kinaba: 1100,1200,1300,1400,1500,1600,1700,1800,1900,2000, 4fd800b3a8 2011-02-23 kinaba: 3100,3200,3300,3400,3500,3600,3700,3800,3900,31000, 4fd800b3a8 2011-02-23 kinaba: 43100,43200,43300,43400,43500,43600,43700,43800,43900,431000, 4fd800b3a8 2011-02-23 kinaba: 999100,999200,999300,999400,999500,999600,999700,999800,999900,9991000, 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: vector <int> leaving(leaving_, leaving_+sizeof(leaving_)/sizeof(*leaving_)); 4fd800b3a8 2011-02-23 kinaba: int k = 10000000; 4fd800b3a8 2011-02-23 kinaba: int _ = 260357174; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE