ADDED SRM/551-U/2C.cpp Index: SRM/551-U/2C.cpp ================================================================== --- SRM/551-U/2C.cpp +++ SRM/551-U/2C.cpp @@ -0,0 +1,124 @@ +#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 = 1000000007; +struct mint +{ + int val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} + mint(size_t 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; } + +class ColorfulCupcakesDivTwo { public: + int countArrangements(string cupcakes) + { + vector num(3); + for(int i=0; i& num) + { + return (num[0] ? rec(0, 0, num[0]-1, num[1], num[2]) : 0) + + (num[1] ? rec(1, 1, num[0], num[1]-1, num[2]) : 0) + + (num[2] ? rec(2, 2, num[0], num[1], num[2]-1) : 0); + } + + map memo; + mint rec(int F, int P, int A, int B, int C) + { + if( (A+B+C) == 1 ) + { + if(A && F!=0 && P!=0 || B && F!=1 && P!=1 || C && F!=2 && P!=2) + return 1; + else + return 0; + } + + int key = ((((F*3+P)*50+A)*50)+B)*50+C; + if( memo.count(key) ) + return memo[key]; + + mint sum = 0; + if(A && P!=0) sum += rec(F, 0, A-1, B, C); + if(B && P!=1) sum += rec(F, 1, A, B-1, C); + if(C && P!=2) sum += rec(F, 2, A, B, C-1); + return memo[key] = sum; + } +}; + +// 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(_, ColorfulCupcakesDivTwo().countArrangements(cupcakes));} +int main(){ + +CASE(0) + string cupcakes = "ABAB"; + int _ = 2; +END +CASE(1) + string cupcakes = "ABABA"; + int _ = 0; +END +CASE(2) + string cupcakes = "ABC"; + int _ = 6; +END +CASE(3) + string cupcakes = "ABABABABABABABABABABABABABABABABABABABABABABABABAB"; + int _ = 2; +END +CASE(4) + string cupcakes = "BCBABBACBABABCCCCCAABBAACBBBBCBCAAA"; + int _ = 741380640; +END +/* +CASE(5) + string cupcakes = ; + int _ = ; +END +CASE(6) + string cupcakes = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/552/1A.cpp Index: SRM/552/1A.cpp ================================================================== --- SRM/552/1A.cpp +++ SRM/552/1A.cpp @@ -0,0 +1,128 @@ +#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 FoxPaintingBalls { public: + long long theMax(long long R, long long G, long long B, int N_) + { + LL N = N_; + LL tot = N*(N+1)/2; + LL t = tot/3; + LL tr = tot%3; + // (t,t,t+tr) + + LL l=0, r=(R+G+B)/tot+1; // [l,r) + while( l+1 < r ) { + LL c = (l+r)/2; + LL rr = R-t*c; + LL gg = G-t*c; + LL bb = B-t*c; + bool can = true; + if(rr<0 || gg<0 || bb<0) + can = false; + else + can = (rr+gg+bb >= tr*c); + (can ? l : r) = c; + } + return l; + } +}; + +// 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 long long& Expected, const long long& 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(_, FoxPaintingBalls().theMax(R, G, B, N));} +int main(){ + +CASE(0) + long long R = 2LL; + long long G = 2LL; + long long B = 2LL; + int N = 3; + long long _ = 1LL; +END +CASE(1) + long long R = 1LL; + long long G = 2LL; + long long B = 3LL; + int N = 3; + long long _ = 0LL; +END +CASE(2) + long long R = 8LL; + long long G = 6LL; + long long B = 6LL; + int N = 4; + long long _ = 2LL; +END +CASE(3) + long long R = 7LL; + long long G = 6LL; + long long B = 7LL; + int N = 4; + long long _ = 2LL; +END +CASE(4) + long long R = 100LL; + long long G = 100LL; + long long B = 100LL; + int N = 4; + long long _ = 30LL; +END +CASE(5) + long long R = 19330428391852493LL; + long long G = 48815737582834113LL; + long long B = 11451481019198930LL; + int N = 3456; + long long _ = 5750952686LL; +END +CASE(6) + long long R = 1LL; + long long G = 1LL; + long long B = 1LL; + int N = 1; + long long _ = 3LL; +END +CASE(7) + long long R = 1LL; + long long G = 1LL; + long long B = 1LL; + int N = 7; + long long _ = -1LL; +END +CASE(8) + long long R = 1000000000000000000LL; + long long G = 1000000000000000000LL; + long long B = 1000000000000000000LL; + int N = 1; + long long _ = 3000000000000000000LL; +END +} +// END CUT HERE ADDED SRM/552/1B.cpp Index: SRM/552/1B.cpp ================================================================== --- SRM/552/1B.cpp +++ SRM/552/1B.cpp @@ -0,0 +1,234 @@ +#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 FoxAndFlowerShopDivOne { public: + int theMaxFlowers(vector flowers, int maxDiff) + { + int H = flowers.size(); + int W = flowers[0].size(); + vector< vector > s(H+1, vector(W+1)); + vector< vector > d(H+1, vector(W+1)); + for(int y=H-1; y>=0; --y) + for(int x=W-1; x>=0; --x) + { + s[y][x] = s[y+1][x] + s[y][x+1] - s[y+1][x+1] + (flowers[y][x]!='.'); + d[y][x] = d[y+1][x] + d[y][x+1] - d[y+1][x+1] + (flowers[y][x]=='L') - (flowers[y][x]=='P'); + } + + int answer = -1; + + for(int X=1; X d2s_L, d2s_R; + + // left + for(int y=0; y::iterator it=d2s_L.begin(); it!=d2s_L.end(); ++it) + for(map::iterator jt=d2s_R.begin(); jt!=d2s_R.end(); ++jt) + if(abs(it->first + jt->first) <= maxDiff) + answer = max(answer, it->second + jt->second); + } + + for(int Y=1; Y d2s_L, d2s_R; + + // left + for(int y=0; y::iterator it=d2s_L.begin(); it!=d2s_L.end(); ++it) + for(map::iterator jt=d2s_R.begin(); jt!=d2s_R.end(); ++jt) + if(abs(it->first + jt->first) <= maxDiff) + answer = max(answer, it->second + jt->second); + } + + return answer; + } +}; + +// 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(_, FoxAndFlowerShopDivOne().theMaxFlowers(flowers, maxDiff));} +int main(){ + +CASE(0) + string flowers_[] = {"LLL", + "PPP", + "LLL"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 1; + int _ = 7; +END +CASE(1) + string flowers_[] = {"LLL", + "PPP", + "LLL"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 0; + int _ = 6; +END +CASE(2) + string flowers_[] = {"...", + "...", + "..."}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 3; + int _ = 0; +END +CASE(3) + string flowers_[] = {"LLPL.LPP", + "PLPPPPLL", + "L.P.PLLL", + "LPL.PP.L", + ".LLL.P.L", + "PPLP..PL"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 2; + int _ = 38; +END +CASE(4) + string flowers_[] = {"LLLLLLLLLL", + "LLLLLLLLLL", + "LLLLLLLLLL", + "LLLLLLLLLL", + "LLLLLLLLLL"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 0; + int _ = -1; +END +CASE(5) + string flowers_[] = {"LLLP..LLP.PLL.LL..LP", + "L.PL.L.LLLL.LPLLPLP.", + "PLL.LL.LLL..PL...L..", + ".LPPP.PPPLLLLPLP..PP", + "LP.P.PPL.L...P.L.LLL", + "L..LPLPP.PP...PPPL..", + "PP.PLLL.LL...LP..LP.", + "PL...P.PPPL..PLP.L..", + "P.PPPLPLP.LL.L.LLLPL", + "PLLPLLP.LLL.P..P.LPL", + "..LLLPLPPPLP.P.LP.LL", + "..LP..L..LLPPP.LL.LP", + "LPLL.PLLPPLP...LL..P", + "LL.....PLL.PLL.P....", + "LLL...LPPPPL.PL...PP", + ".PLPLLLLP.LPP...L...", + "LL...L.LL.LLLPLPPPP.", + "PLPLLLL..LP.LLPLLLL.", + "PP.PLL..L..LLLPPL..P", + ".LLPL.P.PP.P.L.PLPLL"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 9; + int _ = 208; +END +CASE(6) +string flowers_[] = {"L","P"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 0; + int _ = 2; +END +CASE(7) + string flowers_[] = {"LLLP..LLP.PLL.LL..LPPLL.LL..LP", + "L.PL.L.LLLL.LPLLPLP.PLL.LL..LP", + "PLL.LL.LLL..PL...L..PLL.LL..LP", + ".LPPP.PPPLLLLPLP..PPPLL.LL..LP", + "LP.P.PPL.L...P.L.LLLPLL.LL..LP", + "L..LPLPP.PP...PPPL..PLL.LL..LP", + "PP.PLLL.LL...LP..LP.PLL.LL..LP", + "PL...P.PPPL..PLP.L..PLL.LL..LP", + "P.PPPLPLP.LL.L.LLLPLPLL.LL..LP", + "PLLPLLP.LLL.P..P.LPLPLL.LL..LP", + "..LLLPLPPPLP.P.LP.LLPLL.LL..LP", + "..LP..L..LLPPP.LL.LPPLL.LL..LP", + "LPLL.PLLPPLP...LL..PPLL.LL..LP", + ".LPPP.PPPLLLLPLP..PPPLL.LL..LP", + "LP.P.PPL.L...P.L.LLLPLL.LL..LP", + "L..LPLPP.PP...PPPL..PLL.LL..LP", + "PP.PLLL.LL...LP..LP.PLL.LL..LP", + "PL...P.PPPL..PLP.L..PLL.LL..LP", + "P.PPPLPLP.LL.L.LLLPLPLL.LL..LP", + "PLLPLLP.LLL.P..P.LPLPLL.LL..LP", + "..LLLPLPPPLP.P.LP.LLPLL.LL..LP", + "..LP..L..LLPPP.LL.LPPLL.LL..LP", + "LPLL.PLLPPLP...LL..PPLL.LL..LP", + "LL.....PLL.PLL.P....PLL.LL..LP", + "LLL...LPPPPL.PL...PPPLL.LL..LP", + ".PLPLLLLP.LPP...L...PLL.LL..LP", + "LL...L.LL.LLLPLPPPP.PLL.LL..LP", + "PLPLLLL..LP.LLPLLLL.PLL.LL..LP", + "PP.PLL..L..LLLPPL..PPLL.LL..LP", + ".LLPL.P.PP.P.L.PLPLLPLL.LL..LP"}; + vector flowers(flowers_, flowers_+sizeof(flowers_)/sizeof(*flowers_)); + int maxDiff = 0; + int _ = -1; +END +} +// END CUT HERE ADDED SRM/552/1C.cpp Index: SRM/552/1C.cpp ================================================================== --- SRM/552/1C.cpp +++ SRM/552/1C.cpp @@ -0,0 +1,111 @@ +#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 HolyNumbers { public: + vector erat(int N) + { + vector ps; + vector is_prime(N+1, true); + for(int p=2; p<=N; ++p) + if(is_prime[p]) { + ps.push_back(p); + for(int q=p+p; q<=N; q+=p) + is_prime[q] = false; + } + return ps; + } + + long long count(long long upTo, int maximalPrime) + { + vector ps = erat(maximalPrime); + return naive(ps, 0, upTo); + } + + LL naive(const vector& ps, int i, LL upto) + { + if( i==ps.size() || upto +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 long long& Expected, const long long& 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(_, HolyNumbers().count(upTo, maximalPrime));} +int main(){ + +CASE(0) + long long upTo = 10LL; + int maximalPrime = 100; + long long _ = 8LL; +END +CASE(1) + long long upTo = 10LL; + int maximalPrime = 3; + long long _ = 5LL; +END +CASE(2) + long long upTo = 123LL; + int maximalPrime = 12; + long long _ = 32LL; +END +CASE(3) + long long upTo = 123LL; + int maximalPrime = 456; + long long _ = 88LL; +END +CASE(4) + long long upTo = 123456789LL; + int maximalPrime = 12345; + long long _ = 25994500LL; +END +CASE(5) + long long upTo = 10000000000LL; + int maximalPrime = 1000000; + long long _ = -1LL; +END +CASE(6) + long long upTo = 10000000000LL; + int maximalPrime = 100; + long long _ = -1LL; +END +} +// END CUT HERE