74d1f45c5c 2014-10-14 kinaba: #include <iostream> 74d1f45c5c 2014-10-14 kinaba: #include <sstream> 74d1f45c5c 2014-10-14 kinaba: #include <iomanip> 74d1f45c5c 2014-10-14 kinaba: #include <vector> 74d1f45c5c 2014-10-14 kinaba: #include <string> 74d1f45c5c 2014-10-14 kinaba: #include <map> 74d1f45c5c 2014-10-14 kinaba: #include <set> 74d1f45c5c 2014-10-14 kinaba: #include <algorithm> 74d1f45c5c 2014-10-14 kinaba: #include <numeric> 74d1f45c5c 2014-10-14 kinaba: #include <iterator> 74d1f45c5c 2014-10-14 kinaba: #include <functional> 74d1f45c5c 2014-10-14 kinaba: #include <complex> 74d1f45c5c 2014-10-14 kinaba: #include <queue> 74d1f45c5c 2014-10-14 kinaba: #include <stack> 74d1f45c5c 2014-10-14 kinaba: #include <cmath> 74d1f45c5c 2014-10-14 kinaba: #include <cassert> 74d1f45c5c 2014-10-14 kinaba: #include <tuple> 74d1f45c5c 2014-10-14 kinaba: using namespace std; 74d1f45c5c 2014-10-14 kinaba: typedef long long LL; 74d1f45c5c 2014-10-14 kinaba: typedef complex<double> CMP; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: static const unsigned MODVAL = 1000000009; 74d1f45c5c 2014-10-14 kinaba: struct mint 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: unsigned val; 74d1f45c5c 2014-10-14 kinaba: mint():val(0){} 74d1f45c5c 2014-10-14 kinaba: mint(int x):val(x%MODVAL) {} 74d1f45c5c 2014-10-14 kinaba: mint(unsigned x):val(x%MODVAL) {} 74d1f45c5c 2014-10-14 kinaba: mint(LL x):val(x%MODVAL) {} 74d1f45c5c 2014-10-14 kinaba: }; 74d1f45c5c 2014-10-14 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 74d1f45c5c 2014-10-14 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 74d1f45c5c 2014-10-14 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 74d1f45c5c 2014-10-14 kinaba: mint operator+(mint x, mint y) { return x+=y; } 74d1f45c5c 2014-10-14 kinaba: mint operator-(mint x, mint y) { return x-=y; } 74d1f45c5c 2014-10-14 kinaba: mint operator*(mint x, mint y) { return x*=y; } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; } 74d1f45c5c 2014-10-14 kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); } 74d1f45c5c 2014-10-14 kinaba: mint operator/(mint x, mint y) { return x/=y; } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: class ColourHolic { public: 74d1f45c5c 2014-10-14 kinaba: int countSequences(vector <int> n) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: mint ans = 0; 74d1f45c5c 2014-10-14 kinaba: for(int c=0; c<4; ++c) if(n[c]) { 74d1f45c5c 2014-10-14 kinaba: n[c]--; 74d1f45c5c 2014-10-14 kinaba: ans += calc(c, n); 74d1f45c5c 2014-10-14 kinaba: n[c]++; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: return ans.val; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: map<LL, mint> memo; 74d1f45c5c 2014-10-14 kinaba: mint calc(int cp, vector<int>& n) 74d1f45c5c 2014-10-14 kinaba: { 74d1f45c5c 2014-10-14 kinaba: if(n[0]+n[1]+n[2]+n[3]==0) 74d1f45c5c 2014-10-14 kinaba: return 1; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: vector<int> nn; 74d1f45c5c 2014-10-14 kinaba: for(int c=0; c<4; ++c) if(c!=cp) 74d1f45c5c 2014-10-14 kinaba: nn.push_back(n[c]); 74d1f45c5c 2014-10-14 kinaba: sort(nn.begin(), nn.end()); 74d1f45c5c 2014-10-14 kinaba: LL key = n[cp]; 74d1f45c5c 2014-10-14 kinaba: for(int v: nn) 74d1f45c5c 2014-10-14 kinaba: key=key*50001+v; 74d1f45c5c 2014-10-14 kinaba: if(memo.count(key)) 74d1f45c5c 2014-10-14 kinaba: return memo[key]; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: int tot = accumulate(n.begin(), n.end(), 0); 74d1f45c5c 2014-10-14 kinaba: int ma = max(nn.front(), nn.back()); 74d1f45c5c 2014-10-14 kinaba: if(ma*2 > tot+1) 74d1f45c5c 2014-10-14 kinaba: return 0; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: mint ans = 0; 74d1f45c5c 2014-10-14 kinaba: for(int c=0; c<4; ++c) if(c!=cp && n[c]) { 74d1f45c5c 2014-10-14 kinaba: n[c]--; 74d1f45c5c 2014-10-14 kinaba: ans += calc(c, n); 74d1f45c5c 2014-10-14 kinaba: n[c]++; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: return memo[key] = ans; 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: }; 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: // BEGIN CUT HERE 74d1f45c5c 2014-10-14 kinaba: #include <ctime> 74d1f45c5c 2014-10-14 kinaba: double start_time; string timer() 74d1f45c5c 2014-10-14 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 74d1f45c5c 2014-10-14 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 74d1f45c5c 2014-10-14 kinaba: { os << "{ "; 74d1f45c5c 2014-10-14 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 74d1f45c5c 2014-10-14 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 74d1f45c5c 2014-10-14 kinaba: void verify_case(const int& Expected, const int& Received) { 74d1f45c5c 2014-10-14 kinaba: bool ok = (Expected == Received); 74d1f45c5c 2014-10-14 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 74d1f45c5c 2014-10-14 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 74d1f45c5c 2014-10-14 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 74d1f45c5c 2014-10-14 kinaba: #define END verify_case(_, ColourHolic().countSequences(n));} 74d1f45c5c 2014-10-14 kinaba: int main(){ 74d1f45c5c 2014-10-14 kinaba: 74d1f45c5c 2014-10-14 kinaba: CASE(0) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {1,0,2,3}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 10; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(1) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {1,1,1,1}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 24; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(2) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {42,42,42,9001}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 0; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(3) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {8,0,0,8}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 2; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(4) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {0,5,1,3}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 4; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(5) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {4,2,1,3}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 1074; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(6) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {15,900,357,22}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 0; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(7) 74d1f45c5c 2014-10-14 kinaba: int n_[] = {110,30000,200,29999}; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = 118115350; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: /* 74d1f45c5c 2014-10-14 kinaba: CASE(8) 74d1f45c5c 2014-10-14 kinaba: int n_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: CASE(9) 74d1f45c5c 2014-10-14 kinaba: int n_[] = ; 74d1f45c5c 2014-10-14 kinaba: vector <int> n(n_, n_+sizeof(n_)/sizeof(*n_)); 74d1f45c5c 2014-10-14 kinaba: int _ = ; 74d1f45c5c 2014-10-14 kinaba: END 74d1f45c5c 2014-10-14 kinaba: */ 74d1f45c5c 2014-10-14 kinaba: } 74d1f45c5c 2014-10-14 kinaba: // END CUT HERE