Artifact Content
Not logged in

Artifact bb93038d2c81fe7fd4b59f3bf1a268c3166528cf


#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>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

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

static const int MODVAL = 1000000007;

class RowOfColors { public:
	int countWays(int w, int h, int k) 
	{
		DP3x<LL> dp(w+1, h+1, k+1);
		for(int i=w; i>=0; --i)
		for(int stack=0; stack<=h; ++stack)
		for(int used =0; used <=k; ++used)
			if( i == w )
				dp(i,stack,used) = (stack==1 && used==k ? 1 : 0);
			else {
				LL cnt = 0;
				if(stack+1<=h && used+1<=k) // push new color
					cnt += dp(i+1,stack+1,used+1);
				if(stack) // continue the same color
					cnt += dp(i+1,stack,used);
				if(stack && used+1<=k) // replace top
					cnt += dp(i+1,stack,used+1);
				if(stack>=2) // pop
					cnt += dp(i+1,stack-1,used);
				dp(i,stack,used) = cnt % MODVAL;
			}
		LL v = dp(0,0,0);
		for(int i=1; i<=k; ++i)
			v = (v*i) % MODVAL;
		return int(v);
	}
};

// 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(_, RowOfColors().countWays(w, h, k));}
int main(){

CASE(0)
	int w = 4; 
	int h = 1; 
	int k = 2; 
	int _ = 6; 
END
CASE(1)
	int w = 4; 
	int h = 3; 
	int k = 2; 
	int _ = 12; 
END
CASE(2)
	int w = 4; 
	int h = 4; 
	int k = 10; 
	int _ = 0; 
END
CASE(3)
	int w = 14; 
	int h = 28; 
	int k = 14; 
	int _ = 178290591; 
END
/*
CASE(4)
	int w = ; 
	int h = ; 
	int k = ; 
	int _ = ; 
END
CASE(5)
	int w = ; 
	int h = ; 
	int k = ; 
	int _ = ; 
END
*/
}
// END CUT HERE