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 int MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: typedef pair<pair<LL,LL>,LL> state; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RabbitPuzzle { public: 4fd800b3a8 2011-02-23 kinaba: int theCount(vector<long long> rabbits, vector<long long> nests, int K) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: state s(make_pair(rabbits[0],rabbits[1]), rabbits[2]); 4fd800b3a8 2011-02-23 kinaba: state g(make_pair(nests[0],nests[1]), nests[2]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<state,int> ms, mg; 4fd800b3a8 2011-02-23 kinaba: calc(s, K/2, ms); 4fd800b3a8 2011-02-23 kinaba: calc(g, (K+1)/2, mg); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int result = 0; 4fd800b3a8 2011-02-23 kinaba: for(map<state,int>::iterator it=ms.begin(); it!=ms.end(); ++it) { 4fd800b3a8 2011-02-23 kinaba: map<state,int>::iterator jt = mg.find(it->first); 4fd800b3a8 2011-02-23 kinaba: if( jt != mg.end() ) 4fd800b3a8 2011-02-23 kinaba: result = (result + LL(it->second)*jt->second)%MODVAL; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: return result; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: void calc(const state& s, int n, map<state,int>& m) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( n == 0 ) 4fd800b3a8 2011-02-23 kinaba: (m[s] += 1) %= MODVAL; 4fd800b3a8 2011-02-23 kinaba: else { 4fd800b3a8 2011-02-23 kinaba: LL a = s.first.first; 4fd800b3a8 2011-02-23 kinaba: LL b = s.first.second; 4fd800b3a8 2011-02-23 kinaba: LL c = s.second; 4fd800b3a8 2011-02-23 kinaba: if( 2*b-a < c ) calc(state(make_pair(b,2*b-a),c), n-1, m); 4fd800b3a8 2011-02-23 kinaba: calc(state(make_pair(2*a-b,a),c), n-1, m); 4fd800b3a8 2011-02-23 kinaba: calc(state(make_pair(a,c),2*c-b), n-1, m); 4fd800b3a8 2011-02-23 kinaba: if( a < 2*b-c ) calc(state(make_pair(a,2*b-c),b), n-1, m); 4fd800b3a8 2011-02-23 kinaba: } 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(_, RabbitPuzzle().theCount(rabbits, nests, 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: long rabbits_[] = {0, 5, 8}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {0, 8, 11}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 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(1) 4fd800b3a8 2011-02-23 kinaba: long rabbits_[] = {0, 5, 8}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {0, 8, 11}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 3; 4fd800b3a8 2011-02-23 kinaba: int _ = 5; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: long rabbits_[] = {0, 5, 8}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {0, 8, 11}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 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(3) 4fd800b3a8 2011-02-23 kinaba: long rabbits_[] = {5, 8, 58}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {13, 22, 64}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 58; 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: long rabbits_[] = {0, 1, 2}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {1, 2, 3}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 100; 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: long rabbits_[] = {5, 8, 58}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {20, 26, 61}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 58; 4fd800b3a8 2011-02-23 kinaba: int _ = 537851168; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: long rabbits_[] = {67, 281, 2348}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {235, 1394, 3293}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 83; 4fd800b3a8 2011-02-23 kinaba: int _ = 167142023; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(7) 4fd800b3a8 2011-02-23 kinaba: long rabbits_[] = {-1000000000000000000, 999999999999999998, 999999999999999999}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> rabbits(rabbits_, rabbits_+sizeof(rabbits_)/sizeof(*rabbits_)); 4fd800b3a8 2011-02-23 kinaba: long nests_[] = {-1000000000000000000, 999999999999999999, 1000000000000000000}; 4fd800b3a8 2011-02-23 kinaba: vector<long long> nests(nests_, nests_+sizeof(nests_)/sizeof(*nests_)); 4fd800b3a8 2011-02-23 kinaba: int K = 5; 4fd800b3a8 2011-02-23 kinaba: int _ = 29; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE