0ea7f42c5c 2012-09-02 kinaba: #include <iostream> 0ea7f42c5c 2012-09-02 kinaba: #include <sstream> 0ea7f42c5c 2012-09-02 kinaba: #include <iomanip> 0ea7f42c5c 2012-09-02 kinaba: #include <vector> 0ea7f42c5c 2012-09-02 kinaba: #include <string> 0ea7f42c5c 2012-09-02 kinaba: #include <map> 0ea7f42c5c 2012-09-02 kinaba: #include <set> 0ea7f42c5c 2012-09-02 kinaba: #include <algorithm> 0ea7f42c5c 2012-09-02 kinaba: #include <numeric> 0ea7f42c5c 2012-09-02 kinaba: #include <iterator> 0ea7f42c5c 2012-09-02 kinaba: #include <functional> 0ea7f42c5c 2012-09-02 kinaba: #include <complex> 0ea7f42c5c 2012-09-02 kinaba: #include <queue> 0ea7f42c5c 2012-09-02 kinaba: #include <stack> 0ea7f42c5c 2012-09-02 kinaba: #include <cmath> 0ea7f42c5c 2012-09-02 kinaba: #include <cassert> 0ea7f42c5c 2012-09-02 kinaba: using namespace std; 0ea7f42c5c 2012-09-02 kinaba: typedef long long LL; 0ea7f42c5c 2012-09-02 kinaba: typedef long double LD; 0ea7f42c5c 2012-09-02 kinaba: typedef complex<LD> CMP; 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: static const int MODVAL = 1234567891; 0ea7f42c5c 2012-09-02 kinaba: struct mint 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: unsigned val; 0ea7f42c5c 2012-09-02 kinaba: mint():val(0){} 0ea7f42c5c 2012-09-02 kinaba: mint(int x):val(x%MODVAL) {} 0ea7f42c5c 2012-09-02 kinaba: mint(unsigned x):val(x%MODVAL) {} 0ea7f42c5c 2012-09-02 kinaba: mint(LL x):val(x%MODVAL) {} 0ea7f42c5c 2012-09-02 kinaba: }; 0ea7f42c5c 2012-09-02 kinaba: mint& operator+=(mint& x, mint y) { return x = x.val+y.val; } 0ea7f42c5c 2012-09-02 kinaba: mint& operator-=(mint& x, mint y) { return x = x.val-y.val+MODVAL; } 0ea7f42c5c 2012-09-02 kinaba: mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } 0ea7f42c5c 2012-09-02 kinaba: mint operator+(mint x, mint y) { return x+=y; } 0ea7f42c5c 2012-09-02 kinaba: mint operator-(mint x, mint y) { return x-=y; } 0ea7f42c5c 2012-09-02 kinaba: mint operator*(mint x, mint y) { return x*=y; } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: template<typename T> 0ea7f42c5c 2012-09-02 kinaba: vector<T> MATMUL(const vector< vector<T> >& a, const vector<T>& v) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: int N = a.size(); 0ea7f42c5c 2012-09-02 kinaba: vector<T> u(N); 0ea7f42c5c 2012-09-02 kinaba: for(int i=0; i<N; ++i) 0ea7f42c5c 2012-09-02 kinaba: for(int j=0; j<N; ++j) 0ea7f42c5c 2012-09-02 kinaba: u[i] += a[i][j]*v[j]; 0ea7f42c5c 2012-09-02 kinaba: return u; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: template<typename T> 0ea7f42c5c 2012-09-02 kinaba: vector< vector<T> > MATMUL(const vector< vector<T> >& a, const vector< vector<T> >& b) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: int N = a.size(); 0ea7f42c5c 2012-09-02 kinaba: vector< vector<T> > c(N, vector<T>(N)); 0ea7f42c5c 2012-09-02 kinaba: for(int i=0; i<N; ++i) 0ea7f42c5c 2012-09-02 kinaba: for(int j=0; j<N; ++j) 0ea7f42c5c 2012-09-02 kinaba: for(int k=0; k<N; ++k) 0ea7f42c5c 2012-09-02 kinaba: c[i][j] += a[i][k]*b[k][j]; 0ea7f42c5c 2012-09-02 kinaba: return c; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: template<typename T> 0ea7f42c5c 2012-09-02 kinaba: vector< vector<T> > MATPOW(vector< vector<T> > a, LL e) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: int N = a.size(); 0ea7f42c5c 2012-09-02 kinaba: vector< vector<T> > c(N, vector<T>(N)); 0ea7f42c5c 2012-09-02 kinaba: for(int i=0; i<N; ++i) c[i][i] = 1; 0ea7f42c5c 2012-09-02 kinaba: for(; e; e>>=1) { 0ea7f42c5c 2012-09-02 kinaba: if(e&1) 0ea7f42c5c 2012-09-02 kinaba: c = MATMUL(c, a); 0ea7f42c5c 2012-09-02 kinaba: a = MATMUL(a, a); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: return c; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: class TheBrickTowerHardDivOne { public: 0ea7f42c5c 2012-09-02 kinaba: int find(int C, int K, long long H) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: vector< vector<int> > patterns; 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: vector<int> p; 0ea7f42c5c 2012-09-02 kinaba: vector<bool> used(4); 0ea7f42c5c 2012-09-02 kinaba: generate_patterns(true, 4, used, p, 0, patterns); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: const vector<int> compress = calc_compress_table(patterns).first; 0ea7f42c5c 2012-09-02 kinaba: const vector<bool> is_repr = calc_compress_table(patterns).second; 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: const int PN = patterns.size(); 0ea7f42c5c 2012-09-02 kinaba: const int CPN = *max_element(compress.begin(), compress.end())+1; 0ea7f42c5c 2012-09-02 kinaba: const int D = CPN * (K+1); 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: vector< vector<mint> > m(D+1, vector<mint>(D+1)); 0ea7f42c5c 2012-09-02 kinaba: for(int pa=0; pa<PN; ++pa) if(is_repr[pa]) 0ea7f42c5c 2012-09-02 kinaba: for(int pb=0; pb<PN; ++pb) { 0ea7f42c5c 2012-09-02 kinaba: vector<mint> tt = transition(patterns[pa], patterns[pb], K, C); 0ea7f42c5c 2012-09-02 kinaba: for(int ka=0; ka<=K; ++ka) 0ea7f42c5c 2012-09-02 kinaba: for(int kt=0; kt<=K; ++kt) { 0ea7f42c5c 2012-09-02 kinaba: int kb = ka + kt + internal(patterns[pb]); 0ea7f42c5c 2012-09-02 kinaba: if(kb <= K) 0ea7f42c5c 2012-09-02 kinaba: m[compress[pb]*(K+1)+kb][compress[pa]*(K+1)+ka] += tt[kt]; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: for(int x=0; x<D+1; ++x) 0ea7f42c5c 2012-09-02 kinaba: m[D][x] = 1; 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: vector<mint> v(D+1); 0ea7f42c5c 2012-09-02 kinaba: for(int pa=0; pa<PN; ++pa) { 0ea7f42c5c 2012-09-02 kinaba: int ka = internal(patterns[pa]); 0ea7f42c5c 2012-09-02 kinaba: if(ka <= K) 0ea7f42c5c 2012-09-02 kinaba: v[compress[pa]*(K+1)+ka] += counting(patterns[pa], C); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: v = MATMUL(MATPOW(m, H-1), v); 0ea7f42c5c 2012-09-02 kinaba: return accumulate(v.begin(), v.end(), mint(0)).val; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: // TODO: auto generate. 0ea7f42c5c 2012-09-02 kinaba: pair< vector<int>, vector<bool> > 0ea7f42c5c 2012-09-02 kinaba: calc_compress_table(const vector< vector<int> >& patterns) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: // {0} 00,00 0ea7f42c5c 2012-09-02 kinaba: // {1,2,5,9} 00,01 0ea7f42c5c 2012-09-02 kinaba: // {3,6} 00,11 0ea7f42c5c 2012-09-02 kinaba: // {4,7,12,13} 00,12 0ea7f42c5c 2012-09-02 kinaba: // {8} 01,10 0ea7f42c5c 2012-09-02 kinaba: // {10,11} 01,12 0ea7f42c5c 2012-09-02 kinaba: // {14} 01,23 0ea7f42c5c 2012-09-02 kinaba: vector<int> repr(patterns.size()); 0ea7f42c5c 2012-09-02 kinaba: vector<bool> is_repr(patterns.size(), false); 0ea7f42c5c 2012-09-02 kinaba: is_repr[0] = true; repr[0] = 0; 0ea7f42c5c 2012-09-02 kinaba: is_repr[1] = true; repr[1] = repr[2] = repr[5] = repr[9] = 1; 0ea7f42c5c 2012-09-02 kinaba: is_repr[3] = true; repr[3] = repr[6] = 2; 0ea7f42c5c 2012-09-02 kinaba: is_repr[4] = true; repr[4] = repr[7] = repr[12] = repr[13] = 3; 0ea7f42c5c 2012-09-02 kinaba: is_repr[8] = true; repr[8] = 4; 0ea7f42c5c 2012-09-02 kinaba: is_repr[10] = true; repr[10] = repr[11] = 5; 0ea7f42c5c 2012-09-02 kinaba: is_repr[14] = true; repr[14] = 6; 0ea7f42c5c 2012-09-02 kinaba: return make_pair(repr, is_repr); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: mint counting(const vector<int>& p, int C) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: return counting(set<int>(p.begin(), p.end()).size(), C); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: mint counting(int N, int C) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: if(C<0) 0ea7f42c5c 2012-09-02 kinaba: return 0; 0ea7f42c5c 2012-09-02 kinaba: mint v = 1; 0ea7f42c5c 2012-09-02 kinaba: for(int i=0; i<N; ++i) 0ea7f42c5c 2012-09-02 kinaba: v *= (C-i); 0ea7f42c5c 2012-09-02 kinaba: return v; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: int internal(const vector<int>& p) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: return (p[0]==p[1])+(p[1]==p[3])+(p[3]==p[2])+(p[2]==p[0]); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: vector<mint> transition(const vector<int>& pa, const vector<int>& pb, int K, int C) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: const int AN = set<int>(pa.begin(), pa.end()).size(); 0ea7f42c5c 2012-09-02 kinaba: const int BN = set<int>(pb.begin(), pb.end()).size(); 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: vector< vector<int> > patterns; 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: vector<int> p; 0ea7f42c5c 2012-09-02 kinaba: vector<bool> used(AN+BN, false); 0ea7f42c5c 2012-09-02 kinaba: generate_patterns(false, BN, used, p, AN, patterns); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: vector<mint> answer(K+1); 0ea7f42c5c 2012-09-02 kinaba: for(int i=0; i<patterns.size(); ++i) { 0ea7f42c5c 2012-09-02 kinaba: const vector<int>& p = patterns[i]; 0ea7f42c5c 2012-09-02 kinaba: int k = 0; 0ea7f42c5c 2012-09-02 kinaba: for(int x=0; x<4; ++x) k += (pa[x]==p[pb[x]]); 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: if(k <= K) { 0ea7f42c5c 2012-09-02 kinaba: int N = max(*max_element(p.begin(), p.end()) - (AN-1), 0); 0ea7f42c5c 2012-09-02 kinaba: answer[k] += counting(N, C-AN); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: return answer; 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: void generate_patterns(bool ALLOW_DUP, int LEN, vector<bool>& used, 0ea7f42c5c 2012-09-02 kinaba: vector<int>& p, int next, vector< vector<int> >& patterns) 0ea7f42c5c 2012-09-02 kinaba: { 0ea7f42c5c 2012-09-02 kinaba: if(p.size() == LEN) 0ea7f42c5c 2012-09-02 kinaba: patterns.push_back(p); 0ea7f42c5c 2012-09-02 kinaba: else 0ea7f42c5c 2012-09-02 kinaba: for(int v=0; v<=next; ++v) if(ALLOW_DUP || !used[v]) { 0ea7f42c5c 2012-09-02 kinaba: p.push_back(v); 0ea7f42c5c 2012-09-02 kinaba: used[v] = true; 0ea7f42c5c 2012-09-02 kinaba: generate_patterns(ALLOW_DUP, LEN, used, p, max(v+1, next), patterns); 0ea7f42c5c 2012-09-02 kinaba: used[v] = false; 0ea7f42c5c 2012-09-02 kinaba: p.pop_back(); 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: }; 0ea7f42c5c 2012-09-02 kinaba: 0ea7f42c5c 2012-09-02 kinaba: // BEGIN CUT HERE 0ea7f42c5c 2012-09-02 kinaba: #include <ctime> 0ea7f42c5c 2012-09-02 kinaba: double start_time; string timer() 0ea7f42c5c 2012-09-02 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 0ea7f42c5c 2012-09-02 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 0ea7f42c5c 2012-09-02 kinaba: { os << "{ "; 0ea7f42c5c 2012-09-02 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 0ea7f42c5c 2012-09-02 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 0ea7f42c5c 2012-09-02 kinaba: void verify_case(const int& Expected, const int& Received) { 0ea7f42c5c 2012-09-02 kinaba: bool ok = (Expected == Received); 0ea7f42c5c 2012-09-02 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 0ea7f42c5c 2012-09-02 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 0ea7f42c5c 2012-09-02 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 0ea7f42c5c 2012-09-02 kinaba: #define END verify_case(_, TheBrickTowerHardDivOne().find(C, K, H));} 0ea7f42c5c 2012-09-02 kinaba: int main(){ 0ea7f42c5c 2012-09-02 kinaba: CASE(0) 0ea7f42c5c 2012-09-02 kinaba: int C = 2; 0ea7f42c5c 2012-09-02 kinaba: int K = 0; 0ea7f42c5c 2012-09-02 kinaba: long long H = 2LL; 0ea7f42c5c 2012-09-02 kinaba: int _ = 4; 0ea7f42c5c 2012-09-02 kinaba: END 0ea7f42c5c 2012-09-02 kinaba: CASE(2) 0ea7f42c5c 2012-09-02 kinaba: int C = 2; 0ea7f42c5c 2012-09-02 kinaba: int K = 3; 0ea7f42c5c 2012-09-02 kinaba: long long H = 1LL; 0ea7f42c5c 2012-09-02 kinaba: int _ = 14; 0ea7f42c5c 2012-09-02 kinaba: END 0ea7f42c5c 2012-09-02 kinaba: CASE(3) 0ea7f42c5c 2012-09-02 kinaba: int C = 4; 0ea7f42c5c 2012-09-02 kinaba: int K = 7; 0ea7f42c5c 2012-09-02 kinaba: long long H = 47LL; 0ea7f42c5c 2012-09-02 kinaba: int _ = 1008981254; 0ea7f42c5c 2012-09-02 kinaba: END 0ea7f42c5c 2012-09-02 kinaba: CASE(4) 0ea7f42c5c 2012-09-02 kinaba: int C = 4747; 0ea7f42c5c 2012-09-02 kinaba: int K = 7; 0ea7f42c5c 2012-09-02 kinaba: long long H = 474747474747474747LL; 0ea7f42c5c 2012-09-02 kinaba: int _ = 473182063; 0ea7f42c5c 2012-09-02 kinaba: END 0ea7f42c5c 2012-09-02 kinaba: } 0ea7f42c5c 2012-09-02 kinaba: // END CUT HERE