Artifact Content
Not logged in

Artifact a5241fc4c196a524e8dc559fc8ee967a6b436676


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

static const unsigned MODVAL = 1000000009;
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; }
mint POW(mint x, LL e) { mint v=1; for(;e;x*=x,e>>=1) if(e&1) v*=x; return v; }

class CoinsGame { public:
	int ways(vector <string> board)
	{
		map<pair<int,int>,int> ID;
		for(int y=0; y<board.size(); ++y)
		for(int x=0; x<board[0].size(); ++x)
		if(board[y][x]=='.') {
			int id = ID.size() + 1;
			ID[make_pair(y,x)] = id;
		}

		vector< vector<int> > G(ID.size()+1);
		G[0] = vector<int>(4, 0);

		int H = board.size();
		int W = board[0].size();
		for(int y=0; y<board.size(); ++y)
		for(int x=0; x<board[0].size(); ++x)
		if(board[y][x]=='.')
		{
			int dy[]={-1,+1,0,0};
			int dx[]={0,0,-1,+1};
			for(int d=0; d<4; ++d)
			{
				int yy=y+dy[d], xx=x+dx[d];
				if(yy<0||H<=yy||xx<0||W<=xx) {
					// fell
					G[ID[make_pair(y,x)]].push_back(0); // 0=fall
				}
				else if(board[yy][xx]=='#') {
					// stop at yy-dy[d], xx-dx[d]
					G[ID[make_pair(y,x)]].push_back(ID[make_pair(y,x)]);
				}
				else {
					G[ID[make_pair(y,x)]].push_back(ID[make_pair(yy,xx)]);
				}
			}
		}
		return solve(G).val;
	}

	mint solve(vector< vector<int> >& G)
	{
		vector< vector<int> > set_buffer;
		set<int> active_sets;
		set<int> active_splitters;

		{
			set_buffer.push_back( vector<int>(1,0) );
			vector<int> all;
			for(int v=1; v<G.size(); ++v)
				all.push_back(v);
			set_buffer.push_back(all);

			active_sets.insert(0);
			active_sets.insert(1);
			active_splitters.insert(0);
		}

		while(!active_splitters.empty())
		{
			const vector<int>& splv = set_buffer[*active_splitters.begin()];
			set<int> spl(splv.begin(), splv.end());
			active_splitters.erase(active_splitters.begin());

			for(int d=0; d<4; ++d)
			for(set<int>::iterator it=active_sets.begin(); it!=active_sets.end(); )
			{
				int ii = *it++;
				const vector<int>& cur = set_buffer[ii];
				vector<int> t, f;
				for(int k=0; k<cur.size(); ++k)
					(spl.count(G[cur[k]][d]) ? t : f).push_back(cur[k]);
				if(!t.empty() && !f.empty())
				{
					set_buffer.push_back(t);
					set_buffer.push_back(f);
					active_sets.insert(set_buffer.size()-2);
					active_sets.insert(set_buffer.size()-1);
					if(active_splitters.count(ii)) {
						active_splitters.insert(set_buffer.size()-2);
						active_splitters.insert(set_buffer.size()-1);
					} else {
						active_splitters.insert(t.size()<f.size() ? set_buffer.size()-2 : set_buffer.size()-1);
					}
					active_splitters.erase(ii);
					active_sets.erase(ii);
				}
			}
		}

		vector<int> es;
		for(set<int>::iterator it=active_sets.begin(); it!=active_sets.end(); ++it)
			if(*it)
				es.push_back(set_buffer[*it].size());
/*
		for(bool changed=true; changed;) 
		{
			changed=false;
			for(int i=0; i<eqs.size(); ++i) if(!eqs[i].second && !dead[i]){
				set<int> spl(eqs[i].first.begin(), eqs[i].first.end());
				eqs[i].second = true;
				for(int k=0; k<eqs.size(); ++k)
				for(int d=0; d<4; ++d) if(!dead[k]) {
					vector<int> t, f;
					for(vector<int>::iterator it=eqs[k].first.begin(); it!=eqs[k].first.end(); ++it)
						(spl.count(G[*it][d]) ? t : f).push_back(*it);
					if(!t.empty() && !f.empty()) {
						if(eqs[k].second) {
							eqs.push_back(make_pair(t,t.size()>f.size()));
							eqs.push_back(make_pair(f,t.size()<=f.size()));
							dead.push_back(false);
							dead.push_back(false);
							changed = true;
						} else {
							eqs.push_back(make_pair(t,false));
							eqs.push_back(make_pair(f,false));
							dead.push_back(false);
							dead.push_back(false);
							changed = true;
						}
						dead[k] = true;
					}
				}
			}
		}

		vector<int> es;
		for(int i=1; i<eqs.size(); ++i) if(!dead[i]) 
			es.push_back(eqs[i].first.size());
*/
		int sum = accumulate(es.begin(), es.end(), 0);
		mint x = POW(2, sum);
		for(int i=0; i<es.size(); ++i)
			x -= POW(2, es[i])-1;
		return x-1;
	}
};

// 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(_, CoinsGame().ways(board));}
int main(){

CASE(0)
	string board_[] = {".."};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 1; 
END
CASE(1)
	string board_[] = {"##.#",
 ".###",
 "###.",
 "#.##"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 11; 
END
CASE(2)
	string board_[] = {"####",
 "#..#",
 "#..#",
 "####"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 0; 
END
CASE(3)
	string board_[] = {"#.#.#"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 0; 
END
CASE(4)
	string board_[] = {"........",
 "........",
 "........",
 "........",
 "........",
 "........",
 "........",
 "........"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 688856388; 
END
CASE(5)
	string board_[] = {"........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................", "........................................"};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 150087966; 
END
CASE(5)
	string board_[] = {"........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 150087966; 
END
CASE(5)
	string board_[] = {"........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
"........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
"........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
"........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................", 
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",
 "........................................",};
	  vector <string> board(board_, board_+sizeof(board_)/sizeof(*board_)); 
	int _ = 150087966; 
END
}
// END CUT HERE