某岛

… : "…アッカリ~ン . .. . " .. .
June 17, 2012

SRM 546

Brief description:

Problem 250. KleofasTail
给定下面的变幻规则 。。。

x->2x | x 是任意数
x->2x+1 | x 是偶数

问从 x 开始变幻,落在区间 [l, r] 之间的数字的个数。

Problem 500. FavouriteDigits
求满足数位中有 c1 个 d1, c2 个 d2 的大于等于 n 的最小数。

Problem 1000. FleaCircus
设 P 是一个置换,现在给定 P^4 。。。
计数:所有可能满足的 P。

Analysis:

Problem 250. KleofasTail
。。。现场生代码直接暴力 TLE 了。。。
(我只略微估计了一下暴力是 O(n) 的就交了,没考虑一些比较极限的情况。
实际上在这个基础上稍加考虑就不难发现分阶段后变幻的数字总是在一个区间内。。求交累加即可。。。

Problem 500. FavouriteDigits
1. 构造出含有 c1 个 d1, c2 个 d2 的最小数 t。
2. 如果 t <= n 打印 t。。。否则,设 n 的位数等于 len。构造出 len 位最大符合条件的数 t。 3. 如果 t <= n ,运行一个 len 位的 dfs() 构造。。 4. 如果 t >= n ,运行一个 (len+1) 位的 dfs() 构造。。 (设置第一位等于 1。。。

。。。(现场生代码 dfs() 写错了几个地方。。然后对 (d1 == 0) 的一些情况。。
在求 t 的时候也没有进行一些调整。。。 /$:^ ^

Problem 1000. FleaCircus
进行一次推广。。(4 这个条件明显不自然。。
那么正向考察一个置换 P 和它的 k 次幂的循环节。
对于长度为 l 的一个循环节,那么变幻过后
将会生成 gcd(k, l) 个 l / gcd(k, l) 长度的循环节。。

(所以主算法框架即对不同长度的循环节当做整体进行计数,然后全部乘起来。
考虑反向问题的话大概还要进行一些枚举,我们设 P^4 中长度为 l 的循环节有 cnt[l] 个。)

对于 k = 4 的情况进行细部分析。。得到以下结论:
如果 l 是偶数,那么只可能通过 4l 得到。。(此时如果 cnt[l] 不能整除 4 将造成无解。。这里也是造成无解的唯一情况。
如果 l 是奇数,那么 l 可以通过 l, 2l 和 4l 得到。。(具体需要枚举。。。

。。。先做好函数 f(a, b)
(这里表示循环节长度为 b,且在原置换中是由长度 ab 的循环节得到的。。a 只有 2 和 4 两种可能。。。

int f(int a, int b){
    return a == 4 ? cub(b)*6 : b;
}

(这里只给出 f(4, 3)的图例。。对于 1 可以寻找本组外的 9 个数字中的任意一个对应,并且一旦 1 找到归宿后,
本组内的其他几个数也会依次找到情感的归宿。。(咦?。。
所以是 (b*3) * (b*2) * (b*1) = b^3 * 6 …

{1 -> 2 -> 3 -> 1 .. . }
{4 -> 5 -> 6 -> .. .. .}
{7 -> 8 -> 9 .. .}
{10 -> 11 -> 12 .. .}

那么现在只要考虑 l 等于奇数时的子问题即可了。。
有 n 个互不相同的球,问分成若干个 1个1个 一组,或者2个2个 一组 或者 4个4个 一组一共有多少种分组方案。

.. .
        int res = 1, tmp, r1, n1, r2, n2; REP_1(i, n) if (cnt[i]){
            if (i&1){
                tmp = 0, r1 = 1, n1 = cnt[i]; FOR_1_C(j, 0, n1/4){
                    r2 = r1, n2 = n1; FOR_1_C(k, 0, n2/2){
                        INC(tmp, qtt(r2, pdt(F[j], F[k]))), MUL(r2, C[n2][2]), MUL(r2, f(2, i)), n2 -= 2;
                    }
                    MUL(r1, C[n1][4]), MUL(r1, f(4, i)), n1 -= 4;
                }
            }
            else {
                tmp = _I(F[(n1=cnt[i])/4]);
                while (n1) MUL(tmp, C[n1][4]), MUL(tmp, f(4, i)), n1 -= 4;

            }
            MUL(res, tmp);
        }
.. .

。。这里外层循环枚举有多少组 4个4个 一组的。。内层循环枚举有多少 2个2个 一组的。。
r1, n1 表示当前外层计算的结果,n1 表示枚举过 4个4个 后还剩多少个球。。
r2, n2 类似。。(注意累加时避免重复计数即可。。。

/* -&$&#*( &#*@)^$@&*)*/

class KleofasTail {
public:
	long long countGoodSequences(LL s, LL l, LL r) {
	    LL res = 0; if (!s){s = 1; if (!l) res = 1;}
        LL ll = s, rr = s + !(s&1);
	    while (true){
	        if (ll > r) break;
	        res += max(0LL, min(r, rr) - max(l, ll) + 1);
	        ll <<= 1, rr <<= 1, rr |= 1;
	    }
	    return res;
	}
};
/* &*#()&*#)&E*F& */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

#define REP(I, N) for (int I=0;I<int(N);++I)
#define FOR(I, A, B) for (int I=int(A);I<int(B);++I)
#define DWN(I, B, A) for (int I=int(B-1);I>=int(A);--I)
#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)

#define ALL(A) A.begin(), A.end()
#define CLR(A) A.clear()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define SRT(A) sort(ALL(A))
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

typedef long long LL;
typedef double DB;

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}

template<class T> inline void checkMin(T &a, T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a, T b){if (b>a) a=b;}

/* -&$&#*( &#*@)^$@&*)*/

const int MOD = 1000000007;
const int INF = 0x7fffffff;

const int N = 50;

int A[N], d1, d2;
LL t, res; int len;

// c1  c1 .. c1 + 1 .. c2
// c2 c2 c2 + 1 ...
// f f, f+1, c1, c2 ...
// 0000c1c1c1c2c2c2

bool dfs(int depth, int c1, int c2, bool flag){
    if (flag){
        int c3 = (len - depth) - max(0, c1) - max(0, c2);
        if (c3 < 0) return false;
        REP(i, c3) res *= 10;
        REP(i, c1) res *= 10, res += d1;
        REP(i, c2) res *= 10, res += d2;

        return true;
    }

    if (depth == len){
        return c1<=0 && c2<=0;
    }
    else {
        if (A[depth] == d2){

            //cout << depth << " " << A[depth] << endl;

            res *= 10, res += d2;
            if (dfs(depth+1, c1, c2-1, false)) return true;
            res /= 10;
            //#
        }
        else if (A[depth] == d1){

            //cout << depth << " " << A[depth] << endl;

            res *= 10, res += d1;
            if (dfs(depth+1, c1-1, c2, false)) return true;
            res /= 10;

            res *= 10, res += d1+1; //#
            if (dfs(depth+1, c1, c2 - (d1 + 1 == d2), true)) return true;
            res /= 10;

            if (d1 + 1 < d2){
                res *= 10, res += d2; //#
                if (dfs(depth+1, c1, c2 - 1, true)) return true;
                res /= 10;
            }

        }
        else {
            res *= 10, res += A[depth];
            if (dfs(depth+1, c1, c2, false)) return true;
            res /= 10;

            if (A[depth] == 9) return false;

            res *= 10, res += A[depth] + 1;
            if (dfs(depth+1, c1 - (A[depth] + 1 == d1), c2 - (A[depth] + 1 == d2), true)) return true;
            res /= 10;

            if (d1 > A[depth]){
                res *= 10, res += d1;
                if (dfs(depth+1, c1-1, c2, true)) return true;
                res /= 10;
            }
            if (d2 > A[depth]){
                res *= 10, res += d2;
                if (dfs(depth+1, c1, c2-1, true)) return true;
                res /= 10;
            }

            return false;
        }
    }

    return false;
}

class FavouriteDigits {
public:
	long long findNext(LL n, int _d1, int c1, int _d2, int c2) {

		d1 = _d1, d2 = _d2, res = 0;

        if (d1 > d2) swap(c1, c2), swap(d1, d2);

        if (!d1){
            t = c2 ? d2 : 1; REP(i, c1) t *= 10, t += d1;
            REP(i, c2-1) t *= 10, t += d2;
        }
        else{
            t = 0; REP(i, c1) t *= 10, t += d1;
            REP(i, c2) t *= 10, t += d2;
        }

        if (t >= n){
            return t;
        }
        else {
            len = int ((DB) log(n-1) / (DB) log(10)) + 1;
            
            int c3 = max(len - c1 - c2, 0);
            t = 0; REP(i, c3) t *= 10, t += 9;
            REP(i, c2) t *= 10, t += d2;
            REP(i, c1) t *= 10, t += d1;

            if (t >= n){
                DWN(i, len, 0) A[i] = n % 10, n /= 10;
                dfs(0, c1, c2, false);
                if (res == 0) res = t;
                return res;
            }
            else {
                DWN(i, len, 0) A[i+1] = n % 10, n /= 10;
                ++len, A[0] = 0; res = 1;
                dfs(1, c1 - (d1 == 1), c2 - (d2 == 1), true);
                return res;
            }

        }
	}
};


// BEGIN CUT HERE
namespace moj_harness {
	int run_test_case(int);
	void run_test(int casenum = -1, bool quiet = false) {
		if (casenum != -1) {
			if (run_test_case(casenum) == -1 && !quiet) {
				cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
			}
			return;
		}

		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = run_test_case(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}

		if (total == 0) {
			cerr << "No test cases run." << endl;
		} else if (correct < total) {
			cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
		} else {
			cerr << "All " << total << " tests passed!" << endl;
		}
	}

	int verify_case(int casenum, const long long &expected, const long long &received, clock_t elapsed) {
		cerr << "Example " << casenum << "... ";

		string verdict;
		vector<string> info;
		char buf[100];

		if (elapsed > CLOCKS_PER_SEC / 200) {
			sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
			info.push_back(buf);
		}

		if (expected == received) {
			verdict = "PASSED";
		} else {
			verdict = "FAILED";
		}

		cerr << verdict;
		if (!info.empty()) {
			cerr << " (";
			for (int i=0; i<(int)info.size(); ++i) {
				if (i > 0) cerr << ", ";
				cerr << info[i];
			}
			cerr << ")";
		}
		cerr << endl;

		if (verdict == "FAILED") {
			cerr << "    Expected: " << expected << endl;
			cerr << "    Received: " << received << endl;
		}

		return verdict == "PASSED";
	}

	int run_test_case(int casenum) {
		switch (casenum) {
		case 0: {
			long long N               = 701234568901234LL;
			int digit1                = 6;
			int count1                = 14;
			int digit2                = 0;
			int count2                = 0;
			long long expected__      = 766666666666666LL;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 1: {
			long long N               = 47;
			int digit1                = 4;
			int count1                = 8;
			int digit2                = 7;
			int count2                = 7;
			long long expected__      = 444444447777777;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 2: {
			long long N               = 47;
			int digit1                = 5;
			int count1                = 0;
			int digit2                = 3;
			int count2                = 1;
			long long expected__      = 53;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 3: {
			long long N               = 47;
			int digit1                = 2;
			int count1                = 1;
			int digit2                = 0;
			int count2                = 2;
			long long expected__      = 200;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 4: {
			long long N               = 123456789012345LL;
			int digit1                = 1;
			int count1                = 2;
			int digit2                = 2;
			int count2                = 4;
			long long expected__      = 123456789012422LL;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 5: {
			long long N               = 92;
			int digit1                = 1;
			int count1                = 1;
			int digit2                = 0;
			int count2                = 0;
			long long expected__      = 100;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}

		// custom cases

/*      case 6: {
			long long N               = ;
			int digit1                = ;
			int count1                = ;
			int digit2                = ;
			int count2                = ;
			long long expected__      = ;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 7: {
			long long N               = ;
			int digit1                = ;
			int count1                = ;
			int digit2                = ;
			int count2                = ;
			long long expected__      = ;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 8: {
			long long N               = ;
			int digit1                = ;
			int count1                = ;
			int digit2                = ;
			int count2                = ;
			long long expected__      = ;

			clock_t start__           = clock();
			long long received__      = FavouriteDigits().findNext(N, digit1, count1, digit2, count2);
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
		default:
			return -1;
		}
	}
}

int main(int argc, char *argv[]) {
	if (argc == 1) {
		moj_harness::run_test();
	} else {
		for (int i=1; i<argc; ++i)
			moj_harness::run_test(atoi(argv[i]));
	}
}
// END CUT HERE
#define LOCAL

/** ` Micro Mezzo Macro Flation -- Overheated Economy ., **/

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>

using namespace std;

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_C(i, n) for (int n____=int(n),i=0;i<n____;++i)
#define FOR_C(i, a, b) for (int b____=int(b),i=a;i<b____;++i)
#define DWN_C(i, b, a) for (int a____=int(a),i=b-1;i>=a____;--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_C(i, n) for (int n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C(i, a, b) for (int b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C(i, b, a) for (int a____=int(a),i=b;i>=a____;--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)
#define REP_C_N(i, n) for (n____=int(n),i=0;i<n____;++i)
#define FOR_C_N(i, a, b) for (b____=int(b),i=a;i<b____;++i)
#define DWN_C_N(i, b, a) for (a____=int(a),i=b-1;i>=a____;--i)
#define REP_1_C_N(i, n) for (n____=int(n),i=1;i<=n____;++i)
#define FOR_1_C_N(i, a, b) for (b____=int(b),i=a;i<=b____;++i)
#define DWN_1_C_N(i, b, a) for (a____=int(a),i=b;i>=a____;--i)

#define ECH(it, A) for (typeof(A.begin()) it=A.begin(); it != A.end(); ++it)
#define DO(n) while(n--)
#define DO_C(n) int n____ = n; while(n____--)
#define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
#define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
#define SQZ(i, j, a, b) for (int i=int(a),j=int(b)-1;i<j;++i,--j)
#define SQZ_1(i, j, a, b) for (int i=int(a),j=int(b);i<=j;++i,--j)
#define REP_2(i, j, n, m) REP(i, n) REP(j, m)
#define REP_2_1(i, j, n, m) REP_1(i, n) REP_1(j, m)

#define ALL(A) A.begin(), A.end()
#define LLA(A) A.rbegin(), A.rend()
#define CPY(A, B) memcpy(A, B, sizeof(A))
#define INS(A, P, B) A.insert(A.begin() + P, B)
#define ERS(A, P) A.erase(A.begin() + P)
#define BSC(A, X) find(ALL(A), X) // != A.end()
#define CTN(T, x) (T.find(x) != T.end())
#define SZ(A) int(A.size())
#define PB push_back
#define MP(A, B) make_pair(A, B)

#define Rush int T____; RD(T____); DO(T____)
#pragma comment(linker, "/STACK:36777216")
//#pragma GCC optimize ("O2")
#define Ruby system("ruby main.rb")
#define Haskell system("runghc main.hs")
#define Pascal system("fpc main.pas")

typedef long long LL;
typedef double DB;
typedef unsigned UINT;
typedef unsigned long long ULL;

typedef vector<int> VI;
typedef vector<char> VC;
typedef vector<string> VS;
typedef vector<LL> VL;
typedef vector<DB> VD;
typedef set<int> SI;
typedef set<string> SS;
typedef set<LL> SL;
typedef set<DB> SD;
typedef map<int, int> MII;
typedef map<string, int> MSI;
typedef map<LL, int> MLI;
typedef map<DB, int> MDI;
typedef map<int, bool> MIB;
typedef map<string, bool> MSB;
typedef map<LL, bool> MLB;
typedef map<DB, bool> MDB;
typedef pair<int, int> PII;
typedef pair<int, bool> PIB;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef vector<VII> VVII;
typedef set<PII> SII;
typedef map<PII, int> MPIII;
typedef map<PII, bool> MPIIB;

/** I/O Accelerator **/

/* ... :" We are I/O Accelerator ... Use us at your own risk ;) ... " .. */

template<class T> inline void RD(T &);
template<class T> inline void OT(const T &);

inline int RD(){ int x; RD(x); return x;}
template<class T> inline T& _RD(T &x){ RD(x); return x;}
inline void RC(char &c){scanf(" %c", &c);}
inline void RS(char *s){scanf("%s", s);}

template<class T0, class T1> inline void RD(T0 &x0, T1 &x1){RD(x0), RD(x1);}
template<class T0, class T1, class T2> inline void RD(T0 &x0, T1 &x1, T2 &x2){RD(x0), RD(x1), RD(x2);}
template<class T0, class T1, class T2, class T3> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3){RD(x0), RD(x1), RD(x2), RD(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RD(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){RD(x0), RD(x1), RD(x2), RD(x3), RD(x4), RD(x5), RD(x6);}
template<class T0, class T1> inline void OT(T0 &x0, T1 &x1){OT(x0), OT(x1);}
template<class T0, class T1, class T2> inline void OT(T0 &x0, T1 &x1, T2 &x2){OT(x0), OT(x1), OT(x2);}
template<class T0, class T1, class T2, class T3> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3){OT(x0), OT(x1), OT(x2), OT(x3);}
template<class T0, class T1, class T2, class T3, class T4> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void OT(T0 &x0, T1 &x1, T2 &x2, T3 &x3, T4 &x4, T5 &x5, T6 &x6){OT(x0), OT(x1), OT(x2), OT(x3), OT(x4), OT(x5), OT(x6);}

template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}
template<class T0, class T1> inline void RST(T0 &A0, T1 &A1){RST(A0), RST(A1);}
template<class T0, class T1, class T2> inline void RST(T0 &A0, T1 &A1, T2 &A2){RST(A0), RST(A1), RST(A2);}
template<class T0, class T1, class T2, class T3> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3){RST(A0), RST(A1), RST(A2), RST(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void RST(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){RST(A0), RST(A1), RST(A2), RST(A3), RST(A4), RST(A5), RST(A6);}


template<class T> inline void CLR(priority_queue<T, vector<T>, less<T> > &Q){
    while (!Q.empty()) Q.pop();
}

template<class T> inline void CLR(priority_queue<T, vector<T>, greater<T> > &Q){
    while (!Q.empty()) Q.pop();
}

template<class T> inline void CLR(T &A){A.clear();}
template<class T0, class T1> inline void CLR(T0 &A0, T1 &A1){CLR(A0), CLR(A1);}
template<class T0, class T1, class T2> inline void CLR(T0 &A0, T1 &A1, T2 &A2){CLR(A0), CLR(A1), CLR(A2);}
template<class T0, class T1, class T2, class T3> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3){CLR(A0), CLR(A1), CLR(A2), CLR(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void CLR(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){CLR(A0), CLR(A1), CLR(A2), CLR(A3), CLR(A4), CLR(A5), CLR(A6);}
template<class T> inline void CLR(T &A, int n){REP(i, n) CLR(A[i]);}
template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}
template<class T0, class T1> inline void FLC(T0 &A0, T1 &A1, int x){FLC(A0, x), FLC(A1, x);}
template<class T0, class T1, class T2> inline void FLC(T0 &A0, T1 &A1, T2 &A2){FLC(A0), FLC(A1), FLC(A2);}
template<class T0, class T1, class T2, class T3> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3){FLC(A0), FLC(A1), FLC(A2), FLC(A3);}
template<class T0, class T1, class T2, class T3, class T4> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4);}
template<class T0, class T1, class T2, class T3, class T4, class T5> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5);}
template<class T0, class T1, class T2, class T3, class T4, class T5, class T6> inline void FLC(T0 &A0, T1 &A1, T2 &A2, T3 &A3, T4 &A4, T5 &A5, T6 &A6){FLC(A0), FLC(A1), FLC(A2), FLC(A3), FLC(A4), FLC(A5), FLC(A6);}

template<class T> inline void SRT(T &A){sort(ALL(A));}
template<class T, class C> inline void SRT(T &A, C B){sort(ALL(A), B);}

/** Add - On **/

const int MOD = 1000000009;
const int INF = 10009;
const DB EPS = 1e-2;
const DB OO = 1e15;
const DB PI = 3.14159265358979323846264; //M_PI;

// <<= ` 0. Daily Use .,

template<class T> inline void checkMin(T &a,const T b){if (b<a) a=b;}
template<class T> inline void checkMax(T &a,const T b){if (b>a) a=b;}
template <class T, class C> inline void checkMin(T& a, const T b, C c){if (c(b,a)) a = b;}
template <class T, class C> inline void checkMax(T& a, const T b, C c){if (c(a,b)) a = b;}
template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}
template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}
template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}
template<class T> inline T max(T a, T b, T c, T d){return max(min(a, b), max(c, d));}
template<class T> inline T sqr(T a){return a*a;}
template<class T> inline T cub(T a){return a*a*a;}
int Ceil(int x, int y){return (x - 1) / y + 1;}

// <<= ` 1. Bitwise Operation .,
inline bool _1(int x, int i){return x & 1<<i;}
inline bool _1(LL x, int i){return x & 1LL<<i;}
inline LL _1(int i){return 1LL<<i;}
//inline int _1(int i){return 1<<i;}
inline LL _U(int i){return _1(i) - 1;};
//inline int _U(int i){return _1(i) - 1;};

inline int count_bits(int x){
    x = (x & 0x55555555) + ((x & 0xaaaaaaaa) >> 1);
    x = (x & 0x33333333) + ((x & 0xcccccccc) >> 2);
    x = (x & 0x0f0f0f0f) + ((x & 0xf0f0f0f0) >> 4);
    x = (x & 0x00ff00ff) + ((x & 0xff00ff00) >> 8);
    x = (x & 0x0000ffff) + ((x & 0xffff0000) >> 16);
    return x;
}

inline int count_bits(LL x){
    x = (x & 0x5555555555555555LL) + ((x & 0xaaaaaaaaaaaaaaaaLL) >> 1);
    x = (x & 0x3333333333333333LL) + ((x & 0xccccccccccccccccLL) >> 2);
    x = (x & 0x0f0f0f0f0f0f0f0fLL) + ((x & 0xf0f0f0f0f0f0f0f0LL) >> 4);
    x = (x & 0x00ff00ff00ff00ffLL) + ((x & 0xff00ff00ff00ff00LL) >> 8);
    x = (x & 0x0000ffff0000ffffLL) + ((x & 0xffff0000ffff0000LL) >> 16);
    x = (x & 0x00000000ffffffffLL) + ((x & 0xffffffff00000000LL) >> 32);
    return x;
}

int reverse_bits(int x){
    x = ((x >> 1) & 0x55555555) | ((x << 1) & 0xaaaaaaaa);
    x = ((x >> 2) & 0x33333333) | ((x << 2) & 0xcccccccc);
    x = ((x >> 4) & 0x0f0f0f0f) | ((x << 4) & 0xf0f0f0f0);
    x = ((x >> 8) & 0x00ff00ff) | ((x << 8) & 0xff00ff00);
    x = ((x >> 16) & 0x0000ffff) | ((x << 16) & 0xffff0000);
    return x;
}

// <<= ` 2. Modular Arithmetic Basic .,

inline void INC(int &a, int b){a += b; if (a >= MOD) a -= MOD;}
inline int sum(int a, int b){a += b; if (a >= MOD) a -= MOD; return a;}
inline void DEC(int &a, int b){a -= b; if (a < 0) a += MOD;}
inline int dff(int a, int b){a -= b; if (a < 0) a  += MOD; return a;}
inline void MUL(int &a, int b){a = (LL)a * b % MOD;}
inline int pdt(int a, int b){return (LL)a * b % MOD;}
inline int pdt(int a, int b, int c){return pdt(pdt(a, b), c);}

inline int pow(int a, int b){
    int c = 1;
    while (b) {
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

template<class T>
inline int pow(T a, int b){
    T c(1);
    while (b) {
        if (b&1) MUL(c, a);
        MUL(a, a), b >>= 1;
    }
    return c;
}

inline int _I(int b){
    int a = MOD, x1 = 0, x2 = 1, q;
    while (true){
        q = a / b, a %= b;
        if (!a) return (x2 + MOD) % MOD;
        DEC(x1, pdt(q, x2));

        q = b / a, b %= a;
        if (!b) return (x1 + MOD) % MOD;
        DEC(x2, pdt(q, x1));
    }
}

inline void DIV(int &a, int b){MUL(a, _I(b));}
inline int qtt(int a, int b){return pdt(a, _I(b));}

inline int sum(int a, int b, int MOD){
    a += b; if (a >= MOD) a -= MOD;
    return a;
}

inline int phi(int n){
    int res = n;
    for (int i=2;sqr(i)<=n;++i) if (!(n%i)){
        DEC(res, qtt(res, i));
        do{n /= i;} while(!(n%i));
    }
    if (n != 1)
        DEC(res, qtt(res, n));
    return res;
}

// <<= '9. Comutational Geometry .,

struct Po; struct Line; struct Seg;

inline int sgn(DB x){return x < -EPS ? -1 : x > EPS;}
inline int sgn(DB x, DB y){return sgn(x - y);}

struct Po{
    DB x, y;
    Po(DB _x = 0, DB _y = 0):x(_x), y(_y){}

    friend istream& operator >>(istream& in, Po &p){return in >> p.x >> p.y;}
    friend ostream& operator <<(ostream& out, Po p){return out << "(" << p.x << ", " << p.y << ")";}

    friend bool operator ==(Po, Po);
    friend bool operator !=(Po, Po);
    friend Po operator +(Po, Po);
    friend Po operator -(Po, Po);
    friend Po operator *(Po, DB);
    friend Po operator /(Po, DB);

    bool operator < (const Po &rhs) const{return sgn(x, rhs.x) < 0 || sgn(x, rhs.x) == 0 && sgn(y, rhs.y) < 0;}
    Po operator-() const{return Po(-x, -y);}
    Po& operator +=(Po rhs){x += rhs.x, y += rhs.y; return *this;}
    Po& operator -=(Po rhs){x -= rhs.x, y -= rhs.y; return *this;}
    Po& operator *=(DB k){x *= k, y *= k; return *this;}
    Po& operator /=(DB k){x /= k, y /= k; return *this;}

    DB length_sqr(){return sqr(x) + sqr(y);}
    DB length(){return sqrt(length_sqr());}

    DB atan(){
        return atan2(y, x);
    }

    void input(){
        scanf("%lf %lf", &x, &y);
    }
};

bool operator ==(Po a, Po b){return sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0;}
bool operator !=(Po a, Po b){return sgn(a.x - b.x) != 0 || sgn(a.y - b.y) != 0;}
Po operator +(Po a, Po b){return Po(a.x + b.x, a.y + b.y);}
Po operator -(Po a, Po b){return Po(a.x - b.x, a.y - b.y);}
Po operator *(Po a, DB k){return Po(a.x * k, a.y * k);}
Po operator *(DB k, Po a){return a * k;}
Po operator /(Po a, DB k){return Po(a.x / k, a.y / k);}

struct Line{
    Po a, b;
    Line(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
    Line(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
    Line(Seg);

    friend ostream& operator <<(ostream& out, Line p){return out << p.a << "-" << p.b;}
};

struct Seg{
    Po a, b;
    Seg(Po _a = Po(), Po _b = Po()):a(_a), b(_b){}
    Seg(DB x0, DB y0, DB x1, DB y1):a(Po(x0, y0)), b(Po(x1, y1)){}
    Seg(Line l);

    friend ostream& operator <<(ostream& out, Seg p){return out << p.a << "-" << p.b;}
    DB length(){return (b - a).length();}
};

Line::Line(Seg l):a(l.a), b(l.b){}
Seg::Seg(Line l):a(l.a), b(l.b){}

#define innerProduct dot
#define scalarProduct dot
#define dotProduct dot
#define outerProduct det
#define crossProduct det

inline DB dot(DB x1, DB y1, DB x2, DB y2){return x1 * x2 + y1 * y2;}
inline DB dot(Po a, Po b){return dot(a.x, a.y, b.x, b.y);}
inline DB dot(Po p0, Po p1, Po p2){return dot(p1 - p0, p2 - p0);}
inline DB dot(Line l1, Line l2){return dot(l1.b - l1.a, l2.b - l2.a);}
inline DB det(DB x1, DB y1, DB x2, DB y2){return x1 * y2 - x2 * y1;}
inline DB det(Po a, Po b){return det(a.x, a.y, b.x, b.y);}
inline DB det(Po p0, Po p1, Po p2){return det(p1 - p0, p2 - p0);}
inline DB det(Line l1, Line l2){return det(l1.b - l1.a, l2.b - l2.a);}

template<class T1, class T2> inline DB dist(T1 x, T2 y){return sqrt(dist_sqr(x, y));}

inline DB dist_sqr(Po a, Po b){return sqr(a.x - b.x) + sqr(a.y - b.y);}
inline DB dist_sqr(Po p, Line l){Po v0 = l.b - l.a, v1 = p - l.a; return sqr(fabs(det(v0, v1))) / v0.length_sqr();}
inline DB dist_sqr(Po p, Seg l){
    Po v0 = l.b - l.a, v1 = p - l.a, v2 = p - l.b;
    if (sgn(dot(v0, v1)) * sgn(dot(v0, v2)) <= 0) return dist_sqr(p, Line(l));
    else return min(v1.length_sqr(), v2.length_sqr());
}

inline DB dist_sqr(Line l, Po p){return dist_sqr(p, l);}
inline DB dist_sqr(Seg l, Po p){return dist_sqr(p, l);}

inline DB dist_sqr(Line l1, Line l2){
    if (sgn(det(l1, l2)) != 0) return 0;
    return dist_sqr(l1.a, l2);
}
inline DB dist_sqr(Line l1, Seg l2){
    Po v0 = l1.b - l1.a, v1 = l2.a - l1.a, v2 = l2.b - l1.a; DB c1 = det(v0, v1), c2 = det(v0, v2);
    return sgn(c1) != sgn(c2) ? 0 : sqr(min(fabs(c1), fabs(c2))) / v0.length_sqr();
}

bool isIntersect(Seg l1, Seg l2){

    //if (l1.a == l2.a || l1.a == l2.b || l1.b == l2.a || l1.b == l2.b) return true;

    return
        min(l1.a.x, l1.b.x) <= max(l2.a.x, l2.b.x) &&
        min(l2.a.x, l2.b.x) <= max(l1.a.x, l1.b.x) &&
        min(l1.a.y, l1.b.y) <= max(l2.a.y, l2.b.y) &&
        min(l2.a.y, l2.b.y) <= max(l1.a.y, l1.b.y) &&
    sgn( det(l1.a, l2.a, l2.b) ) * sgn( det(l1.b, l2.a, l2.b) ) <= 0 &&
    sgn( det(l2.a, l1.a, l1.b) ) * sgn( det(l2.b, l1.a, l1.b) ) <= 0;

}

inline DB dist_sqr(Seg l1, Seg l2){
    if (isIntersect(l1, l2)) return 0;
    else return min(dist_sqr(l1.a, l2), dist_sqr(l1.b, l2), dist_sqr(l2.a, l1), dist_sqr(l2.b, l1));
}

inline bool isOnExtremePoint(const Po &p, const Seg &l){
    return p == l.a || p == l.b;
}

inline bool isOnseg(const Po &p, const Seg &l){

    //if (p == l.a || p == l.b) return false;

    return sgn(det(p, l.a, l.b)) == 0 &&
        sgn(l.a.x, p.x) * sgn(l.b.x, p.x) <= 0 && sgn(l.a.y, p.y) * sgn(l.b.y, p.y) <= 0;
}

inline Po intersect(const Line &l1, const Line &l2){
    return l1.a + (l1.b - l1.a) * (det(l2.a, l1.a, l2.b) / det(l2, l1));
}

// perpendicular foot
inline Po intersect(const Po & p, const Line &l){
    return intersect(Line(p, p + Po(l.a.y - l.b.y, l.b.x - l.a.x)), l);
}

inline Po rotate(Po p, DB alpha, Po o = Po()){
    p.x -= o.x, p.y -= o .y;
    return Po(p.x * cos(alpha) - p.y * sin(alpha), p.y * cos(alpha) + p.x * sin(alpha)) + o;
}

// <<= ' A. Random Event ..

inline int rand32(){return (bool(rand() & 1) << 30) | (rand() << 15) + rand();}
inline int random32(int l, int r){return rand32() % (r - l + 1) + l;}
inline int random(int l, int r){return rand() % (r - l + 1) + l;}
int dice(){return rand() % 6;}
bool coin(){return rand() % 2;}

// <<= ' 0. I/O Accelerator interface .,

template<class T> inline void RD(T &x){
    //cin >> x;
    scanf("%d", &x);
    //char c; for (c = getchar(); c < '0'; c = getchar()); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
    //char c; c = getchar(); x = c - '0'; for (c = getchar(); c >= '0'; c = getchar()) x = x * 10 + c - '0';
}

template<class T> inline void OT(const T &p){
    printf("%.0lf\n", p);
}

/* .................................................................................................................................. */

const int N = 1009;

VI P; bool vis[N]; int cnt[N];
int F[N], C[N][N], n;

int dfs(int u){
    if (vis[u]) return 0; vis[u] = true;
    return 1 + dfs(P[u]);
}

int f(int a, int b){
    return a == 4 ? cub(b)*6 : b;
}

class FleaCircus {
public:
	int countArrangements(vector <string> S) {

	    REP(i, N){C[i][0] = 1; FOR_1(j, 1, min(i, 4)) C[i][j] = sum(C[i-1][j-1], C[i-1][j]);}
	    F[0] = 1; FOR(i, 1, N) F[i] = pdt(F[i-1], i);

        string s; REP(i, SZ(S)) s += S[i]; istringstream iss(s);
        CLR(P); int t; while (iss >> t) P.PB(t); n = SZ(P);
        RST(vis, cnt); REP(i, n) if (!vis[i]) ++cnt[dfs(i)];

        REP_1(i, n) if (!(i&1) && (cnt[i]%4)) return 0;

        int res = 1, tmp, r1, n1, r2, n2; REP_1(i, n) if (cnt[i]){
            if (i&1){
                tmp = 0, r1 = 1, n1 = cnt[i]; FOR_1_C(j, 0, n1/4){
                    r2 = r1, n2 = n1; FOR_1_C(k, 0, n2/2){
                        INC(tmp, qtt(r2, pdt(F[j], F[k]))), MUL(r2, C[n2][2]), MUL(r2, f(2, i)), n2 -= 2;
                    }
                    MUL(r1, C[n1][4]), MUL(r1, f(4, i)), n1 -= 4;
                }
            }
            else {
                tmp = _I(F[(n1=cnt[i])/4]);
                while (n1) MUL(tmp, C[n1][4]), MUL(tmp, f(4, i)), n1 -= 4;

            }
            MUL(res, tmp);
        }

		return res;
	}
};

// 8


// BEGIN CUT HERE
namespace moj_harness {
	int run_test_case(int);
	void run_test(int casenum = -1, bool quiet = false) {
		if (casenum != -1) {
			if (run_test_case(casenum) == -1 && !quiet) {
				cerr << "Illegal input! Test case " << casenum << " does not exist." << endl;
			}
			return;
		}

		int correct = 0, total = 0;
		for (int i=0;; ++i) {
			int x = run_test_case(i);
			if (x == -1) {
				if (i >= 100) break;
				continue;
			}
			correct += x;
			++total;
		}

		if (total == 0) {
			cerr << "No test cases run." << endl;
		} else if (correct < total) {
			cerr << "Some cases FAILED (passed " << correct << " of " << total << ")." << endl;
		} else {
			cerr << "All " << total << " tests passed!" << endl;
		}
	}

	int verify_case(int casenum, const int &expected, const int &received, clock_t elapsed) {
		cerr << "Example " << casenum << "... ";

		string verdict;
		vector<string> info;
		char buf[100];

		if (elapsed > CLOCKS_PER_SEC / 200) {
			sprintf(buf, "time %.2fs", elapsed * (1.0/CLOCKS_PER_SEC));
			info.push_back(buf);
		}

		if (expected == received) {
			verdict = "PASSED";
		} else {
			verdict = "FAILED";
		}

		cerr << verdict;
		if (!info.empty()) {
			cerr << " (";
			for (int i=0; i<(int)info.size(); ++i) {
				if (i > 0) cerr << ", ";
				cerr << info[i];
			}
			cerr << ")";
		}
		cerr << endl;

		if (verdict == "FAILED") {
			cerr << "    Expected: " << expected << endl;
			cerr << "    Received: " << received << endl;
		}

		return verdict == "PASSED";
	}

	int run_test_case(int casenum) {
		switch (casenum) {
		case 0: {
			string afterFourClicks[]  = {"0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 ", "20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36", " 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 5", "3 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 ", "70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86", " 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 10", "2 103 104 105 106 107 108 109 110 111 112 113 114 ", "115 116 117 118 119 120 121 122 123 124 125 126 12", "7 128 129 130 131 132 133 134 135 136 137 138 139 ", "140 141 142 143 144 145 146 147 148 149 150 151 15", "2 153 154 155 156 157 158 159 160 161 162 163 164 ", "165 166 167 168 169 170 171 172 173 174 175 176 17", "7 178 179 180 181 182 183 184 185 186 187 188 189 ", "190 191 192 193 194 195 196 197 198 199 200 201 20", "2 203 204 205 206 207 208 209 210 211 212 213 214 ", "215 216 217 218 219 220 221 222 223 224 225 226 22", "7 228 229 230 231 232 233 234 235 236 237 238 239 ", "240 241 242 243 244 245 246 247 248 249 250 251 25", "2 253 254 255 256 257 258 259 260 261 262 263 264 ", "265 266 267 268 269 270 271 272 273 274 275 276 27", "7 278 279 280 281 282 283 284 285 286 287 288 289 ", "290 291 292 293 294 295 296 297 298 299 300 301 30", "2 303 304 305 306 307 308 309 310 311 312 313 314 ", "315 316 317 318 319 320 321 322 323 324 325 326 32", "7 328 329 330 331 332 333 334 335 336 337 338 339 ", "340 341 342 343 344 345 346 347 348 349 350 351 35", "2 353 354 355 356 357 358 359 360 361 362 363 364 ", "365 366 367 368 369 370 371 372 373 374 375 376 37", "7 378 379 380 381 382 383 384 385 386 387 388 389 ", "390 391 392 393 394 395 396 397 398 399 400 401 40", "2 403 404 405 406 407 408 409 410 411 412 413 414 ", "415 416 417 418 419 420 421 422 423 424 425 426 42", "7 428 429 430 431 432 433 434 435 436 437 438 439 ", "440 441 442 443 444 445 446 447 448 449 450 451 45", "2 453 454 455 456 457 458 459 460 461 462 463 464 ", "465 466 467 468 469 470 471 472 473 474 475 476 47", "7 478 479 480 481 482 483 484 485 486 487 488 489 ", "490 491 492 493 494 495 496 497 498 499 500 501 50", "2 503 504 505 506 507 508 509 510 511 512 513 514 ", "515 516 517 518 519 520 521 522 523 524 525 526 52", "7 528 529 530 531 532 533 534 535 536 537 538 539 ", "540 541 542 543 544 545 546 547 548 549 550 551 55", "2 553 554 555 556 557 558 559 560 561 562 563 564 ", "565 566 567 568 569 570 571 572 573 574 575 576 57", "7 578 579 580 581 582 583 584 585 586 587 588 589 ", "590 591 592 593 594 595 596 597 598 599 600 601 60", "2 603 604 605 606 607 608 609 610 611 612 613 614 ", "615 616 617 618 619 620 621 622 623 624 625 626 62", "7 628 629 630 631 632 633 634 635 636 637 638 639 ", "640 641 642 643 644 645 646 647 648 649 650 651"};
			int expected__            = 92639029;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 1: {
			string afterFourClicks[]  = {"1 2 ", "0 3"};
			int expected__            = 1;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 2: {
			string afterFourClicks[]  = {"0 1 2"};
			int expected__            = 4;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 3: {
			string afterFourClicks[]  = {"0 1 2 3 5 4"};
			int expected__            = 0;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 4: {
			string afterFourClicks[]  = {"3 6 7 9 8 2 1", "0 5 1 0 4"};
			int expected__            = 4;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}
		case 5: {
			string afterFourClicks[]  = {"1 0 7 5 6 3 4 2"};
			int expected__            = 48;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}

		// custom cases

/*      case 6: {
			string afterFourClicks[]  = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 7: {
			string afterFourClicks[]  = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
/*      case 8: {
			string afterFourClicks[]  = ;
			int expected__            = ;

			clock_t start__           = clock();
			int received__            = FleaCircus().countArrangements(vector <string>(afterFourClicks, afterFourClicks + (sizeof afterFourClicks / sizeof afterFourClicks[0])));
			return verify_case(casenum, expected__, received__, clock()-start__);
		}*/
		default:
			return -1;
		}
	}
}

int main(int argc, char *argv[]) {
	if (argc == 1) {
		moj_harness::run_test();
	} else {
		for (int i=1; i<argc; ++i)
			moj_harness::run_test(atoi(argv[i]));
	}
}
// END CUT HERE

External link:

http://community.topcoder.com/stat?c=coder_room_stats&rd=14738&cr=22727863