ADDED SRM/540-U/1A.cpp Index: SRM/540-U/1A.cpp ================================================================== --- SRM/540-U/1A.cpp +++ SRM/540-U/1A.cpp @@ -0,0 +1,137 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class ImportantSequence { public: + int getCount(vector B, string operators) + { + vector< pair > A; + A.push_back( make_pair(1,0) ); + for(int i=0; i 0 + if( a == +1 ) { + // x > -b + xMin = max(xMin, -b+1); + } else { + // -x > -b == x < b + xMax = min(xMax, b-1); + } + } + + if( xMax == INF ) + return -1; + return (int)max(0LL, xMax-xMin+1); + } +}; + +// 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(_, ImportantSequence().getCount(B, operators));} +int main(){ + +CASE(0) + int B_[] = {3, -1, 7}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "+-+"; + int _ = 2; +END +CASE(1) + int B_[] = {1}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "-"; + int _ = -1; +END +CASE(2) + int B_[] = {1}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "+"; + int _ = 0; +END +CASE(3) + int B_[] = {10}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "+"; + int _ = 9; +END +CASE(4) + int B_[] = {540, 2012, 540, 2012, 540, 2012, 540}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "-+-+-+-"; + int _ = 1471; +END +CASE(5) + int B_[] = {1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000, 1000000000, 1000000000, 1000000000, 1000000000, + 1000000000}; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = "------------------------------+"; + int _ = -1; +END +/* +CASE(6) + int B_[] = ; + vector B(B_, B_+sizeof(B_)/sizeof(*B_)); + string operators = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/540-U/1B.cpp Index: SRM/540-U/1B.cpp ================================================================== --- SRM/540-U/1B.cpp +++ SRM/540-U/1B.cpp @@ -0,0 +1,250 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#ifdef __GNUC__ +#include +#define unordered_map __gnu_cxx::hash_map +#else +#include +#endif +using namespace std; +typedef long long LL; +typedef complex CMP; + +class RandomColoring { public: + double getProbability(int N, int maxR, int maxG, int maxB, int startR, int startG, int startB, int d1, int d2) + { + const int dx = d1-1; + + vector< vector< vector > > p(maxR, vector >(maxG, vector(maxB))); + p[startR][startG][startB] = 1.0; + + for(int i=1; i > > low = p; + for(int rgb=0; rgb<=maxR+maxG+maxB-3; ++rgb) + for(int r=0; r > > q = p; + for(int r=0; r > > low = p; + for(int rgb=0; rgb<=maxR+maxG+maxB-3; ++rgb) + for(int r=0; r > >& low, + int r, int R, int g, int G, int b, int B) { + r = max(0, min(low.size()-1, r)); + R = max(0, min(low.size()-1, R)); + g = max(0, min(low[0].size()-1, g)); + G = max(0, min(low[0].size()-1, G)); + b = max(0, min(low[0][0].size()-1, b)); + B = max(0, min(low[0][0].size()-1, B)); + return low[R][G][B] + - (r ? low[r-1][G][B] : 0) + - (g ? low[R][g-1][B] : 0) + - (b ? low[R][G][b-1] : 0) + + (r&&g ? low[r-1][g-1][B] : 0) + + (g&&b ? low[R][g-1][b-1] : 0) + + (b&&r ? low[r-1][G][b-1] : 0) + - (r&&g&&b ? low[r-1][g-1][b-1] : 0); + } + + int cnt(int maxR, int maxG, int maxB, + int r, int R, int g, int G, int b, int B) { + r = max(0, min(maxR-1, r)); + R = max(0, min(maxR-1, R)); + g = max(0, min(maxG-1, g)); + G = max(0, min(maxG-1, G)); + b = max(0, min(maxB-1, b)); + B = max(0, min(maxB-1, B)); + return (R-r+1)*(G-g+1)*(B-b+1); + } +}; + +// 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 double& Expected, const double& Received) { + bool ok = (abs(Expected - Received) < 1e-9); + 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(_, RandomColoring().getProbability(N, maxR, maxG, maxB, startR, startG, startB, d1, d2));} +int main(){ + +CASE(0) + int N = 2; + int maxR = 5; + int maxG = 1; + int maxB = 1; + int startR = 2; + int startG = 0; + int startB = 0; + int d1 = 0; + int d2 = 1; + double _ = 0.0; +END +CASE(1) + int N = 3; + int maxR = 5; + int maxG = 1; + int maxB = 1; + int startR = 2; + int startG = 0; + int startB = 0; + int d1 = 0; + int d2 = 1; + double _ = 0.22222222222222227; +END +CASE(2) + int N = 7; + int maxR = 4; + int maxG = 2; + int maxB = 2; + int startR = 0; + int startG = 0; + int startB = 0; + int d1 = 3; + int d2 = 3; + double _ = 1.0; +END +CASE(3) + int N = 10; + int maxR = 7; + int maxG = 8; + int maxB = 9; + int startR = 1; + int startG = 2; + int startB = 3; + int d1 = 0; + int d2 = 10; + double _ = 0.0; +END +CASE(4) + int N = 10; + int maxR = 7; + int maxG = 8; + int maxB = 9; + int startR = 1; + int startG = 2; + int startB = 3; + int d1 = 4; + int d2 = 10; + double _ = 0.37826245943967396; +END +CASE(5) + int N = 3; + int maxR = 3; + int maxG = 2; + int maxB = 2; + int startR = 1; + int startG = 0; + int startB = 0; + int d1 = 1; + int d2 = 2; + double _ = 0.09090909090909148; +END +/* +CASE(6) + int N = ; + int maxR = ; + int maxG = ; + int maxB = ; + int startR = ; + int startG = ; + int startB = ; + int d1 = ; + int d2 = ; + double _ = ; +END +CASE(7) + int N = ; + int maxR = ; + int maxG = ; + int maxB = ; + int startR = ; + int startG = ; + int startB = ; + int d1 = ; + int d2 = ; + double _ = ; +END +*/ +} +// END CUT HERE