Artifact Content
Not logged in

Artifact 246d5d9c483933cb34a7622cc075d4ca3de50750


#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>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> CMP;

static const int MODVAL = 1000000009;
struct mint
{
	int val;
	mint():val(0){}
	mint(int    x):val(x%MODVAL) {}
	mint(size_t x):val(x%MODVAL) {}
	mint(LL     x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL-2); }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }
mint operator/(mint x, mint y) { return x/=y; }
vector<mint> FAC_(1,1);
mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; }
vector< vector<mint> > CP_; // Pascal's triangle: if O(1) nCk is needed.
mint C(LL n, LL k) {
	while( CP_.size() <= n ) {
		int nn = CP_.size();
		CP_.push_back(vector<mint>(nn+1,1));
		for(int kk=1; kk<nn; ++kk)
			CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk];
	}
	return k<0 || n<k ? 0 : CP_[n][k];
}

class FleaCircus { public:
	int countArrangements(vector <string> afterFourClicks)
	{
		stringstream sin(accumulate(afterFourClicks.begin(), afterFourClicks.end(), string("")));
		vector<int> v;
		for(int n; sin>>n; )
			v.push_back(n);
		return solve(v, v.size());
	}

	int solve(const vector<int>& S, int N)
	{
		vector<int> cycles;
		{
			vector<bool> used(N, false);
			for(int s=0; s<N; ++s)
				if(!used[s]) {
					int len = 0;
					do {
						used[s] = true;
						s = S[s];
						++len;
					} while( !used[s] );
					cycles.push_back(len);
				}
		}
		return solvesolve(cycles);
	}

	int solvesolve(const vector<int>& cycles)
	{
		map<int, int> cc;
		for(int i=0; i<cycles.size(); ++i)
			cc[cycles[i]] ++;

		mint total = 1;
		for(map<int,int>::iterator it=cc.begin(); it!=cc.end(); ++it)
		{
			int clen = it->first;
			int num = it->second;

			mint my = 0;
			if(clen%2 == 0)
			{
				// 4n -> n*4
				if( num%4 != 0 )
					return 0;
				my = split<4>(num) * POW(POW(clen,3)*3*2*1, num/4);
			}
			else
			{
				//  o -> o * 1
				// 2o -> o * 2
				// 4o -> o * 4

				for(int fnum=0; fnum<=num; fnum+=4)
					for(int tnum=0; fnum+tnum<=num; tnum+=2) {
						int onum=num-fnum-tnum;
						my += split<4>(fnum)*POW(POW(clen,3)*3*2*1, fnum/4)
							 *split<2>(tnum)*POW(POW(clen,1)*1, tnum/2)
							 *C(num,fnum)*C(num-fnum,tnum);
					}
			}
			total *= my;
		}
		return total.val;
	}

	template<int k>
	mint split(int n)
	{
		static vector<mint> memo(10000);
		static vector<bool> used(10000);
		if( n<=k )
			return 1;
		else {
			if(used[n])
				return memo[n];
			used[n] = true;
			return memo[n] = C(n-1,k-1) * split<k>(n-k);
		}
	}
};

// 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(_, FleaCircus().countArrangements(afterFourClicks));}
int main(){

CASE(0)
	string afterFourClicks_[] = {"1 2 0 3"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 1; 
END
CASE(1)
	string afterFourClicks_[] = {"1 2 ", "0 3"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 1; 
END
CASE(2)
	string afterFourClicks_[] = {"0 1 2"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 4; 
END
CASE(3)
	string afterFourClicks_[] = {"0 1 2 3 5 4"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 0; 
END
CASE(4)
	string afterFourClicks_[] = {"3 6 7 9 8 2 1", "0 5 1 0 4"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 4; 
END
CASE(5)
	string afterFourClicks_[] = {"1 0 7 5 6 3 4 2"};
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = 48; 
END
/*
CASE(6)
	string afterFourClicks_[] = ;
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = ; 
END
CASE(7)
	string afterFourClicks_[] = ;
	  vector <string> afterFourClicks(afterFourClicks_, afterFourClicks_+sizeof(afterFourClicks_)/sizeof(*afterFourClicks_)); 
	int _ = ; 
END
*/
}
// END CUT HERE