4fd800b3a8 2011-02-23 kinaba: #include <iostream> 4fd800b3a8 2011-02-23 kinaba: #include <sstream> 4fd800b3a8 2011-02-23 kinaba: #include <iomanip> 4fd800b3a8 2011-02-23 kinaba: #include <vector> 4fd800b3a8 2011-02-23 kinaba: #include <string> 4fd800b3a8 2011-02-23 kinaba: #include <map> 4fd800b3a8 2011-02-23 kinaba: #include <set> 4fd800b3a8 2011-02-23 kinaba: #include <algorithm> 4fd800b3a8 2011-02-23 kinaba: #include <numeric> 4fd800b3a8 2011-02-23 kinaba: #include <iterator> 4fd800b3a8 2011-02-23 kinaba: #include <complex> 4fd800b3a8 2011-02-23 kinaba: #include <queue> 4fd800b3a8 2011-02-23 kinaba: #include <stack> 4fd800b3a8 2011-02-23 kinaba: #include <cmath> 4fd800b3a8 2011-02-23 kinaba: #include <cassert> 4fd800b3a8 2011-02-23 kinaba: #include <cstring> 4fd800b3a8 2011-02-23 kinaba: using namespace std; 4fd800b3a8 2011-02-23 kinaba: typedef long long LL; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: static const int MODVAL = 1000000007; 4fd800b3a8 2011-02-23 kinaba: int ADD(int x, int y) { return (x+y)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: int SUB(int x, int y) { return (x-y+MODVAL)%MODVAL; } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: class Unicorn { 4fd800b3a8 2011-02-23 kinaba: public: 4fd800b3a8 2011-02-23 kinaba: int countWays(int R, int C, int L, int seed, string word) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: // input... 4fd800b3a8 2011-02-23 kinaba: char B[300][300]; 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int x = seed; 4fd800b3a8 2011-02-23 kinaba: int d = (65535 / L)+1; 4fd800b3a8 2011-02-23 kinaba: for (int r=0; r<R; r++) 4fd800b3a8 2011-02-23 kinaba: for (int c=0; c<C; c++) { 4fd800b3a8 2011-02-23 kinaba: x = (x * 25173 + 13849) % 65536; 4fd800b3a8 2011-02-23 kinaba: B[r][c] = (65+(x / d)); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // solve 4fd800b3a8 2011-02-23 kinaba: vector<int> cnt(C*R); 4fd800b3a8 2011-02-23 kinaba: for(int r=0; r<R; ++r) 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<C; ++c) 4fd800b3a8 2011-02-23 kinaba: if( B[r][c] == word[0] ) 4fd800b3a8 2011-02-23 kinaba: cnt[r*C+c] = 1; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int i=1; i<word.size(); ++i) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int total_cnt = 0; 4fd800b3a8 2011-02-23 kinaba: vector<int> c_cnt(C); 4fd800b3a8 2011-02-23 kinaba: vector<int> r_cnt(R); 4fd800b3a8 2011-02-23 kinaba: for(int r=0; r<R; ++r) 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<C; ++c) 4fd800b3a8 2011-02-23 kinaba: r_cnt[r] = ADD( r_cnt[r], cnt[r*C+c]), 4fd800b3a8 2011-02-23 kinaba: c_cnt[c] = ADD( c_cnt[c], cnt[r*C+c]), 4fd800b3a8 2011-02-23 kinaba: total_cnt = ADD(total_cnt, cnt[r*C+c]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: vector<int> cnt2(C*R); 4fd800b3a8 2011-02-23 kinaba: for(int r=0; r<R; ++r) 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<C; ++c) 4fd800b3a8 2011-02-23 kinaba: if( B[r][c] == word[i] ) 4fd800b3a8 2011-02-23 kinaba: { 4fd800b3a8 2011-02-23 kinaba: int x = total_cnt; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr && rr<R) 4fd800b3a8 2011-02-23 kinaba: x = SUB(x, r_cnt[rr]); 4fd800b3a8 2011-02-23 kinaba: for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc && cc<C) 4fd800b3a8 2011-02-23 kinaba: x = SUB(x, c_cnt[cc]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr && rr<R) 4fd800b3a8 2011-02-23 kinaba: for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc && cc<C) 4fd800b3a8 2011-02-23 kinaba: x = ADD(x, cnt[rr*C+cc]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: for(int rr=r-2; rr<=r+2; rr+=4) if(0<=rr && rr<R) 4fd800b3a8 2011-02-23 kinaba: for(int cc=c-2; cc<=c+2; cc+=4) if(0<=cc && cc<C) 4fd800b3a8 2011-02-23 kinaba: x = SUB(x, cnt[rr*C+cc]); 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: cnt2[r*C+c] = x; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: cnt.swap(cnt2); 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: int live = 0; 4fd800b3a8 2011-02-23 kinaba: for(int r=0; r<R; ++r) 4fd800b3a8 2011-02-23 kinaba: for(int c=0; c<C; ++c) 4fd800b3a8 2011-02-23 kinaba: live = ADD(live, cnt[r*C+c]); 4fd800b3a8 2011-02-23 kinaba: return live; 4fd800b3a8 2011-02-23 kinaba: } 4fd800b3a8 2011-02-23 kinaba: }; 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: // BEGIN CUT HERE 4fd800b3a8 2011-02-23 kinaba: #include <ctime> 4fd800b3a8 2011-02-23 kinaba: double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } 4fd800b3a8 2011-02-23 kinaba: int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;} 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> struct Case_ { Case_(){start_time=clock();} }; 4fd800b3a8 2011-02-23 kinaba: char Test_(...); 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<0>) { 4fd800b3a8 2011-02-23 kinaba: int R = 3; 4fd800b3a8 2011-02-23 kinaba: int C = 4; 4fd800b3a8 2011-02-23 kinaba: int L = 2; 4fd800b3a8 2011-02-23 kinaba: int seed = 47; 4fd800b3a8 2011-02-23 kinaba: string word = "AB"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 2; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<1>) { 4fd800b3a8 2011-02-23 kinaba: int R = 5; 4fd800b3a8 2011-02-23 kinaba: int C = 5; 4fd800b3a8 2011-02-23 kinaba: int L = 2; 4fd800b3a8 2011-02-23 kinaba: int seed = 47; 4fd800b3a8 2011-02-23 kinaba: string word = "CD"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<2>) { 4fd800b3a8 2011-02-23 kinaba: int R = 4; 4fd800b3a8 2011-02-23 kinaba: int C = 4; 4fd800b3a8 2011-02-23 kinaba: int L = 1; 4fd800b3a8 2011-02-23 kinaba: int seed = 42; 4fd800b3a8 2011-02-23 kinaba: string word = "AA"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 20; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<3>) { 4fd800b3a8 2011-02-23 kinaba: int R = 4; 4fd800b3a8 2011-02-23 kinaba: int C = 4; 4fd800b3a8 2011-02-23 kinaba: int L = 1; 4fd800b3a8 2011-02-23 kinaba: int seed = 42; 4fd800b3a8 2011-02-23 kinaba: string word = "AAAAA"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 172; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<4>) { 4fd800b3a8 2011-02-23 kinaba: int R = 1; 4fd800b3a8 2011-02-23 kinaba: int C = 1; 4fd800b3a8 2011-02-23 kinaba: int L = 5; 4fd800b3a8 2011-02-23 kinaba: int seed = 54321; 4fd800b3a8 2011-02-23 kinaba: string word = "ABCDE"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 0; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<5>) { 4fd800b3a8 2011-02-23 kinaba: int R = 8; 4fd800b3a8 2011-02-23 kinaba: int C = 8; 4fd800b3a8 2011-02-23 kinaba: int L = 26; 4fd800b3a8 2011-02-23 kinaba: int seed = 226; 4fd800b3a8 2011-02-23 kinaba: string word = "TOPCODER"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 1; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: int Test_(Case_<6>) { 4fd800b3a8 2011-02-23 kinaba: int R = 20; 4fd800b3a8 2011-02-23 kinaba: int C = 20; 4fd800b3a8 2011-02-23 kinaba: int L = 2; 4fd800b3a8 2011-02-23 kinaba: int seed = 47; 4fd800b3a8 2011-02-23 kinaba: string word = "AAAAA"; 4fd800b3a8 2011-02-23 kinaba: int RetVal = 373977054; 4fd800b3a8 2011-02-23 kinaba: return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); } 4fd800b3a8 2011-02-23 kinaba: 4fd800b3a8 2011-02-23 kinaba: template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); } 4fd800b3a8 2011-02-23 kinaba: template<> void Run_<-1>() {} 4fd800b3a8 2011-02-23 kinaba: int main() { Run_<0>(); } 4fd800b3a8 2011-02-23 kinaba: // END CUT HERE 4fd800b3a8 2011-02-23 kinaba: