Artifact Content
Not logged in

Artifact 35a3e3aba95688f7aca063ca8240781311cb81c3


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

static const LL MODVAL = 1000000009;

LL ADD(LL x, LL y) { return (x+y)%MODVAL; }
LL SUB(LL x, LL y) { return (x-y+MODVAL)%MODVAL; }
LL MUL(LL x, LL y) { return (x*y)%MODVAL; }
LL POW(LL x, LL e) {
	LL v = 1;
	for(;e;x=MUL(x,x),e>>=1)
		if(e&1)
			v = MUL(v, x);
	return v;
}
LL DIV(LL x, LL y) { return MUL(x, POW(y, MODVAL-2)); }
LL C_impl(LL n, LL k) {
	LL v = 1;
	for(LL i=1; i<=k; ++i)
		v = DIV(MUL(v, n-i+1), i);
	return v;
}
LL C_tbl[40][40];
LL C(LL n, LL k) {
	if( C_tbl[n][k] < 0 )
		C_tbl[n][k] = C_impl(n,k);
	return C_tbl[n][k];
}

class RooksParty { public:
	int countArrangements(int rows, int columns, vector <int> counts) 
	{
		for(int n=0;n<40;++n)
			for(int k=0;k<40;++k)
				C_tbl[n][k] = -1;
		sort(counts.rbegin(), counts.rend());
		return (int)rec(rows, columns, counts, 0);	
	}
	map<pair<pair<int,int>,int>, LL> memo;
	LL rec(int H, int W, vector<int>& counts, int i)
	{
		if( i == counts.size() )
			return 1;
		int n = counts[i];
		if( H*W < n )
			return 0;

		if(H>W)
			swap(H,W);

		pair<pair<int,int>,int> key(make_pair(H,W),i);
		if( memo.count(key) )
			return memo[key];

		LL cnt = 0;
		for(int h=1; h<=min(H,n); ++h)
			for(int w=1; w<=min(W,n); ++w)
				if( h*w >= n )
					cnt = ADD(cnt, MUL(MUL(C(H,h), C(W,w)), MUL(count(h,w,n),rec(H-h,W-w,counts,i+1))));
		return memo[key] = cnt;
	}

	map<pair<pair<int,int>,int>, LL> memo2;
	LL count(int h, int w, int n)
	{
		if( h*w < n )
			return 0;
		pair<pair<int,int>,int> key(make_pair(h,w),n);
		if(memo2.count(key))
			return memo2[key];
		LL cnt = freedom(h,w,n);
		for(int t=1; t<=w-1; ++t)
			cnt = SUB(cnt, MUL(count(h,w-t,n),C(w,t)));
		return memo2[key] = cnt;
	}

	map<pair<pair<int,int>,int>, LL> memo3;
	LL freedom(int h, int w, int n)
	{
		if(h*w<n) return 0;
		//if(h>n)   return 0;
		if(h==0)  return 1;
		pair<pair<int,int>,int> key(make_pair(h,w),n);
		if(memo3.count(key))
			return memo3[key];

		LL cnt = 0;
		for(int t=0; t<=min(n,w); ++t)
			cnt = ADD(cnt, MUL(freedom(h-1, w, n-t), C(w,t)));
		return memo3[key]=cnt;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, RooksParty().countArrangements(rows, columns, counts));}
int main(){

CASE(0)
	int rows = 2; 
	int columns = 3; 
	int counts_[] = {1,1};
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = 12; 
END
CASE(1)
	int rows = 5; 
	int columns = 2; 
	int counts_[] = {3};
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = 120; 
END
CASE(2)
	int rows = 5; 
	int columns = 2; 
	int counts_[] = {1,1,1};
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = 0; 
END
CASE(3)
	int rows = 8; 
	int columns = 8; 
	int counts_[] = {1,1,1,1,1,1,1,1};
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = 625702391; 
END
CASE(4)
	int rows = 4; 
	int columns = 2; 
	int counts_[] = {3,1};
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = 8; 
END
/*
CASE(5)
	int rows = ; 
	int columns = ; 
	int counts_[] = ;
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = ; 
END
CASE(6)
	int rows = ; 
	int columns = ; 
	int counts_[] = ;
	  vector <int> counts(counts_, counts_+sizeof(counts_)/sizeof(*counts_)); 
	int _ = ; 
END
*/
}
// END CUT HERE