ADDED SRM/554/1A.cpp Index: SRM/554/1A.cpp ================================================================== --- SRM/554/1A.cpp +++ SRM/554/1A.cpp @@ -0,0 +1,98 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class TheBrickTowerEasyDivOne { public: + int find(int redCount, int redHeight, int blueCount, int blueHeight) + { + if(redHeight != blueHeight) + { + int ans = 0; + ans += min(redCount, blueCount); + ans += min(redCount-1, blueCount) + 1; + ans += min(redCount, blueCount-1) + 1; + return ans; + } + return min(redCount,blueCount)*2 + (redCount != blueCount); + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, TheBrickTowerEasyDivOne().find(redCount, redHeight, blueCount, blueHeight));} +int main(){ + +CASE(0) + int redCount = 1; + int redHeight = 2; + int blueCount = 3; + int blueHeight = 4; + int _ = 4; +END +CASE(1) + int redCount = 4; + int redHeight = 4; + int blueCount = 4; + int blueHeight = 7; + int _ = 12; +END +CASE(2) + int redCount = 7; + int redHeight = 7; + int blueCount = 4; + int blueHeight = 4; + int _ = 13; +END +CASE(3) + int redCount = 47; + int redHeight = 47; + int blueCount = 47; + int blueHeight = 47; + int _ = 94; +END +/* +CASE(4) + int redCount = ; + int redHeight = ; + int blueCount = ; + int blueHeight = ; + int _ = ; +END +CASE(5) + int redCount = ; + int redHeight = ; + int blueCount = ; + int blueHeight = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/554/1B.cpp Index: SRM/554/1B.cpp ================================================================== --- SRM/554/1B.cpp +++ SRM/554/1B.cpp @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +class TheBrickTowerMediumDivOne { public: + vector find(vector heights) + { + const int N = heights.size(); + const int best = best_for(heights); + vector used(N, false); + vector answer; + vector current; + int current_score = 0; + for(int i=0; i hh; + for(int a=0; a h) + { + sort(h.begin(), h.end()); + int best = 0; + for(int i=0; i+1 h) + { + if(h.empty()) + return 0; + + sort(h.begin(), h.end()); + int best = max(h0, h[0]); + for(int i=0; i+1 +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::const_iterator it=v.begin(); it!=v.end(); ++it) + os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } +void verify_case(const vector & Expected, const vector & 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(_, TheBrickTowerMediumDivOne().find(heights));} +int main(){ + +CASE(0) + int heights_[] = {4, 7, 5}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {0, 2, 1 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + int heights_[] = {4, 4, 4, 4, 4, 4, 4}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {0, 1, 2, 3, 4, 5, 6 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(2) + int heights_[] = {2, 3, 3, 2}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {0, 3, 1, 2 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + int heights_[] = {13, 32, 38, 25, 43, 47, 6}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + int __[] = {0, 6, 3, 1, 2, 4, 5 }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + int heights_[] = {28,36,27,33,30,18,16,23,39,31,4,40,41,41,30,43,45,16,5,26,35,19,2,40,27,3,37,46,5,34,22,25,19,6,45,6,45,36,40,13,29,25,10,23,6,3,21}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + vector _; +END +CASE(5) + int heights_[] = {7,34,5,15,43,8,36,5,42,22,37,45,46,38,40,2,3,22,35,23,14,31,26,1,43,11,24,41,21,8,11,28,46,21,42,5,9,18,4,3,47,15,32,16,26,45,34}; + vector heights(heights_, heights_+sizeof(heights_)/sizeof(*heights_)); + vector _; +END +} +// END CUT HERE ADDED SRM/554/1C.cpp Index: SRM/554/1C.cpp ================================================================== --- SRM/554/1C.cpp +++ SRM/554/1C.cpp @@ -0,0 +1,241 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef long double LD; +typedef complex CMP; + +static const int MODVAL = 1234567891; +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; } + +template +vector MATMUL(const vector< vector >& a, const vector& v) +{ + int N = a.size(); + vector u(N); + for(int i=0; i +vector< vector > MATMUL(const vector< vector >& a, const vector< vector >& b) +{ + int N = a.size(); + vector< vector > c(N, vector(N)); + for(int i=0; i +vector< vector > MATPOW(vector< vector > a, LL e) +{ + int N = a.size(); + vector< vector > c(N, vector(N)); + for(int i=0; i>=1) { + if(e&1) + c = MATMUL(c, a); + a = MATMUL(a, a); + } + return c; +} + +class TheBrickTowerHardDivOne { public: + int find(int C, int K, long long H) + { + vector< vector > patterns; + { + vector p; + vector used(4); + generate_patterns(true, 4, used, p, 0, patterns); + } + const vector compress = calc_compress_table(patterns).first; + const vector is_repr = calc_compress_table(patterns).second; + + const int PN = patterns.size(); + const int CPN = *max_element(compress.begin(), compress.end())+1; + const int D = CPN * (K+1); + + vector< vector > m(D+1, vector(D+1)); + for(int pa=0; pa tt = transition(patterns[pa], patterns[pb], K, C); + for(int ka=0; ka<=K; ++ka) + for(int kt=0; kt<=K; ++kt) { + int kb = ka + kt + internal(patterns[pb]); + if(kb <= K) + m[compress[pb]*(K+1)+kb][compress[pa]*(K+1)+ka] += tt[kt]; + } + } + for(int x=0; x v(D+1); + for(int pa=0; pa, vector > + calc_compress_table(const vector< vector >& patterns) + { + // {0} 00,00 + // {1,2,5,9} 00,01 + // {3,6} 00,11 + // {4,7,12,13} 00,12 + // {8} 01,10 + // {10,11} 01,12 + // {14} 01,23 + vector repr(patterns.size()); + vector is_repr(patterns.size(), false); + is_repr[0] = true; repr[0] = 0; + is_repr[1] = true; repr[1] = repr[2] = repr[5] = repr[9] = 1; + is_repr[3] = true; repr[3] = repr[6] = 2; + is_repr[4] = true; repr[4] = repr[7] = repr[12] = repr[13] = 3; + is_repr[8] = true; repr[8] = 4; + is_repr[10] = true; repr[10] = repr[11] = 5; + is_repr[14] = true; repr[14] = 6; + return make_pair(repr, is_repr); + } + + mint counting(const vector& p, int C) + { + return counting(set(p.begin(), p.end()).size(), C); + } + + mint counting(int N, int C) + { + if(C<0) + return 0; + mint v = 1; + for(int i=0; i& p) + { + return (p[0]==p[1])+(p[1]==p[3])+(p[3]==p[2])+(p[2]==p[0]); + } + + vector transition(const vector& pa, const vector& pb, int K, int C) + { + const int AN = set(pa.begin(), pa.end()).size(); + const int BN = set(pb.begin(), pb.end()).size(); + + vector< vector > patterns; + { + vector p; + vector used(AN+BN, false); + generate_patterns(false, BN, used, p, AN, patterns); + } + + vector answer(K+1); + for(int i=0; i& p = patterns[i]; + int k = 0; + for(int x=0; x<4; ++x) k += (pa[x]==p[pb[x]]); + + if(k <= K) { + int N = max(*max_element(p.begin(), p.end()) - (AN-1), 0); + answer[k] += counting(N, C-AN); + } + } + return answer; + } + + void generate_patterns(bool ALLOW_DUP, int LEN, vector& used, + vector& p, int next, vector< vector >& patterns) + { + if(p.size() == LEN) + patterns.push_back(p); + else + for(int v=0; v<=next; ++v) if(ALLOW_DUP || !used[v]) { + p.push_back(v); + used[v] = true; + generate_patterns(ALLOW_DUP, LEN, used, p, max(v+1, next), patterns); + used[v] = false; + p.pop_back(); + } + } +}; + +// BEGIN CUT HERE +#include +double start_time; string timer() + { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } +template ostream& operator<<(ostream& os, const vector& v) + { os << "{ "; + for(typename vector::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(_, TheBrickTowerHardDivOne().find(C, K, H));} +int main(){ +CASE(0) + int C = 2; + int K = 0; + long long H = 2LL; + int _ = 4; +END +CASE(2) + int C = 2; + int K = 3; + long long H = 1LL; + int _ = 14; +END +CASE(3) + int C = 4; + int K = 7; + long long H = 47LL; + int _ = 1008981254; +END +CASE(4) + int C = 4747; + int K = 7; + long long H = 474747474747474747LL; + int _ = 473182063; +END +} +// END CUT HERE Index: lib/numeric/modArith.cpp ================================================================== --- lib/numeric/modArith.cpp +++ lib/numeric/modArith.cpp @@ -5,32 +5,37 @@ // Verified by // - TCO10 R3 LV3 // - SRM 545 LV2 //------------------------------------------------------------- -static const int MODVAL = 1000000007; +static const unsigned MODVAL = 1000000007; struct mint { - int val; + unsigned val; mint():val(0){} - mint(int x):val(x%MODVAL) {} - mint(size_t x):val(x%MODVAL) {} - mint(LL x):val(x%MODVAL) {} + 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 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; } 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; } + +// nCk :: O(log MODVAL) time, O(n) space. vector FAC_(1,1); mint FAC(LL n) { while( FAC_.size()<=n ) FAC_.push_back( FAC_.back()*FAC_.size() ); return FAC_[n]; } mint C(LL n, LL k) { return k<0 || n > CP_; // Pascal's triangle: if O(1) nCk is needed. + +// nCk :: O(1) time, O(n^2) space. +vector< vector > CP_; mint C(LL n, LL k) { while( CP_.size() <= n ) { int nn = CP_.size(); CP_.push_back(vector(nn+1,1)); for(int kk=1; kk