Artifact Content
Not logged in

Artifact 4aebde82e204f3cff5e25e548ebe9e0527d1407e


#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; }
mint POW(mint x, LL e) { mint v = 1; for (; e; x *= x, e >>= 1) if (e & 1) v *= x; return v; }
mint& operator/=(mint& x, mint y) { return x *= POW(y, MODVAL - 2); }
mint operator/(mint x, mint y) { return x /= y; }

class DiagonalColumn { public:
	int countGrids(int H, int W)
	{
		mint ans = 1; // all black
		for(int l=0; l<W; ++l)
			for (int r = 0; l + r < W; ++r) {
				if (W - l - r <= 2)
					ans += 1;
				else
					ans += sub(H, W - l - r);
			}
		return ans.val;
	}

	mint sub(LL H, LL W)
	{
		// columns = _?????_
		mint f = POW(2, W - 2);

		// Diag: H+W-1個のうち
		// 最初H個のどれかはmust空き
		// 最後H個のどれかはmust空き
		// 他でH連続塗りがある場合は縦の自由度が減る /2
		vector<mint> dp(H+1);
		dp[0] = f;
		for (int i = 0; i < H + W - 1; ++i) {
			vector<mint> dp_prev = dp;
			vector<mint> dp(H+1);
			for (int r = 0; r <= H; ++r) {
				if (r == 0) {
					accumulate(dp_prev.begin(), dp_prev.end(), mint(0));
				}
				else {
					if(r == H)
						dp[r] = (i == H - 1 || i == H + W - 2 ? mint(0) : (dp_prev[r - 1]+ dp_prev[r]) / 2);
					else
						dp[r] = dp_prev[r - 1];
				}
			}
		}
		return accumulate(dp.begin(), dp.begin() + H, mint(0));
	}
};

// 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(_, DiagonalColumn().countGrids(H, W));}
int main(){

CASE(0)
	int H = 2; 
	int W = 2; 
	int _ = 12; 
END
CASE(1)
	int H = 2; 
	int W = 3; 
	int _ = 37; 
END
CASE(2)
	int H = 3; 
	int W = 2; 
	int _ = 28; 
END
CASE(3)
	int H = 1; 
	int W = 5; 
	int _ = 32; 
END
CASE(4)
	int H = 6; 
	int W = 1; 
	int _ = 64; 
END
CASE(5)
	int H = 38; 
	int W = 38; 
	int _ = 173889321; 
END
/*
CASE(6)
	int H = ; 
	int W = ; 
	int _ = ; 
END
CASE(7)
	int H = ; 
	int W = ; 
	int _ = ; 
END
*/
}
// END CUT HERE