ADDED SRM/586-U/1A.cpp Index: SRM/586-U/1A.cpp ================================================================== --- SRM/586-U/1A.cpp +++ SRM/586-U/1A.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex 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+=y; } +mint& operator*=(mint& x, mint y) { return x = LL(x.val)*y.val; } +mint operator*(mint x, mint y) { return x*=y; } + +class TrafficCongestion { public: + int theMinCars(int treeHeight) + { + vector dp(treeHeight+1); + vector sum(treeHeight+1); + dp[0] = 1; + sum[0] = dp[0]; + dp[1] = 1; + sum[1] = dp[1] + sum[0]; + for(int h=2; h<=treeHeight; ++h) + { + dp[h] = sum[h-2]*2 + 1; + sum[h] = dp[h] + sum[h-1]; + } + return dp[treeHeight].val; + } +}; + +// 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(_, TrafficCongestion().theMinCars(treeHeight));} +int main(){ + +CASE(0) + int treeHeight = 1; + int _ = 1; +END +CASE(1) + int treeHeight = 2; + int _ = 3; +END +CASE(2) + int treeHeight = 3; + int _ = 5; +END +CASE(3) + int treeHeight = 585858; + int _ = 548973404; +END +CASE(4) + int treeHeight = 5; + int _ = -1; +END +/* +CASE(5) + int treeHeight = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/586-U/1B-U.cpp Index: SRM/586-U/1B-U.cpp ================================================================== --- SRM/586-U/1B-U.cpp +++ SRM/586-U/1B-U.cpp @@ -0,0 +1,157 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex 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; } + +vector< vector > CP_; +mint C(int n, int k) { + while( CP_.size() <= n ) { + int nn = CP_.size(); + CP_.push_back(vector(nn+1,1)); + for(int kk=1; kk cardsnum, int K) + { + memo.assign(accumulate(cardsnum.begin(), cardsnum.end(), 1)*cardsnum.size(), -1); + return rec(1, 0, cardsnum, 0, K); + } + + vector memo; + int rec(int blks, int may, const vector& cards, int i, int K) + { + if(i == cards.size()) + return (K == may); + if(K +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(_, LISNumber().count(cardsnum, K));} +int main(){ +/* +CASE(0) + int cardsnum_[] = {1, 1, 1}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 2; + int _ = 4; +END +*/ +CASE(1) + int cardsnum_[] = {2}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 1; + int _ = 0; +END +CASE(2) + int cardsnum_[] = {36, 36, 36, 36, 36}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 36; + int _ = 1; +END +CASE(3) + int cardsnum_[] = {3, 2, 11, 5, 7}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 20; + int _ = 474640725; +END +CASE(4) + int cardsnum_[] = {31, 4, 15, 9, 26, 5, 35, 8, 9, 7, 9, 32, 3, 8, 4, 6, 26}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 58; + int _ = 12133719; +END +CASE(5) + int cardsnum_[] = {27, 18, 28, 18, 28, 4, 5, 9, 4, 5, 23, 5, + 36, 28, 7, 4, 7, 13, 5, 26, 6, 24, 9, 7, + 7, 5, 7, 24, 7, 9, 36, 9, 9, 9, 5, 9}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 116; + int _ = 516440918; +END +CASE(6) +int cardsnum_[] = { +36,36,36,36,36,36, +36,36,36,36,36,36, +36,36,36,36,36,36, +36,36,36,36,36,36, +36,36,36,36,36,36, +36,36,36,36,36,36}; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = 36*36; + int _ = -1; +END +/* +CASE(7) + int cardsnum_[] = ; + vector cardsnum(cardsnum_, cardsnum_+sizeof(cardsnum_)/sizeof(*cardsnum_)); + int K = ; + int _ = ; +END +*/ +} +// END CUT HERE Index: lib/numeric/modArith.cpp ================================================================== --- lib/numeric/modArith.cpp +++ lib/numeric/modArith.cpp @@ -34,11 +34,11 @@ // nCk :: O(log MODVAL) time, O(n) space. mint C(LL n, LL k) { return k<0 || n > CP_; -mint C(LL n, LL k) { +mint C(int n, int k) { while( CP_.size() <= n ) { int nn = CP_.size(); CP_.push_back(vector(nn+1,1)); for(int kk=1; kk