a31002ea47 2014-01-30 kinaba: #include <iostream> a31002ea47 2014-01-30 kinaba: #include <sstream> a31002ea47 2014-01-30 kinaba: #include <iomanip> a31002ea47 2014-01-30 kinaba: #include <vector> a31002ea47 2014-01-30 kinaba: using namespace std; a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: static const unsigned MODVAL = 1000000007; a31002ea47 2014-01-30 kinaba: struct mint a31002ea47 2014-01-30 kinaba: { a31002ea47 2014-01-30 kinaba: unsigned val; a31002ea47 2014-01-30 kinaba: mint():val(0){} a31002ea47 2014-01-30 kinaba: mint(int x):val(x%MODVAL) {} a31002ea47 2014-01-30 kinaba: mint(unsigned x):val(x%MODVAL) {} a31002ea47 2014-01-30 kinaba: }; a31002ea47 2014-01-30 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: class AlienAndSetDiv1 { public: a31002ea47 2014-01-30 kinaba: int getNumber(int N, int K) a31002ea47 2014-01-30 kinaba: { a31002ea47 2014-01-30 kinaba: memo.assign((1<<K)*(N+1)*(N+1), -1); a31002ea47 2014-01-30 kinaba: vector<int> A, B; a31002ea47 2014-01-30 kinaba: return rec(N, K, A, B, 0); a31002ea47 2014-01-30 kinaba: } a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: vector<int> memo; a31002ea47 2014-01-30 kinaba: int rec(size_t N, int K, vector<int>& A, vector<int>& B, int lastk) a31002ea47 2014-01-30 kinaba: { a31002ea47 2014-01-30 kinaba: if(A.size()==N && B.size()==N) a31002ea47 2014-01-30 kinaba: return 1; a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: auto key = (lastk*(N+1)+A.size())*(N+1)+B.size(); a31002ea47 2014-01-30 kinaba: if(memo[key] >= 0) a31002ea47 2014-01-30 kinaba: return memo[key]; a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: int next = A.size()+B.size()+1, nextmask = (lastk &~ (1<<(K-1)))<<1; a31002ea47 2014-01-30 kinaba: int ai = A.size(), bi = B.size(); a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: mint total = 0; a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: A.push_back(next); a31002ea47 2014-01-30 kinaba: if(A.size()<=N && (ai>=B.size() || abs(A[ai]-B[ai])>=K)) a31002ea47 2014-01-30 kinaba: total += rec(N, K, A, B, nextmask); a31002ea47 2014-01-30 kinaba: A.pop_back(); a31002ea47 2014-01-30 kinaba: B.push_back(next); a31002ea47 2014-01-30 kinaba: if(B.size()<=N && (bi>=A.size() || abs(A[bi]-B[bi])>=K)) a31002ea47 2014-01-30 kinaba: total += rec(N, K, A, B, nextmask|1); a31002ea47 2014-01-30 kinaba: B.pop_back(); a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: return memo[key] = total.val; a31002ea47 2014-01-30 kinaba: } a31002ea47 2014-01-30 kinaba: }; a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: // BEGIN CUT HERE a31002ea47 2014-01-30 kinaba: #include <ctime> a31002ea47 2014-01-30 kinaba: double start_time; string timer() a31002ea47 2014-01-30 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } a31002ea47 2014-01-30 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) a31002ea47 2014-01-30 kinaba: { os << "{ "; a31002ea47 2014-01-30 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) a31002ea47 2014-01-30 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } a31002ea47 2014-01-30 kinaba: void verify_case(const int& Expected, const int& Received) { a31002ea47 2014-01-30 kinaba: bool ok = (Expected == Received); a31002ea47 2014-01-30 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; a31002ea47 2014-01-30 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } a31002ea47 2014-01-30 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); a31002ea47 2014-01-30 kinaba: #define END verify_case(_, AlienAndSetDiv1().getNumber(N, K));} a31002ea47 2014-01-30 kinaba: int main(){ a31002ea47 2014-01-30 kinaba: a31002ea47 2014-01-30 kinaba: CASE(0) a31002ea47 2014-01-30 kinaba: int N = 2; a31002ea47 2014-01-30 kinaba: int K = 2; a31002ea47 2014-01-30 kinaba: int _ = 2; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(1) a31002ea47 2014-01-30 kinaba: int N = 3; a31002ea47 2014-01-30 kinaba: int K = 1; a31002ea47 2014-01-30 kinaba: int _ = 20; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(2) a31002ea47 2014-01-30 kinaba: int N = 4; a31002ea47 2014-01-30 kinaba: int K = 2; a31002ea47 2014-01-30 kinaba: int _ = 14; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(3) a31002ea47 2014-01-30 kinaba: int N = 10; a31002ea47 2014-01-30 kinaba: int K = 7; a31002ea47 2014-01-30 kinaba: int _ = 40; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(4) a31002ea47 2014-01-30 kinaba: int N = 50; a31002ea47 2014-01-30 kinaba: int K = 10; a31002ea47 2014-01-30 kinaba: int _ = -1; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(5) a31002ea47 2014-01-30 kinaba: int N = 50; a31002ea47 2014-01-30 kinaba: int K = 1; a31002ea47 2014-01-30 kinaba: int _ = -1; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(5) a31002ea47 2014-01-30 kinaba: int N = 50; a31002ea47 2014-01-30 kinaba: int K = 2; a31002ea47 2014-01-30 kinaba: int _ = -1; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(5) a31002ea47 2014-01-30 kinaba: int N = 50; a31002ea47 2014-01-30 kinaba: int K = 4; a31002ea47 2014-01-30 kinaba: int _ = -1; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: CASE(5) a31002ea47 2014-01-30 kinaba: int N = 2; a31002ea47 2014-01-30 kinaba: int K = 2; a31002ea47 2014-01-30 kinaba: int _ = 2; a31002ea47 2014-01-30 kinaba: END a31002ea47 2014-01-30 kinaba: } a31002ea47 2014-01-30 kinaba: // END CUT HERE