Artifact Content
Not logged in

Artifact 757c488211084a3db2b0e02d1b79a2702d190f7c


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

static const int MODVAL = 1000000007;

struct UnionFind
{
	vector<int> uf, sz;
	int nc;

	UnionFind(int N): uf(N), sz(N,1), nc(N)
		{ for(int i=0; i<N; ++i) uf[i] = i; }
	int size()
		{ return nc; }
	int Find(int a)
		{ return uf[a]==a ? a : uf[a]=Find(uf[a]); }
	bool Union(int a, int b)
		{
			a = Find(a);
			b = Find(b);
			if( a != b )
			{
				if( sz[a] >= sz[b] ) swap(a, b);
				uf[a] = b;
				sz[b] += sz[a];
				--nc;
			}
			return (a!=b);
		}
};

struct mint
{
	int val;
	mint():val(0){}
	mint(int x):val(x%MODVAL) {}
	mint(LL  x):val(x%MODVAL) {}
};
mint operator+(mint x, mint y) { return x.val+y.val; }
mint operator-(mint x, mint y) { return x.val-y.val+MODVAL; }
mint operator*(mint x, mint y) { return LL(x.val)*y.val; }
mint POW(mint x, int e) {
	mint v = 1;
	for(;e;x=x*x,e>>=1)
		if(e&1)
			v=v*x;
	return v;
}

class MagicalGirlLevelTwoDivOne { public:
	int theCount(vector <string> palette, int n, int m)
	{
		if( (n^m)&1 )
			return 0;
		int H = palette.size();
		int W = palette[0].size();

		#define key(y,x) (2+(y)*W+(x))

		UnionFind uf(W*H+2);
		for(int y=0; y<H; ++y)
		for(int x=0; x<W; ++x)
		{
			if( palette[y][x] != '.' )
				uf.Union(key(y,x), (palette[y][x]-'0')%2); // 0:even, 1:odd
			if( y-n>=0 )
				uf.Union(key(y,x), key(y-n,x));
			if( x-m>=0 )
				uf.Union(key(y,x), key(y,x-m));
		}

		if( uf.Find(0) == uf.Find(1) )
			return 0;

		vector< vector<int> > dep(n, vector<int>(m));
		vector< vector<int> > stt(n, vector<int>(m));
		for(int y=0; y<n; ++y)
		for(int x=0; x<m; ++x)
		{
			for(int yy=y; yy<H; yy+=n)
			for(int xx=x; xx<W; xx+=m)
				if(palette[yy][xx] == '.')
					dep[y][x]++;
			int k = uf.Find(key(y,x));
			if( k == uf.Find(0) )
				stt[y][x] = 0;
			else if( k == uf.Find(1) )
				stt[y][x] = 1;
			else
				stt[y][x] = 2;
		}

		memo.assign(1024*10, -1);
		return rec(dep, stt, n, m, 0, 0);
	}

	vector<int> memo;
	int rec(const vector< vector<int> >& dep, const vector< vector<int> >& stt, const int H, const int W,
		int y, int status)
	{
		if( y == H )
			if( status == (1<<W)-1 )
				return 1;
			else
				return 0;
		int me = status*H+y;
		if( memo[me] != -1 )
			return memo[me];

		mint total;
		for(int bits=0; bits<(1<<W); ++bits)
		{
			mint cur(1);
			int odd = 0;
			for(int x=0; x<W; ++x)
				if( (bits>>x)&1 )
					++odd;
			int odddep = 0;
			int evendep = 0;
			if(odd%2 == 0)
				goto next;
			for(int x=0; x<W; ++x)
				if( stt[y][x] != 2 && stt[y][x] != ((bits>>x)&1) )
					goto next;
			for(int x=0; x<W; ++x)
			{
				int d = dep[y][x];
				(((bits>>x)&1) ? odddep : evendep) += d;
			}
				cur = cur * POW(5, odddep);
				cur = cur * POW(4,evendep);
		cur = cur * rec(dep, stt, H, W, y+1, status^bits);
			total = total + cur;
		next:;
		}
		return memo[me] = 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(_, MagicalGirlLevelTwoDivOne().theCount(palette, n, m));}
int main(){

CASE(0)
	string palette_[] = {"12",
 "2."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 2; 
	int m = 2; 
	int _ = 5; 
END
CASE(1)
	string palette_[] = {"21",
 "1."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 2; 
	int m = 2; 
	int _ = 4; 
END
CASE(2)
	string palette_[] = {"...",
 "...",
 "..."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 1; 
	int m = 1; 
	int _ = 1953125; 
END
CASE(3)
	string palette_[] = {"..58..",
 "..47.."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 2; 
	int m = 3; 
	int _ = 0; 
END
CASE(4)
	string palette_[] = {"...1.2.3",
 "4.5.6...",
 "...7.8.9",
 "1.2.3..."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 4; 
	int m = 4; 
	int _ = 886073030; 
END
CASE(5)
	string palette_[] = {"....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................."};
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = 10; 
	int m = 10; 
	int _ = 240076532; 
END
/*
CASE(6)
	string palette_[] = ;
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = ; 
	int m = ; 
	int _ = ; 
END
CASE(7)
	string palette_[] = ;
	  vector <string> palette(palette_, palette_+sizeof(palette_)/sizeof(*palette_)); 
	int n = ; 
	int m = ; 
	int _ = ; 
END
*/
}
// END CUT HERE