Artifact Content
Not logged in

Artifact 527ef29abc31b65231b9849c635227471eb24494


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

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned 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 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< vector<mint> > CP_;
mint C(int n, int 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 SumOverPermutations { public:
	int N;
	vector<int> memoOO, memoCO, memoCC;

	int findSum(int n)
	{
		N = n;
		memoOO.assign(N+1, -1);
		memoCO.assign(N+1, -1);
		memoCC.assign(N+1, -1);
		return recOO(n);
	}

	int recOO(int k) {
		if(k == 0) return 1;
		if(k == 1) return N;
		if(memoOO[k] != -1) return memoOO[k];

		mint total = 0;
		total += mint(N) * recCO(k-1); // left
		total += mint(N) * recCO(k-1); // right
		for(int i=1; i+1<k; ++i) // mid
			total += mint(N) * recCO(i) * recCO(k-1-i) * C(k-1, i);
		return memoOO[k] = total.val;
	}

	int recCO(int k) {
		if(k == 0) return 1;
		if(k == 1) return N-1;
		if(memoCO[k] != -1) return memoCO[k];

		mint total = 0;
		total += mint(N-1) * recCO(k-1); // left
		total += mint(N) * recCC(k-1); // right
		for(int i=1; i+1<k; ++i) // mid
			total += mint(N) * recCC(i) * recCO(k-1-i) * C(k-1, i);
		return memoCO[k] = total.val;
	}

	mint recCC(int k) {
		if(k == 0) return 1;
		if(k == 1) return N-2;
		if(memoCC[k] != -1) return memoCC[k];

		mint total = 0;
		total += mint(N-1) * recCC(k-1); // left
		total += mint(N-1) * recCC(k-1); // right
		for(int i=1; i+1<k; ++i) // mid
			total += mint(N) * recCC(i) * recCC(k-1-i) * C(k-1, i);
		return memoCC[k] = total.val;
	}
};

// 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(_, SumOverPermutations().findSum(n));}
int main(){

CASE(0)
	int n = 2; 
	int _ = 4; 
END
CASE(1)
	int n = 3; 
	int _ = 66; 
END
CASE(2)
	int n = 10; 
	int _ = 58310114; 
END
CASE(3)
	int n = 3900; 
	int _ = 940560814; 
END
/*
CASE(4)
	int n = ; 
	int _ = ; 
END
CASE(5)
	int n = ; 
	int _ = ; 
END
*/
}
// END CUT HERE