Artifact Content
Not logged in

Artifact 31849cb71c63089ea1adbd27f4022b0233526019


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

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(LL       x):val(x%MODVAL) {}
};
mint& operator+=(mint& x, mint y) { return x = x.val+y.val; }
mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; }
mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; }
mint operator+(mint x, mint y) { return x+=y; }
mint operator-(mint x, mint y) { return x-=y; }
mint operator*(mint x, mint y) { return x*=y; }

class CampLunch { public:
	int count(int N, int M, vector <string> a)
	{
		memo.assign(N<<M, -1);
		memo2.assign(N*M<<M, -1);
		return rec(N, M, a, 0, 0);
	}

	vector<int> memo;
	int rec(int N, int M, const vector<string>& A, int d, int yesterday_free) {
		if(d == N)
			return 1;

		const int key = (d<<M) | yesterday_free;
		if(memo[key] >= 0)
			return memo[key];

		return memo[key] = rec2(N, M, A, d, 0, yesterday_free, 0);
	}

	vector<int> memo2;
	int rec2(int N, int M, const vector<string>& A,
		     int d, int i, int yesterday_free, int today_free) {
		if(i == M)
			return rec(N,M,A,d+1,today_free);

		const int key = ((d*M+i)<<M) | yesterday_free | today_free;
		if(memo2[key] >= 0)
			return memo2[key];

		mint total = 0;

		// Plan 2.
		int p =  A[d][i] - 'A';
		if(yesterday_free & (1<<p))
			total += rec2(N,M,A,d,i+1,yesterday_free&~(1<<p),today_free);
		// Plan 1.
		if(i+1<M) {
			int p2 = A[d][i+1]-'A';
			total += rec2(N,M,A,d,i+2,yesterday_free&~(1<<p)&~(1<<p2),today_free);
		}
		// No Plan.
		total += rec2(N,M,A,d,i+1,yesterday_free&~(1<<p),today_free|(1<<p));
		return memo2[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(_, CampLunch().count(N, M, a));}
int main(){

CASE(0)
	int N = 2; 
	int M = 2; 
	string a_[] = {"AB","AB"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 7; 
END
CASE(1)
	int N = 2; 
	int M = 3; 
	string a_[] = {"ABC","ABC"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 22; 
END
CASE(2)
	int N = 2; 
	int M = 3; 
	string a_[] = {"ABC","BAC"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 21; 
END
CASE(3)
	int N = 1; 
	int M = 1; 
	string a_[] = {"A"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 1; 
END
CASE(4)
	int N = 1; 
	int M = 10; 
	string a_[] = {"ABDEFHIGJC"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 89; 
END
CASE(5)
	int N = 16; 
	int M = 16; 
	string a_[] = {"ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOP"};
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = 950052677; 
END
/*
CASE(6)
	int N = ; 
	int M = ; 
	string a_[] = ;
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = ; 
END
CASE(7)
	int N = ; 
	int M = ; 
	string a_[] = ;
	  vector <string> a(a_, a_+sizeof(a_)/sizeof(*a_)); 
	int _ = ; 
END
*/
}
// END CUT HERE