Artifact Content
Not logged in

Artifact 7fa2189be95e994e72cf7090925ac133b9f5ed22


#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 = 1000000007;
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 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(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 KingdomAndCities { public:
	int howMany(int N, int M, int K)
	{
		if( M == 0 ) return zero(N, K).val;
		if( M == 1 ) return one(N, K).val;
		return two(N, K).val;
	}

	mint zero_may_be_disconnected(int N, int K)
	{
		return C(N*(N-1)/2, K);
	}

	map<pair<int,int>, mint> memo;
	mint zero(int N, int K)
	{
		pair<int,int> key(N, K);
		if(memo.count(key))
			return memo[key];

		mint sum = zero_may_be_disconnected(N, K);
		for(int NA=1; NA<=N-1; ++NA)
		for(int KA=0; KA<=K; ++KA)
			sum -= C(N-1,NA-1)*zero(NA,KA)*zero_may_be_disconnected(N-NA, K-KA);
		return memo[key] = sum;
	}

	mint one(int N, int K)
	{
		mint sum = zero(N-1,K-2)*C(N-1,2);
		for(int NA=1; NA<=N-2; ++NA)
		for(int KA=0; KA<=K-2; ++KA)
			sum += C(N-2,NA-1)*zero(NA,KA)*NA*(N-1-NA)*zero(N-1-NA,K-2-KA);
		return sum;
	}

	mint two(int N, int K)
	{
		mint sum = one(N-1,K-2)*C(N-2,2);
		for(int NA=1; NA<=N-2; ++NA)
		for(int KA=0; KA<=K-2; ++KA)
			sum += C(N-2,NA-1)*one(NA,KA)*(NA-1)*(N-1-NA)*zero(N-1-NA,K-2-KA);
		--N, --K;
		sum += zero(N-1,K-2)*(N-1)*(N-1);
		for(int NA=1; NA<=N-2; ++NA)
		for(int KA=0; KA<=K-2; ++KA)
			sum += C(N-1,NA)*zero(NA,KA)*NA*(N-1-NA)*zero(N-1-NA,K-2-KA);
		return sum;
	}
};

// 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(_, KingdomAndCities().howMany(N, M, K));}
int main(){

CASE(0)
	int N = 3; 
	int M = 0; 
	int K = 3; 
	int _ = 1; 
END
CASE(1)
	int N = 4; 
	int M = 1; 
	int K = 4; 
	int _ = 9; 
END
CASE(2)
	int N = 5; 
	int M = 2; 
	int K = 11; 
	int _ = 0; 
END
CASE(3)
	int N = 5; 
	int M = 0; 
	int K = 8; 
	int _ = 45; 
END
CASE(4)
	int N = 10; 
	int M = 2; 
	int K = 20; 
	int _ = 150810825; 
END
CASE(5)
	int N = 3; 
	int M = 2; 
	int K = 3; 
	int _ = 1; 
END
/*
CASE(6)
	int N = ; 
	int M = ; 
	int K = ; 
	int _ = ; 
END
*/
}
// END CUT HERE