892764d484 2014-09-17 kinaba: #include <iostream> 892764d484 2014-09-17 kinaba: #include <sstream> 892764d484 2014-09-17 kinaba: #include <iomanip> 892764d484 2014-09-17 kinaba: #include <vector> 892764d484 2014-09-17 kinaba: #include <string> 892764d484 2014-09-17 kinaba: #include <map> 892764d484 2014-09-17 kinaba: #include <set> 892764d484 2014-09-17 kinaba: #include <algorithm> 892764d484 2014-09-17 kinaba: #include <numeric> 892764d484 2014-09-17 kinaba: #include <iterator> 892764d484 2014-09-17 kinaba: #include <functional> 892764d484 2014-09-17 kinaba: #include <complex> 892764d484 2014-09-17 kinaba: #include <queue> 892764d484 2014-09-17 kinaba: #include <stack> 892764d484 2014-09-17 kinaba: #include <cmath> 892764d484 2014-09-17 kinaba: #include <cassert> 892764d484 2014-09-17 kinaba: #include <tuple> 892764d484 2014-09-17 kinaba: using namespace std; 892764d484 2014-09-17 kinaba: typedef long long LL; 892764d484 2014-09-17 kinaba: typedef complex<double> CMP; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: unsigned MODVAL = 1000000007; 892764d484 2014-09-17 kinaba: struct mint 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: unsigned val; 892764d484 2014-09-17 kinaba: mint():val(0){} 892764d484 2014-09-17 kinaba: mint(int x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: mint(unsigned x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: mint(LL x):val(x%MODVAL) {} 892764d484 2014-09-17 kinaba: }; 892764d484 2014-09-17 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 892764d484 2014-09-17 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 892764d484 2014-09-17 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 892764d484 2014-09-17 kinaba: mint operator+(mint x, mint y) { return x+=y; } 892764d484 2014-09-17 kinaba: mint operator-(mint x, mint y) { return x-=y; } 892764d484 2014-09-17 kinaba: mint operator*(mint x, mint y) { return x*=y; } 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: class PatternLock { public: 892764d484 2014-09-17 kinaba: int solve(int N, int MOD) 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: MODVAL = MOD; 892764d484 2014-09-17 kinaba: memo.resize(N+1, vector<int>(N+1, -1)); 892764d484 2014-09-17 kinaba: return (rec(N,N)*2) % MOD; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: vector<vector<int>> memo; 892764d484 2014-09-17 kinaba: int rec(int A, int B) 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: if(memo[A][B] != -1) 892764d484 2014-09-17 kinaba: return memo[A][B]; 892764d484 2014-09-17 kinaba: mint total; 892764d484 2014-09-17 kinaba: for(int a=0; a<A; ++a) 892764d484 2014-09-17 kinaba: total += rec(A, B, a); 892764d484 2014-09-17 kinaba: return memo[A][B] = total.val; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: int rec(int A, int B, int a) 892764d484 2014-09-17 kinaba: { 892764d484 2014-09-17 kinaba: if(A==1 && B==0) 892764d484 2014-09-17 kinaba: return 1; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: mint total; 892764d484 2014-09-17 kinaba: if(B) total += rec(B, A-1); 892764d484 2014-09-17 kinaba: if(a>0) total += rec(A-1, B, a-1); 892764d484 2014-09-17 kinaba: if(a!=A-1) total += rec(A-1, B, a); 892764d484 2014-09-17 kinaba: return total.val; 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: }; 892764d484 2014-09-17 kinaba: 892764d484 2014-09-17 kinaba: // BEGIN CUT HERE 892764d484 2014-09-17 kinaba: #include <ctime> 892764d484 2014-09-17 kinaba: double start_time; string timer() 892764d484 2014-09-17 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 892764d484 2014-09-17 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 892764d484 2014-09-17 kinaba: { os << "{ "; 892764d484 2014-09-17 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 892764d484 2014-09-17 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 892764d484 2014-09-17 kinaba: void verify_case(const int& Expected, const int& Received) { 892764d484 2014-09-17 kinaba: bool ok = (Expected == Received); 892764d484 2014-09-17 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 892764d484 2014-09-17 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 892764d484 2014-09-17 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 892764d484 2014-09-17 kinaba: #define END verify_case(_, PatternLock().solve(N, MOD));} 892764d484 2014-09-17 kinaba: int main(){ 892764d484 2014-09-17 kinaba: /* 892764d484 2014-09-17 kinaba: CASE(0) 892764d484 2014-09-17 kinaba: int N = 1; 892764d484 2014-09-17 kinaba: int MOD = 12345667; 892764d484 2014-09-17 kinaba: int _ = 2; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: */ 892764d484 2014-09-17 kinaba: CASE(1) 892764d484 2014-09-17 kinaba: int N = 2; 892764d484 2014-09-17 kinaba: int MOD = 324124124; 892764d484 2014-09-17 kinaba: int _ = 24; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(2) 892764d484 2014-09-17 kinaba: int N = 3; 892764d484 2014-09-17 kinaba: int MOD = 5325352; 892764d484 2014-09-17 kinaba: int _ = 504; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(3) 892764d484 2014-09-17 kinaba: int N = 500; 892764d484 2014-09-17 kinaba: int MOD = 1000000007; 892764d484 2014-09-17 kinaba: int _ = 286169049; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: /* 892764d484 2014-09-17 kinaba: CASE(4) 892764d484 2014-09-17 kinaba: int N = ; 892764d484 2014-09-17 kinaba: int MOD = ; 892764d484 2014-09-17 kinaba: int _ = ; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: CASE(5) 892764d484 2014-09-17 kinaba: int N = ; 892764d484 2014-09-17 kinaba: int MOD = ; 892764d484 2014-09-17 kinaba: int _ = ; 892764d484 2014-09-17 kinaba: END 892764d484 2014-09-17 kinaba: */ 892764d484 2014-09-17 kinaba: } 892764d484 2014-09-17 kinaba: // END CUT HERE