Artifact Content
Not logged in

Artifact a3d3d1115263fbcbba82ce7dda24827f5924c894


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <cstring>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

template<typename T>
struct DP3
{
	int N1, N2, N3;
	vector<T> data;
	DP3(int N1, int N2, int N3, const T& t = T())
		: N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); }
	T& operator()(int i1, int i2, int i3)
		{ return data[ ((i1*N2)+i2)*N3+i3 ]; }
	void swap(DP3& rhs)
		{ data.swap(rhs.data); }
};

static const int MODVAL = 1000000007; // must be prime for op/
struct mint
{
	int val;
	mint():val(0){}
	mint(int    x):val(x%MODVAL) {} // x>=0
	mint(size_t x):val(x%MODVAL) {} // x>=0
	mint(LL     x):val(x%MODVAL) {} // x>=0
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint operator+(mint x, mint y) { return x+=y; }

class DengklekPaintingSquares { public:
	int numSolutions(int N, int M)
	{
		int END=1; for(int i=0; i<M-1; ++i) END*=3;
		DP3<int> memo(N, M, END*3, -1);
		return rec(memo, N, M, END, -1, M, 0);
	}

	int rec(DP3<int>& memo, const int H, const int W, const int END, int y, int x, int state)
	{
		if( x == W )
			++y, x=0;
		if( y == H )
			return valid(state) ? 1 : 0;

		if( memo(y,x,state)!=-1 )
			return memo(y,x,state);

		mint total = 0;
		int old = state%3;
		int pre = state/END%3;
		bool put=(old==0 || old==1), nut=(old==0 || old==2);
		int parity = (old>=1) ^ (x==0 ? 0 : pre>=1);
		if( x == 0 || pre == 0 ) {
			if(put) total += rec(memo, H, W, END, y, x+1, state/3 + (2-parity)*END);
			if(nut) total += rec(memo, H, W, END, y, x+1, state/3 + 0*END);
		} else {
			if(put) total += rec(memo, H, W, END, y, x+1, (state-pre*END)/3 + (3-pre)*(END/3) + (2-parity)*END);
			if(nut) total += rec(memo, H, W, END, y, x+1, state/3 + 0*END);
		}
		return memo(y,x,state) = total.val;
	}

	bool valid(int state)
	{
		for(; state; state/=3)
			if(state%3 == 1)
				return false;
		return true;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, DengklekPaintingSquares().numSolutions(N, M));}
int main(){

CASE(0)
	int N = 1; 
	int M = 1; 
	int _ = 2; 
END
CASE(1)
	int N = 2; 
	int M = 2; 
	int _ = 8; 
END
CASE(2)
	int N = 1; 
	int M = 3; 
	int _ = 5; 
END
CASE(3)
	int N = 47; 
	int M = 7; 
	int _ = 944149920; 
END
CASE(4)
	int N = 81; 
	int M = 8; 
	int _ = -1; 
END
/*
CASE(5)
	int N = ; 
	int M = ; 
	int _ = ; 
END
*/
}
// END CUT HERE