Artifact Content
Not logged in

Artifact 6d39c5c3460aa153ea4f7b7be93cb8546a55365c


#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;

static const int MODVAL = 1000000007;
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; }

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

class DengklekBuildingRoads { public:
	int numWays(int N, int M, int K)
	{
		DP4<mint> dp(N, M+1, 1<<K+1, K);
		for(int a=N-1; a>=0; --a)
		for(int e=0; e<=M; ++e)
		for(int mask=0; mask<(1<<K+1); ++mask)
		for(int b=K-1; b>=0; --b)
		{
			mint v = 0;
			// draw edge
			if(e && a+b+1<N)
				v += dp(a, e-1, mask^1^(1<<b+1), b);

			// no edge
			if(b+1<K)
				v += dp(a, e, mask, b+1);
			else {
				if(a+1<N) {
					if( (mask&1)==0 )
						v += dp(a+1,e,mask>>1,0);
				} else {
					if( mask==0 && e==0 )
						v += 1;
				}
			}
			dp(a,e,mask,b) = v;
		}
		return dp(0,M,0,0).val;
	}
};

// 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(_, DengklekBuildingRoads().numWays(N, M, K));}
int main(){

CASE(0)
	int N = 3; 
	int M = 4; 
	int K = 1; 
	int _ = 3; 
END
CASE(1)
	int N = 4; 
	int M = 3; 
	int K = 3; 
	int _ = 4; 
END
CASE(2)
	int N = 2; 
	int M = 1; 
	int K = 1; 
	int _ = 0; 
END
CASE(3)
	int N = 5; 
	int M = 0; 
	int K = 3; 
	int _ = 1; 
END
CASE(4)
	int N = 10; 
	int M = 20; 
	int K = 5; 
	int _ = 26964424; 
END
CASE(5)
	int N = 1; 
	int M = 0; 
	int K = 1; 
	int _ = 1; 
END
CASE(6)
	int N = 30; 
	int M = 30; 
	int K = 8; 
	int _ = -1; 
END

}
// END CUT HERE