445abb9772 2015-05-31 kinaba: #include <iostream> 445abb9772 2015-05-31 kinaba: #include <sstream> 445abb9772 2015-05-31 kinaba: #include <iomanip> 445abb9772 2015-05-31 kinaba: #include <vector> 445abb9772 2015-05-31 kinaba: #include <string> 445abb9772 2015-05-31 kinaba: #include <map> 445abb9772 2015-05-31 kinaba: #include <set> 445abb9772 2015-05-31 kinaba: #include <algorithm> 445abb9772 2015-05-31 kinaba: #include <numeric> 445abb9772 2015-05-31 kinaba: #include <iterator> 445abb9772 2015-05-31 kinaba: #include <functional> 445abb9772 2015-05-31 kinaba: #include <complex> 445abb9772 2015-05-31 kinaba: #include <queue> 445abb9772 2015-05-31 kinaba: #include <stack> 445abb9772 2015-05-31 kinaba: #include <cmath> 445abb9772 2015-05-31 kinaba: #include <cassert> 445abb9772 2015-05-31 kinaba: #include <tuple> 445abb9772 2015-05-31 kinaba: using namespace std; 445abb9772 2015-05-31 kinaba: typedef long long LL; 445abb9772 2015-05-31 kinaba: typedef complex<double> CMP; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: static const unsigned MODVAL = 1000000007; 445abb9772 2015-05-31 kinaba: struct mint 445abb9772 2015-05-31 kinaba: { 445abb9772 2015-05-31 kinaba: unsigned val; 445abb9772 2015-05-31 kinaba: mint():val(0){} 445abb9772 2015-05-31 kinaba: mint(int x):val(x%MODVAL) {} 445abb9772 2015-05-31 kinaba: mint(unsigned x):val(x%MODVAL) {} 445abb9772 2015-05-31 kinaba: mint(LL x):val(x%MODVAL) {} 445abb9772 2015-05-31 kinaba: }; 445abb9772 2015-05-31 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 445abb9772 2015-05-31 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 445abb9772 2015-05-31 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 445abb9772 2015-05-31 kinaba: mint operator+(mint x, mint y) { return x+=y; } 445abb9772 2015-05-31 kinaba: mint operator-(mint x, mint y) { return x-=y; } 445abb9772 2015-05-31 kinaba: mint operator*(mint x, mint y) { return x*=y; } 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: class CampLunch { public: 445abb9772 2015-05-31 kinaba: int count(int N, int M, vector <string> a) 445abb9772 2015-05-31 kinaba: { 445abb9772 2015-05-31 kinaba: memo.assign(N<<M, -1); 445abb9772 2015-05-31 kinaba: memo2.assign(N*M<<M, -1); 445abb9772 2015-05-31 kinaba: return rec(N, M, a, 0, 0); 445abb9772 2015-05-31 kinaba: } 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: vector<int> memo; 445abb9772 2015-05-31 kinaba: int rec(int N, int M, const vector<string>& A, int d, int yesterday_free) { 445abb9772 2015-05-31 kinaba: if(d == N) 445abb9772 2015-05-31 kinaba: return 1; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: const int key = (d<<M) | yesterday_free; 445abb9772 2015-05-31 kinaba: if(memo[key] >= 0) 445abb9772 2015-05-31 kinaba: return memo[key]; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: return memo[key] = rec2(N, M, A, d, 0, yesterday_free, 0); 445abb9772 2015-05-31 kinaba: } 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: vector<int> memo2; 445abb9772 2015-05-31 kinaba: int rec2(int N, int M, const vector<string>& A, 445abb9772 2015-05-31 kinaba: int d, int i, int yesterday_free, int today_free) { 445abb9772 2015-05-31 kinaba: if(i == M) 445abb9772 2015-05-31 kinaba: return rec(N,M,A,d+1,today_free); 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: const int key = ((d*M+i)<<M) | yesterday_free | today_free; 445abb9772 2015-05-31 kinaba: if(memo2[key] >= 0) 445abb9772 2015-05-31 kinaba: return memo2[key]; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: mint total = 0; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: // Plan 2. 445abb9772 2015-05-31 kinaba: int p = A[d][i] - 'A'; 445abb9772 2015-05-31 kinaba: if(yesterday_free & (1<<p)) 445abb9772 2015-05-31 kinaba: total += rec2(N,M,A,d,i+1,yesterday_free&~(1<<p),today_free); 445abb9772 2015-05-31 kinaba: // Plan 1. 445abb9772 2015-05-31 kinaba: if(i+1<M) { 445abb9772 2015-05-31 kinaba: int p2 = A[d][i+1]-'A'; 445abb9772 2015-05-31 kinaba: total += rec2(N,M,A,d,i+2,yesterday_free&~(1<<p)&~(1<<p2),today_free); 445abb9772 2015-05-31 kinaba: } 445abb9772 2015-05-31 kinaba: // No Plan. 445abb9772 2015-05-31 kinaba: total += rec2(N,M,A,d,i+1,yesterday_free&~(1<<p),today_free|(1<<p)); 445abb9772 2015-05-31 kinaba: return memo2[key] = total.val; 445abb9772 2015-05-31 kinaba: } 445abb9772 2015-05-31 kinaba: }; 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: // BEGIN CUT HERE 445abb9772 2015-05-31 kinaba: #include <ctime> 445abb9772 2015-05-31 kinaba: double start_time; string timer() 445abb9772 2015-05-31 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 445abb9772 2015-05-31 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 445abb9772 2015-05-31 kinaba: { os << "{ "; 445abb9772 2015-05-31 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 445abb9772 2015-05-31 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 445abb9772 2015-05-31 kinaba: void verify_case(const int& Expected, const int& Received) { 445abb9772 2015-05-31 kinaba: bool ok = (Expected == Received); 445abb9772 2015-05-31 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 445abb9772 2015-05-31 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 445abb9772 2015-05-31 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 445abb9772 2015-05-31 kinaba: #define END verify_case(_, CampLunch().count(N, M, a));} 445abb9772 2015-05-31 kinaba: int main(){ 445abb9772 2015-05-31 kinaba: 445abb9772 2015-05-31 kinaba: CASE(0) 445abb9772 2015-05-31 kinaba: int N = 2; 445abb9772 2015-05-31 kinaba: int M = 2; 445abb9772 2015-05-31 kinaba: string a_[] = {"AB","AB"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 7; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(1) 445abb9772 2015-05-31 kinaba: int N = 2; 445abb9772 2015-05-31 kinaba: int M = 3; 445abb9772 2015-05-31 kinaba: string a_[] = {"ABC","ABC"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 22; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(2) 445abb9772 2015-05-31 kinaba: int N = 2; 445abb9772 2015-05-31 kinaba: int M = 3; 445abb9772 2015-05-31 kinaba: string a_[] = {"ABC","BAC"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 21; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(3) 445abb9772 2015-05-31 kinaba: int N = 1; 445abb9772 2015-05-31 kinaba: int M = 1; 445abb9772 2015-05-31 kinaba: string a_[] = {"A"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 1; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(4) 445abb9772 2015-05-31 kinaba: int N = 1; 445abb9772 2015-05-31 kinaba: int M = 10; 445abb9772 2015-05-31 kinaba: string a_[] = {"ABDEFHIGJC"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 89; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(5) 445abb9772 2015-05-31 kinaba: int N = 16; 445abb9772 2015-05-31 kinaba: int M = 16; 445abb9772 2015-05-31 kinaba: string a_[] = {"ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP"}; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = 950052677; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: /* 445abb9772 2015-05-31 kinaba: CASE(6) 445abb9772 2015-05-31 kinaba: int N = ; 445abb9772 2015-05-31 kinaba: int M = ; 445abb9772 2015-05-31 kinaba: string a_[] = ; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = ; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: CASE(7) 445abb9772 2015-05-31 kinaba: int N = ; 445abb9772 2015-05-31 kinaba: int M = ; 445abb9772 2015-05-31 kinaba: string a_[] = ; 445abb9772 2015-05-31 kinaba: vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 445abb9772 2015-05-31 kinaba: int _ = ; 445abb9772 2015-05-31 kinaba: END 445abb9772 2015-05-31 kinaba: */ 445abb9772 2015-05-31 kinaba: } 445abb9772 2015-05-31 kinaba: // END CUT HERE