File Annotation
Not logged in
af429d4fc8 2012-06-23        kinaba: #include <iostream>
af429d4fc8 2012-06-23        kinaba: #include <sstream>
af429d4fc8 2012-06-23        kinaba: #include <iomanip>
af429d4fc8 2012-06-23        kinaba: #include <vector>
af429d4fc8 2012-06-23        kinaba: #include <string>
af429d4fc8 2012-06-23        kinaba: #include <map>
af429d4fc8 2012-06-23        kinaba: #include <set>
af429d4fc8 2012-06-23        kinaba: #include <algorithm>
af429d4fc8 2012-06-23        kinaba: #include <numeric>
af429d4fc8 2012-06-23        kinaba: #include <iterator>
af429d4fc8 2012-06-23        kinaba: #include <functional>
af429d4fc8 2012-06-23        kinaba: #include <complex>
af429d4fc8 2012-06-23        kinaba: #include <queue>
af429d4fc8 2012-06-23        kinaba: #include <stack>
af429d4fc8 2012-06-23        kinaba: #include <cmath>
af429d4fc8 2012-06-23        kinaba: #include <cassert>
af429d4fc8 2012-06-23        kinaba: using namespace std;
af429d4fc8 2012-06-23        kinaba: typedef long long LL;
af429d4fc8 2012-06-23        kinaba: typedef long double LD;
af429d4fc8 2012-06-23        kinaba: typedef complex<LD> CMP;
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: static const int MODVAL = 1000000009;
af429d4fc8 2012-06-23        kinaba: struct mint
af429d4fc8 2012-06-23        kinaba: {
af429d4fc8 2012-06-23        kinaba: 	int val;
af429d4fc8 2012-06-23        kinaba: 	mint():val(0){}
af429d4fc8 2012-06-23        kinaba: 	mint(int    x):val(x%MODVAL) {}
af429d4fc8 2012-06-23        kinaba: 	mint(size_t x):val(x%MODVAL) {}
af429d4fc8 2012-06-23        kinaba: 	mint(LL     x):val(x%MODVAL) {}
af429d4fc8 2012-06-23        kinaba: };
af429d4fc8 2012-06-23        kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
af429d4fc8 2012-06-23        kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
af429d4fc8 2012-06-23        kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
af429d4fc8 2012-06-23        kinaba: mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }
af429d4fc8 2012-06-23        kinaba: mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); }
af429d4fc8 2012-06-23        kinaba: mint operator+(mint x, mint y) { return x+=y; }
af429d4fc8 2012-06-23        kinaba: mint operator-(mint x, mint y) { return x-=y; }
af429d4fc8 2012-06-23        kinaba: mint operator*(mint x, mint y) { return x*=y; }
af429d4fc8 2012-06-23        kinaba: mint operator/(mint x, mint y) { return x/=y; }
af429d4fc8 2012-06-23        kinaba: vector<mint> FAC_(1,1);
af429d4fc8 2012-06-23        kinaba: mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }
af429d4fc8 2012-06-23        kinaba: vector< vector<mint> > CP_; // Pascal's triangle: if O(1) nCk is needed.
af429d4fc8 2012-06-23        kinaba: mint C(LL n, LL k) {
af429d4fc8 2012-06-23        kinaba: 	while( CP_.size() <= n ) {
af429d4fc8 2012-06-23        kinaba: 		int nn = CP_.size();
af429d4fc8 2012-06-23        kinaba: 		CP_.push_back(vector<mint>(nn+1,1));
af429d4fc8 2012-06-23        kinaba: 		for(int kk=1; kk<nn; ++kk)
af429d4fc8 2012-06-23        kinaba: 			CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk];
af429d4fc8 2012-06-23        kinaba: 	}
af429d4fc8 2012-06-23        kinaba: 	return k<0 || n<k ? 0 : CP_[n][k];
af429d4fc8 2012-06-23        kinaba: }
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: class FleaCircus { public:
af429d4fc8 2012-06-23        kinaba: 	int countArrangements(vector <string> afterFourClicks)
af429d4fc8 2012-06-23        kinaba: 	{
af429d4fc8 2012-06-23        kinaba: 		stringstream sin(accumulate(afterFourClicks.begin(), afterFourClicks.end(), string("")));
af429d4fc8 2012-06-23        kinaba: 		vector<int> v;
af429d4fc8 2012-06-23        kinaba: 		for(int n; sin>>n; )
af429d4fc8 2012-06-23        kinaba: 			v.push_back(n);
af429d4fc8 2012-06-23        kinaba: 		return solve(v, v.size());
af429d4fc8 2012-06-23        kinaba: 	}
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 	int solve(const vector<int>& S, int N)
af429d4fc8 2012-06-23        kinaba: 	{
af429d4fc8 2012-06-23        kinaba: 		vector<int> cycles;
af429d4fc8 2012-06-23        kinaba: 		{
af429d4fc8 2012-06-23        kinaba: 			vector<bool> used(N, false);
af429d4fc8 2012-06-23        kinaba: 			for(int s=0; s<N; ++s)
af429d4fc8 2012-06-23        kinaba: 				if(!used[s]) {
af429d4fc8 2012-06-23        kinaba: 					int len = 0;
af429d4fc8 2012-06-23        kinaba: 					do {
af429d4fc8 2012-06-23        kinaba: 						used[s] = true;
af429d4fc8 2012-06-23        kinaba: 						s = S[s];
af429d4fc8 2012-06-23        kinaba: 						++len;
af429d4fc8 2012-06-23        kinaba: 					} while( !used[s] );
af429d4fc8 2012-06-23        kinaba: 					cycles.push_back(len);
af429d4fc8 2012-06-23        kinaba: 				}
af429d4fc8 2012-06-23        kinaba: 		}
af429d4fc8 2012-06-23        kinaba: 		return solvesolve(cycles);
af429d4fc8 2012-06-23        kinaba: 	}
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 	int solvesolve(const vector<int>& cycles)
af429d4fc8 2012-06-23        kinaba: 	{
af429d4fc8 2012-06-23        kinaba: 		map<int, int> cc;
af429d4fc8 2012-06-23        kinaba: 		for(int i=0; i<cycles.size(); ++i)
af429d4fc8 2012-06-23        kinaba: 			cc[cycles[i]] ++;
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 		mint total = 1;
af429d4fc8 2012-06-23        kinaba: 		for(map<int,int>::iterator it=cc.begin(); it!=cc.end(); ++it)
af429d4fc8 2012-06-23        kinaba: 		{
af429d4fc8 2012-06-23        kinaba: 			int clen = it->first;
af429d4fc8 2012-06-23        kinaba: 			int num = it->second;
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 			mint my = 0;
af429d4fc8 2012-06-23        kinaba: 			if(clen%2 == 0)
af429d4fc8 2012-06-23        kinaba: 			{
af429d4fc8 2012-06-23        kinaba: 				// 4n -> n*4
af429d4fc8 2012-06-23        kinaba: 				if( num%4 != 0 )
af429d4fc8 2012-06-23        kinaba: 					return 0;
af429d4fc8 2012-06-23        kinaba: 				my = split<4>(num) * POW(POW(clen,3)*3*2*1, num/4);
af429d4fc8 2012-06-23        kinaba: 			}
af429d4fc8 2012-06-23        kinaba: 			else
af429d4fc8 2012-06-23        kinaba: 			{
af429d4fc8 2012-06-23        kinaba: 				//  o -> o * 1
af429d4fc8 2012-06-23        kinaba: 				// 2o -> o * 2
af429d4fc8 2012-06-23        kinaba: 				// 4o -> o * 4
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 				for(int fnum=0; fnum<=num; fnum+=4)
af429d4fc8 2012-06-23        kinaba: 					for(int tnum=0; fnum+tnum<=num; tnum+=2) {
af429d4fc8 2012-06-23        kinaba: 						int onum=num-fnum-tnum;
af429d4fc8 2012-06-23        kinaba: 						my += split<4>(fnum)*POW(POW(clen,3)*3*2*1, fnum/4)
af429d4fc8 2012-06-23        kinaba: 							 *split<2>(tnum)*POW(POW(clen,1)*1, tnum/2)
af429d4fc8 2012-06-23        kinaba: 							 *C(num,fnum)*C(num-fnum,tnum);
af429d4fc8 2012-06-23        kinaba: 					}
af429d4fc8 2012-06-23        kinaba: 			}
af429d4fc8 2012-06-23        kinaba: 			total *= my;
af429d4fc8 2012-06-23        kinaba: 		}
af429d4fc8 2012-06-23        kinaba: 		return total.val;
af429d4fc8 2012-06-23        kinaba: 	}
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: 	template<int k>
af429d4fc8 2012-06-23        kinaba: 	mint split(int n)
af429d4fc8 2012-06-23        kinaba: 	{
af429d4fc8 2012-06-23        kinaba: 		static vector<mint> memo(10000);
af429d4fc8 2012-06-23        kinaba: 		static vector<bool> used(10000);
af429d4fc8 2012-06-23        kinaba: 		if( n<=k )
af429d4fc8 2012-06-23        kinaba: 			return 1;
af429d4fc8 2012-06-23        kinaba: 		else {
af429d4fc8 2012-06-23        kinaba: 			if(used[n])
af429d4fc8 2012-06-23        kinaba: 				return memo[n];
af429d4fc8 2012-06-23        kinaba: 			used[n] = true;
af429d4fc8 2012-06-23        kinaba: 			return memo[n] = C(n-1,k-1) * split<k>(n-k);
af429d4fc8 2012-06-23        kinaba: 		}
af429d4fc8 2012-06-23        kinaba: 	}
af429d4fc8 2012-06-23        kinaba: };
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: // BEGIN CUT HERE
af429d4fc8 2012-06-23        kinaba: #include <ctime>
af429d4fc8 2012-06-23        kinaba: double start_time; string timer()
af429d4fc8 2012-06-23        kinaba:  { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
af429d4fc8 2012-06-23        kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
af429d4fc8 2012-06-23        kinaba:  { os << "{ ";
af429d4fc8 2012-06-23        kinaba:    for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
af429d4fc8 2012-06-23        kinaba:    os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
af429d4fc8 2012-06-23        kinaba: void verify_case(const int& Expected, const int& Received) {
af429d4fc8 2012-06-23        kinaba:  bool ok = (Expected == Received);
af429d4fc8 2012-06-23        kinaba:  if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
af429d4fc8 2012-06-23        kinaba:  cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
af429d4fc8 2012-06-23        kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
af429d4fc8 2012-06-23        kinaba: #define END	 verify_case(_, FleaCircus().countArrangements(afterFourClicks));}
af429d4fc8 2012-06-23        kinaba: int main(){
af429d4fc8 2012-06-23        kinaba: 
af429d4fc8 2012-06-23        kinaba: CASE(0)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"1 2 0 3"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 1;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(1)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"1 2 ", "0 3"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 1;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(2)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"0 1 2"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 4;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(3)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"0 1 2 3 5 4"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 0;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(4)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"3 6 7 9 8 2 1", "0 5 1 0 4"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 4;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(5)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = {"1 0 7 5 6 3 4 2"};
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = 48;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: /*
af429d4fc8 2012-06-23        kinaba: CASE(6)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = ;
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = ;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: CASE(7)
af429d4fc8 2012-06-23        kinaba: 	string afterFourClicks_[] = ;
af429d4fc8 2012-06-23        kinaba: 	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_));
af429d4fc8 2012-06-23        kinaba: 	int _ = ;
af429d4fc8 2012-06-23        kinaba: END
af429d4fc8 2012-06-23        kinaba: */
af429d4fc8 2012-06-23        kinaba: }
af429d4fc8 2012-06-23        kinaba: // END CUT HERE