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; 4fd800b3a8 2011-02-23 kinaba: 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: LL C_impl(LL n, LL k) { 4fd800b3a8 2011-02-23 kinaba: LL v = 1; 4fd800b3a8 2011-02-23 kinaba: for(LL i=1; i<=k; ++i) 4fd800b3a8 2011-02-23 kinaba: v = DIV(MUL(v, n-i+1), i); 4fd800b3a8 2011-02-23 kinaba: return v; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: LL C_tbl[40][40]; 4fd800b3a8 2011-02-23 kinaba: LL C(LL n, LL k) { 4fd800b3a8 2011-02-23 kinaba: if( C_tbl[n][k] < 0 ) 4fd800b3a8 2011-02-23 kinaba: C_tbl[n][k] = C_impl(n,k); 4fd800b3a8 2011-02-23 kinaba: return C_tbl[n][k]; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class RooksParty { public: 4fd800b3a8 2011-02-23 kinaba: int countArrangements(int rows, int columns, vector <int> counts) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: for(int n=0;n<40;++n) 4fd800b3a8 2011-02-23 kinaba: for(int k=0;k<40;++k) 4fd800b3a8 2011-02-23 kinaba: C_tbl[n][k] = -1; 4fd800b3a8 2011-02-23 kinaba: sort(counts.rbegin(), counts.rend()); 4fd800b3a8 2011-02-23 kinaba: return (int)rec(rows, columns, counts, 0); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: map<pair<pair<int,int>,int>, LL> memo; 4fd800b3a8 2011-02-23 kinaba: LL rec(int H, int W, vector<int>& counts, int i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( i == counts.size() ) 4fd800b3a8 2011-02-23 kinaba: return 1; 4fd800b3a8 2011-02-23 kinaba: int n = counts[i]; 4fd800b3a8 2011-02-23 kinaba: if( H*W < n ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: if(H>W) 4fd800b3a8 2011-02-23 kinaba: swap(H,W); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: pair<pair<int,int>,int> key(make_pair(H,W),i); 4fd800b3a8 2011-02-23 kinaba: if( memo.count(key) ) 4fd800b3a8 2011-02-23 kinaba: return memo[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL cnt = 0; 4fd800b3a8 2011-02-23 kinaba: for(int h=1; h<=min(H,n); ++h) 4fd800b3a8 2011-02-23 kinaba: for(int w=1; w<=min(W,n); ++w) 4fd800b3a8 2011-02-23 kinaba: if( h*w >= n ) 4fd800b3a8 2011-02-23 kinaba: cnt = ADD(cnt, MUL(MUL(C(H,h), C(W,w)), MUL(count(h,w,n),rec(H-h,W-w,counts,i+1)))); 4fd800b3a8 2011-02-23 kinaba: return memo[key] = cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<pair<pair<int,int>,int>, LL> memo2; 4fd800b3a8 2011-02-23 kinaba: LL count(int h, int w, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if( h*w < n ) 4fd800b3a8 2011-02-23 kinaba: return 0; 4fd800b3a8 2011-02-23 kinaba: pair<pair<int,int>,int> key(make_pair(h,w),n); 4fd800b3a8 2011-02-23 kinaba: if(memo2.count(key)) 4fd800b3a8 2011-02-23 kinaba: return memo2[key]; 4fd800b3a8 2011-02-23 kinaba: LL cnt = freedom(h,w,n); 4fd800b3a8 2011-02-23 kinaba: for(int t=1; t<=w-1; ++t) 4fd800b3a8 2011-02-23 kinaba: cnt = SUB(cnt, MUL(count(h,w-t,n),C(w,t))); 4fd800b3a8 2011-02-23 kinaba: return memo2[key] = cnt; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: map<pair<pair<int,int>,int>, LL> memo3; 4fd800b3a8 2011-02-23 kinaba: LL freedom(int h, int w, int n) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: if(h*w<n) return 0; 4fd800b3a8 2011-02-23 kinaba: //if(h>n) return 0; 4fd800b3a8 2011-02-23 kinaba: if(h==0) return 1; 4fd800b3a8 2011-02-23 kinaba: pair<pair<int,int>,int> key(make_pair(h,w),n); 4fd800b3a8 2011-02-23 kinaba: if(memo3.count(key)) 4fd800b3a8 2011-02-23 kinaba: return memo3[key]; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: LL cnt = 0; 4fd800b3a8 2011-02-23 kinaba: for(int t=0; t<=min(n,w); ++t) 4fd800b3a8 2011-02-23 kinaba: cnt = ADD(cnt, MUL(freedom(h-1, w, n-t), C(w,t))); 4fd800b3a8 2011-02-23 kinaba: return memo3[key]=cnt; 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(_, RooksParty().countArrangements(rows, columns, counts));} 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 rows = 2; 4fd800b3a8 2011-02-23 kinaba: int columns = 3; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = {1,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 12; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(1) 4fd800b3a8 2011-02-23 kinaba: int rows = 5; 4fd800b3a8 2011-02-23 kinaba: int columns = 2; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = {3}; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 120; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(2) 4fd800b3a8 2011-02-23 kinaba: int rows = 5; 4fd800b3a8 2011-02-23 kinaba: int columns = 2; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = {1,1,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 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: int rows = 8; 4fd800b3a8 2011-02-23 kinaba: int columns = 8; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = {1,1,1,1,1,1,1,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 625702391; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(4) 4fd800b3a8 2011-02-23 kinaba: int rows = 4; 4fd800b3a8 2011-02-23 kinaba: int columns = 2; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = {3,1}; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = 8; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: /* 4fd800b3a8 2011-02-23 kinaba: CASE(5) 4fd800b3a8 2011-02-23 kinaba: int rows = ; 4fd800b3a8 2011-02-23 kinaba: int columns = ; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: CASE(6) 4fd800b3a8 2011-02-23 kinaba: int rows = ; 4fd800b3a8 2011-02-23 kinaba: int columns = ; 4fd800b3a8 2011-02-23 kinaba: int counts_[] = ; 4fd800b3a8 2011-02-23 kinaba: vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 4fd800b3a8 2011-02-23 kinaba: int _ = ; 4fd800b3a8 2011-02-23 kinaba: END 4fd800b3a8 2011-02-23 kinaba: */ 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE