Artifact Content
Not logged in

Artifact 850feccc28db3e68eed96cf31ea6081175c044e3


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
using namespace std;
typedef long long LL;

static const int MODVAL = 1000000007;
int ADD(int x, int y) { return (x+y)%MODVAL; }
int SUB(int x, int y) { return (x-y+MODVAL)%MODVAL; }

class Unicorn {
public:
	int countWays(int R, int C, int L, int seed, string word) 
	{
		// input...
		char B[300][300];
		{
			int x = seed;
			int d = (65535 / L)+1;
			for (int r=0; r<R; r++)
			  for (int c=0; c<C; c++) {
			    x = (x * 25173 + 13849) % 65536;
			    B[r][c] = (65+(x / d));
			  }
		}

		// solve
		vector<int> cnt(C*R);
		for(int r=0; r<R; ++r)
		for(int c=0; c<C; ++c)
			if( B[r][c] == word[0] )
				cnt[r*C+c] = 1;

		for(int i=1; i<word.size(); ++i)
		{
			int total_cnt = 0;
			vector<int> c_cnt(C);
			vector<int> r_cnt(R);
			for(int r=0; r<R; ++r)
			for(int c=0; c<C; ++c)
				r_cnt[r]  = ADD( r_cnt[r], cnt[r*C+c]),
				c_cnt[c]  = ADD( c_cnt[c], cnt[r*C+c]),
				total_cnt = ADD(total_cnt, cnt[r*C+c]);

			vector<int> cnt2(C*R);
			for(int r=0; r<R; ++r)
			for(int c=0; c<C; ++c)
				if( B[r][c] == word[i] )
				{
					int x = total_cnt;

					for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr && rr<R)
						x = SUB(x, r_cnt[rr]);
					for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc && cc<C)
						x = SUB(x, c_cnt[cc]);

					for(int rr=r-1; rr<=r+1; ++rr) if(0<=rr && rr<R)
					for(int cc=c-1; cc<=c+1; ++cc) if(0<=cc && cc<C)
						x = ADD(x, cnt[rr*C+cc]);

					for(int rr=r-2; rr<=r+2; rr+=4) if(0<=rr && rr<R)
					for(int cc=c-2; cc<=c+2; cc+=4) if(0<=cc && cc<C)
						x = SUB(x, cnt[rr*C+cc]);

					cnt2[r*C+c] = x;
				}
			cnt.swap(cnt2);
		}

		int live = 0;
		for(int r=0; r<R; ++r)
		for(int c=0; c<C; ++c)
			live = ADD(live, cnt[r*C+c]);
		return live;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time;string timer() { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }

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(); }
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;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int R = 3; 
	int C = 4; 
	int L = 2; 
	int seed = 47; 
	string word = "AB"; 
	int RetVal = 2; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<1>) {
	int R = 5; 
	int C = 5; 
	int L = 2; 
	int seed = 47; 
	string word = "CD"; 
	int RetVal = 0; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<2>) {
	int R = 4; 
	int C = 4; 
	int L = 1; 
	int seed = 42; 
	string word = "AA"; 
	int RetVal = 20; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<3>) {
	int R = 4; 
	int C = 4; 
	int L = 1; 
	int seed = 42; 
	string word = "AAAAA"; 
	int RetVal = 172; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<4>) {
	int R = 1; 
	int C = 1; 
	int L = 5; 
	int seed = 54321; 
	string word = "ABCDE"; 
	int RetVal = 0; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<5>) {
	int R = 8; 
	int C = 8; 
	int L = 26; 
	int seed = 226; 
	string word = "TOPCODER"; 
	int RetVal = 1; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }
int Test_(Case_<6>) {
	int R = 20; 
	int C = 20; 
	int L = 2; 
	int seed = 47; 
	string word = "AAAAA"; 
	int RetVal = 373977054; 
	return verify_case(RetVal, Unicorn().countWays(R, C, L, seed, word)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE