ddf2358d33 2012-08-08 kinaba: #include <iostream> ddf2358d33 2012-08-08 kinaba: #include <sstream> ddf2358d33 2012-08-08 kinaba: #include <iomanip> ddf2358d33 2012-08-08 kinaba: #include <vector> ddf2358d33 2012-08-08 kinaba: #include <string> ddf2358d33 2012-08-08 kinaba: #include <map> ddf2358d33 2012-08-08 kinaba: #include <set> ddf2358d33 2012-08-08 kinaba: #include <algorithm> ddf2358d33 2012-08-08 kinaba: #include <numeric> ddf2358d33 2012-08-08 kinaba: #include <iterator> ddf2358d33 2012-08-08 kinaba: #include <functional> ddf2358d33 2012-08-08 kinaba: #include <complex> ddf2358d33 2012-08-08 kinaba: #include <queue> ddf2358d33 2012-08-08 kinaba: #include <stack> ddf2358d33 2012-08-08 kinaba: #include <cmath> ddf2358d33 2012-08-08 kinaba: #include <cassert> ddf2358d33 2012-08-08 kinaba: using namespace std; ddf2358d33 2012-08-08 kinaba: typedef long long LL; ddf2358d33 2012-08-08 kinaba: typedef long double LD; ddf2358d33 2012-08-08 kinaba: typedef complex<LD> CMP; ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: static const int MODVAL = 1000000007; ddf2358d33 2012-08-08 kinaba: struct mint ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: int val; ddf2358d33 2012-08-08 kinaba: mint():val(0){} ddf2358d33 2012-08-08 kinaba: mint(int x):val(x%MODVAL) {} ddf2358d33 2012-08-08 kinaba: mint(size_t x):val(x%MODVAL) {} ddf2358d33 2012-08-08 kinaba: mint(LL x):val(x%MODVAL) {} ddf2358d33 2012-08-08 kinaba: }; ddf2358d33 2012-08-08 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } ddf2358d33 2012-08-08 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } ddf2358d33 2012-08-08 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } ddf2358d33 2012-08-08 kinaba: mint operator+(mint x, mint y) { return x+=y; } ddf2358d33 2012-08-08 kinaba: mint operator-(mint x, mint y) { return x-=y; } ddf2358d33 2012-08-08 kinaba: mint operator*(mint x, mint y) { return x*=y; } ddf2358d33 2012-08-08 kinaba: vector< vector<mint> > CP_; ddf2358d33 2012-08-08 kinaba: mint C(LL n, LL k) { ddf2358d33 2012-08-08 kinaba: while( CP_.size() <= n ) { ddf2358d33 2012-08-08 kinaba: int nn = CP_.size(); ddf2358d33 2012-08-08 kinaba: CP_.push_back(vector<mint>(nn+1,1)); ddf2358d33 2012-08-08 kinaba: for(int kk=1; kk<nn; ++kk) ddf2358d33 2012-08-08 kinaba: CP_[nn][kk] = CP_[nn-1][kk-1] + CP_[nn-1][kk]; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: return k<0 || n<k ? 0 : CP_[n][k]; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: class KingdomAndCities { public: ddf2358d33 2012-08-08 kinaba: int howMany(int N, int M, int K) ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: if( M == 0 ) return zero(N, K).val; ddf2358d33 2012-08-08 kinaba: if( M == 1 ) return one(N, K).val; ddf2358d33 2012-08-08 kinaba: return two(N, K).val; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: mint zero_may_be_disconnected(int N, int K) ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: return C(N*(N-1)/2, K); ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: map<pair<int,int>, mint> memo; ddf2358d33 2012-08-08 kinaba: mint zero(int N, int K) ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: pair<int,int> key(N, K); ddf2358d33 2012-08-08 kinaba: if(memo.count(key)) ddf2358d33 2012-08-08 kinaba: return memo[key]; ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: mint sum = zero_may_be_disconnected(N, K); ddf2358d33 2012-08-08 kinaba: for(int NA=1; NA<=N-1; ++NA) ddf2358d33 2012-08-08 kinaba: for(int KA=0; KA<=K; ++KA) ddf2358d33 2012-08-08 kinaba: sum -= C(N-1,NA-1)*zero(NA,KA)*zero_may_be_disconnected(N-NA, K-KA); ddf2358d33 2012-08-08 kinaba: return memo[key] = sum; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: mint one(int N, int K) ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: mint sum = zero(N-1,K-2)*C(N-1,2); ddf2358d33 2012-08-08 kinaba: for(int NA=1; NA<=N-2; ++NA) ddf2358d33 2012-08-08 kinaba: for(int KA=0; KA<=K-2; ++KA) ddf2358d33 2012-08-08 kinaba: sum += C(N-2,NA-1)*zero(NA,KA)*NA*(N-1-NA)*zero(N-1-NA,K-2-KA); ddf2358d33 2012-08-08 kinaba: return sum; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: mint two(int N, int K) ddf2358d33 2012-08-08 kinaba: { ddf2358d33 2012-08-08 kinaba: mint sum = one(N-1,K-2)*C(N-2,2); ddf2358d33 2012-08-08 kinaba: for(int NA=1; NA<=N-2; ++NA) ddf2358d33 2012-08-08 kinaba: for(int KA=0; KA<=K-2; ++KA) ddf2358d33 2012-08-08 kinaba: sum += C(N-2,NA-1)*one(NA,KA)*(NA-1)*(N-1-NA)*zero(N-1-NA,K-2-KA); ddf2358d33 2012-08-08 kinaba: --N, --K; ddf2358d33 2012-08-08 kinaba: sum += zero(N-1,K-2)*(N-1)*(N-1); ddf2358d33 2012-08-08 kinaba: for(int NA=1; NA<=N-2; ++NA) ddf2358d33 2012-08-08 kinaba: for(int KA=0; KA<=K-2; ++KA) ddf2358d33 2012-08-08 kinaba: sum += C(N-1,NA)*zero(NA,KA)*NA*(N-1-NA)*zero(N-1-NA,K-2-KA); ddf2358d33 2012-08-08 kinaba: return sum; ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: }; ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: // BEGIN CUT HERE ddf2358d33 2012-08-08 kinaba: #include <ctime> ddf2358d33 2012-08-08 kinaba: double start_time; string timer() ddf2358d33 2012-08-08 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } ddf2358d33 2012-08-08 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) ddf2358d33 2012-08-08 kinaba: { os << "{ "; ddf2358d33 2012-08-08 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) ddf2358d33 2012-08-08 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } ddf2358d33 2012-08-08 kinaba: void verify_case(const int& Expected, const int& Received) { ddf2358d33 2012-08-08 kinaba: bool ok = (Expected == Received); ddf2358d33 2012-08-08 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; ddf2358d33 2012-08-08 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } ddf2358d33 2012-08-08 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); ddf2358d33 2012-08-08 kinaba: #define END verify_case(_, KingdomAndCities().howMany(N, M, K));} ddf2358d33 2012-08-08 kinaba: int main(){ ddf2358d33 2012-08-08 kinaba: ddf2358d33 2012-08-08 kinaba: CASE(0) ddf2358d33 2012-08-08 kinaba: int N = 3; ddf2358d33 2012-08-08 kinaba: int M = 0; ddf2358d33 2012-08-08 kinaba: int K = 3; ddf2358d33 2012-08-08 kinaba: int _ = 1; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: CASE(1) ddf2358d33 2012-08-08 kinaba: int N = 4; ddf2358d33 2012-08-08 kinaba: int M = 1; ddf2358d33 2012-08-08 kinaba: int K = 4; ddf2358d33 2012-08-08 kinaba: int _ = 9; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: CASE(2) ddf2358d33 2012-08-08 kinaba: int N = 5; ddf2358d33 2012-08-08 kinaba: int M = 2; ddf2358d33 2012-08-08 kinaba: int K = 11; ddf2358d33 2012-08-08 kinaba: int _ = 0; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: CASE(3) ddf2358d33 2012-08-08 kinaba: int N = 5; ddf2358d33 2012-08-08 kinaba: int M = 0; ddf2358d33 2012-08-08 kinaba: int K = 8; ddf2358d33 2012-08-08 kinaba: int _ = 45; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: CASE(4) ddf2358d33 2012-08-08 kinaba: int N = 10; ddf2358d33 2012-08-08 kinaba: int M = 2; ddf2358d33 2012-08-08 kinaba: int K = 20; ddf2358d33 2012-08-08 kinaba: int _ = 150810825; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: CASE(5) ddf2358d33 2012-08-08 kinaba: int N = 3; ddf2358d33 2012-08-08 kinaba: int M = 2; ddf2358d33 2012-08-08 kinaba: int K = 3; ddf2358d33 2012-08-08 kinaba: int _ = 1; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: /* ddf2358d33 2012-08-08 kinaba: CASE(6) ddf2358d33 2012-08-08 kinaba: int N = ; ddf2358d33 2012-08-08 kinaba: int M = ; ddf2358d33 2012-08-08 kinaba: int K = ; ddf2358d33 2012-08-08 kinaba: int _ = ; ddf2358d33 2012-08-08 kinaba: END ddf2358d33 2012-08-08 kinaba: */ ddf2358d33 2012-08-08 kinaba: } ddf2358d33 2012-08-08 kinaba: // END CUT HERE