3de32bb725 2011-11-12 kinaba: #include <iostream> 3de32bb725 2011-11-12 kinaba: #include <sstream> 3de32bb725 2011-11-12 kinaba: #include <iomanip> 3de32bb725 2011-11-12 kinaba: #include <vector> 3de32bb725 2011-11-12 kinaba: #include <string> 3de32bb725 2011-11-12 kinaba: #include <map> 3de32bb725 2011-11-12 kinaba: #include <set> 3de32bb725 2011-11-12 kinaba: #include <algorithm> 3de32bb725 2011-11-12 kinaba: #include <numeric> 3de32bb725 2011-11-12 kinaba: #include <iterator> 3de32bb725 2011-11-12 kinaba: #include <functional> 3de32bb725 2011-11-12 kinaba: #include <complex> 3de32bb725 2011-11-12 kinaba: #include <queue> 3de32bb725 2011-11-12 kinaba: #include <stack> 3de32bb725 2011-11-12 kinaba: #include <cmath> 3de32bb725 2011-11-12 kinaba: #include <cassert> 3de32bb725 2011-11-12 kinaba: #include <cstring> 3de32bb725 2011-11-12 kinaba: using namespace std; 3de32bb725 2011-11-12 kinaba: typedef long long LL; 3de32bb725 2011-11-12 kinaba: typedef complex<double> CMP; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: static const int MODVAL = 1000000007; 3de32bb725 2011-11-12 kinaba: struct mint 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: int val; 3de32bb725 2011-11-12 kinaba: mint():val(0){} 3de32bb725 2011-11-12 kinaba: mint(int x):val(x%MODVAL) {} // x>=0 3de32bb725 2011-11-12 kinaba: mint(size_t x):val(x%MODVAL) {} // x>=0 3de32bb725 2011-11-12 kinaba: mint(LL x):val(x%MODVAL) {} // x>=0 3de32bb725 2011-11-12 kinaba: }; 3de32bb725 2011-11-12 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 3de32bb725 2011-11-12 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 3de32bb725 2011-11-12 kinaba: mint operator+(mint x, mint y) { return x+=y; } 3de32bb725 2011-11-12 kinaba: mint operator*(mint x, mint y) { return x*=y; } 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: class BricksN { public: 3de32bb725 2011-11-12 kinaba: int countStructures(int w, int h, int k) 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: memo1.clear(); 3de32bb725 2011-11-12 kinaba: memo2.clear(); 3de32bb725 2011-11-12 kinaba: return rec(w, h, k).val; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: map<int,mint> memo1; 3de32bb725 2011-11-12 kinaba: mint rec(int w, int h, int k) 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: if( h == 0 ) return 1; 3de32bb725 2011-11-12 kinaba: if( w <= 0 ) return 1; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: const int key = w*51+h; 3de32bb725 2011-11-12 kinaba: if( memo1.count(key) ) 3de32bb725 2011-11-12 kinaba: return memo1[key]; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: mint sum = 1; 3de32bb725 2011-11-12 kinaba: for(int s=0; s<w; ++s) 3de32bb725 2011-11-12 kinaba: for(int ww=1; s+ww<=w; ++ww) 3de32bb725 2011-11-12 kinaba: sum += full(ww, k) * rec(ww, h-1, k) * rec(w-(s+ww+1), h, k); 3de32bb725 2011-11-12 kinaba: return memo1[key] = sum; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: map<int,mint> memo2; 3de32bb725 2011-11-12 kinaba: mint full(int w, int k) 3de32bb725 2011-11-12 kinaba: { 3de32bb725 2011-11-12 kinaba: if( w <= 0 ) return 1; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: const int key = w; 3de32bb725 2011-11-12 kinaba: if( memo2.count(key) ) 3de32bb725 2011-11-12 kinaba: return memo2[key]; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: mint sum = 0; 3de32bb725 2011-11-12 kinaba: for(int ww=1; ww<=min(w,k); ++ww) 3de32bb725 2011-11-12 kinaba: sum += full(w-ww, k); 3de32bb725 2011-11-12 kinaba: return memo2[key] = sum; 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: }; 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: // BEGIN CUT HERE 3de32bb725 2011-11-12 kinaba: #include <ctime> 3de32bb725 2011-11-12 kinaba: double start_time; string timer() 3de32bb725 2011-11-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 3de32bb725 2011-11-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 3de32bb725 2011-11-12 kinaba: { os << "{ "; 3de32bb725 2011-11-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 3de32bb725 2011-11-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 3de32bb725 2011-11-12 kinaba: void verify_case(const int& Expected, const int& Received) { 3de32bb725 2011-11-12 kinaba: bool ok = (Expected == Received); 3de32bb725 2011-11-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 3de32bb725 2011-11-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 3de32bb725 2011-11-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 3de32bb725 2011-11-12 kinaba: #define END verify_case(_, BricksN().countStructures(w, h, k));} 3de32bb725 2011-11-12 kinaba: int main(){ 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: CASE(0) 3de32bb725 2011-11-12 kinaba: int w = 3; 3de32bb725 2011-11-12 kinaba: int h = 1; 3de32bb725 2011-11-12 kinaba: int k = 3; 3de32bb725 2011-11-12 kinaba: int _ = 13; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(1) 3de32bb725 2011-11-12 kinaba: int w = 3; 3de32bb725 2011-11-12 kinaba: int h = 2; 3de32bb725 2011-11-12 kinaba: int k = 3; 3de32bb725 2011-11-12 kinaba: int _ = 83; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(2) 3de32bb725 2011-11-12 kinaba: int w = 1; 3de32bb725 2011-11-12 kinaba: int h = 5; 3de32bb725 2011-11-12 kinaba: int k = 1; 3de32bb725 2011-11-12 kinaba: int _ = 6; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(3) 3de32bb725 2011-11-12 kinaba: int w = 10; 3de32bb725 2011-11-12 kinaba: int h = 10; 3de32bb725 2011-11-12 kinaba: int k = 3; 3de32bb725 2011-11-12 kinaba: int _ = 288535435; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(4) 3de32bb725 2011-11-12 kinaba: int w = 50; 3de32bb725 2011-11-12 kinaba: int h = 50; 3de32bb725 2011-11-12 kinaba: int k = 50; 3de32bb725 2011-11-12 kinaba: int _ = -1; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: CASE(5) 3de32bb725 2011-11-12 kinaba: int w = 1; 3de32bb725 2011-11-12 kinaba: int h = 1; 3de32bb725 2011-11-12 kinaba: int k = 1; 3de32bb725 2011-11-12 kinaba: int _ = 1; 3de32bb725 2011-11-12 kinaba: END 3de32bb725 2011-11-12 kinaba: 3de32bb725 2011-11-12 kinaba: } 3de32bb725 2011-11-12 kinaba: // END CUT HERE