Artifact Content
Not logged in

Artifact ae835c90175ffde078c8203e8e698cf3c8d524dc


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;

static const unsigned MODVAL = 1000000007;
struct mint
{
	unsigned val;
	mint():val(0){}
	mint(int      x):val(x%MODVAL) {}
	mint(unsigned x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }

class AlienAndSetDiv1 { public:
	int getNumber(int N, int K)
	{
		memo.assign((1<<K)*(N+1)*(N+1), -1);
		vector<int> A, B;
		return rec(N, K, A, B, 0);
	}

	vector<int> memo;
	int rec(size_t N, int K, vector<int>& A, vector<int>& B, int lastk)
	{
		if(A.size()==N && B.size()==N)
			return 1;

		auto key = (lastk*(N+1)+A.size())*(N+1)+B.size();
		if(memo[key] >= 0)
			return memo[key];

		int next = A.size()+B.size()+1, nextmask = (lastk &~ (1<<(K-1)))<<1;
		int ai = A.size(), bi = B.size();

		mint total = 0;

		A.push_back(next);
		if(A.size()<=N && (ai>=B.size() || abs(A[ai]-B[ai])>=K))
			total += rec(N, K, A, B, nextmask);
		A.pop_back();
		B.push_back(next);
		if(B.size()<=N && (bi>=A.size() || abs(A[bi]-B[bi])>=K))
			total += rec(N, K, A, B, nextmask|1);
		B.pop_back();

		return memo[key] = total.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(_, AlienAndSetDiv1().getNumber(N, K));}
int main(){

CASE(0)
	int N = 2; 
	int K = 2; 
	int _ = 2; 
END
CASE(1)
	int N = 3; 
	int K = 1; 
	int _ = 20; 
END
CASE(2)
	int N = 4; 
	int K = 2; 
	int _ = 14; 
END
CASE(3)
	int N = 10; 
	int K = 7; 
	int _ = 40; 
END
CASE(4)
	int N = 50; 
	int K = 10; 
	int _ = -1; 
END
CASE(5)
	int N = 50; 
	int K = 1; 
	int _ = -1; 
END
CASE(5)
	int N = 50; 
	int K = 2; 
	int _ = -1; 
END
CASE(5)
	int N = 50; 
	int K = 4; 
	int _ = -1; 
END
CASE(5)
	int N = 2; 
	int K = 2; 
	int _ = 2; 
END
}
// END CUT HERE