ADDED SRM/544/1A.cpp Index: SRM/544/1A.cpp ================================================================== --- SRM/544/1A.cpp +++ SRM/544/1A.cpp @@ -0,0 +1,119 @@ +#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 ElectionFraudDiv1 { public: + int MinimumVoters(vector percentages) + { + for(int voter=1; voter<=100000; ++voter) + if(possible(voter, percentages)) + return voter; + return -1; + } + + bool possible(int V, const vector& P) + { + int L=0, R=0; + for(int i=0; i +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(_, ElectionFraudDiv1().MinimumVoters(percentages));} +int main(){ + +CASE(0) + int percentages_[] = {33, 33, 33}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = 3; +END +CASE(1) + int percentages_[] = {29, 29, 43}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = 7; +END +CASE(2) + int percentages_[] = {12, 12, 12, 12, 12, 12, 12, 12}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = -1; +END +CASE(3) + int percentages_[] = {13, 13, 13, 13, 13, 13, 13, 13}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = 8; +END +CASE(4) + int percentages_[] = {0, 1, 100}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = 200; +END +CASE(5) + int percentages_[] = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4}; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = 97; +END +/* +CASE(6) + int percentages_[] = ; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = ; +END +CASE(7) + int percentages_[] = ; + vector percentages(percentages_, percentages_+sizeof(percentages_)/sizeof(*percentages_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/544/1B.cpp Index: SRM/544/1B.cpp ================================================================== --- SRM/544/1B.cpp +++ SRM/544/1B.cpp @@ -0,0 +1,136 @@ +#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 FlipGame { public: + int minOperations(vector board) + { + int cnt = 0; + while( !all_zero(board) ) { + next(board); + ++cnt; + } + return cnt; + } + + bool all_zero(const vector & board) + { + for(int i=0; i& board) + { + int X=0; + for(int y=0; y +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(_, FlipGame().minOperations(board));} +int main(){ + +CASE(0) + string board_[] = {"1000", + "1110", + "1111"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 1; +END +CASE(1) + string board_[] = {"1111", + "1111", + "1111"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 1; +END +CASE(2) + string board_[] = {"00", + "00", + "00"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 0; +END +CASE(3) + string board_[] = { + "00000000", + "00100000", + "01000000", + "00001000", + "00000000"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 4; +END +CASE(4) + string board_[] = {"000000000000001100000000000000", + "000000000000011110000000000000", + "000000000000111111000000000000", + "000000000001111111100000000000", + "000000000011111111110000000000", + "000000000111111111111000000000", + "000000001100111111001100000000", + "000000011000011110000110000000", + "000000111100111111001111000000", + "000001111111111111111111100000", + "000011111111111111111111110000", + "000111111111000000111111111000", + "001111111111100001111111111100", + "011111111111110011111111111110", + "111111111111111111111111111111"}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = 29; +END +/* +CASE(5) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = ; +END +CASE(6) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/544/1C.cpp Index: SRM/544/1C.cpp ================================================================== --- SRM/544/1C.cpp +++ SRM/544/1C.cpp @@ -0,0 +1,175 @@ +#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; // must be prime for op/ +struct mint +{ + int val; + mint():val(0){} + mint(int x):val(x%MODVAL) {} // x>=0 + mint(size_t x):val(x%MODVAL) {} // x>=0 + mint(LL x):val(x%MODVAL) {} // x>=0 +}; +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; } +mint operator-(mint x) { return mint(0) - x; } + +vector vMul( const vector< vector >& A, const vector& B ) +{ + const int n = A.size(); + + vector C(n); + for(int i=0; i > mMul( const vector< vector >& A, const vector< vector >& B ) +{ + const int n = A.size(); + + vector< vector > C(n, vector(n)); + for(int i=0; i > mPow( vector< vector > M, LL t ) // t>0 +{ + vector< vector > R; + for(; t; t>>=1, M=mMul(M,M)) + if( t&1 ) + R = (R.empty() ? M : mMul(R,M)); + return R; +} + +class SplittingFoxes { public: + int sum(long long n, int S_, int L_, int R_) + { + mint S(S_), L(L_), R(R_); + + enum {PR, PT, PL, PB, XR, XT, XL, XB, YR, YT, YL, YB, __}; + vector< vector > M(13, vector(13)); + M[PR][PR] = S; M[PR][YR] = S; M[PR][PT] = L; M[PR][PB] = R; + M[PT][PT] = S; M[PT][XT] = S; M[PT][PL] = L; M[PT][PR] = R; + M[PL][PL] = S; M[PL][YL] =-S; M[PL][PB] = L; M[PL][PT] = R; + M[PB][PB] = S; M[PB][XB] =-S; M[PB][PR] = L; M[PB][PL] = R; + M[XR][XR] = S; M[XR][__] = S; M[XR][XT] = L; M[XR][XB] = R; + M[XT][XT] = S; M[XT][__] = 0; M[XT][XL] = L; M[XT][XR] = R; + M[XL][XL] = S; M[XL][__] =-S; M[XL][XB] = L; M[XL][XT] = R; + M[XB][XB] = S; M[XB][__] = 0; M[XB][XR] = L; M[XB][XL] = R; + M[YR][YR] = S; M[YR][__] = 0; M[YR][YT] = L; M[YR][YB] = R; + M[YT][YT] = S; M[YT][__] = S; M[YT][YL] = L; M[YT][YR] = R; + M[YL][YL] = S; M[YL][__] = 0; M[YL][YB] = L; M[YL][YT] = R; + M[YB][YB] = S; M[YB][__] =-S; M[YB][YR] = L; M[YB][YL] = R; + M[__][__] = S+L+R; + vector V(13); + V[__] = 1; + return vMul(mPow(M, n), V)[PR].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(_, SplittingFoxes().sum(n, S, L, R));} +int main(){ + +CASE(0) + long long n = 58LL; + int S = 2; + int L = 0; + int R = 0; + int _ = 0; +END +CASE(1) + long long n = 3LL; + int S = 1; + int L = 1; + int R = 0; + int _ = 1; +END +CASE(2) + long long n = 5LL; + int S = 1; + int L = 3; + int R = 2; + int _ = 34; +END +CASE(3) + long long n = 5LL; + int S = 1; + int L = 2; + int R = 3; + int _ = 999999973; +END +CASE(4) + long long n = 123456789LL; + int S = 987654321; + int L = 544; + int R = 544; + int _ = 0; +END +CASE(5) + long long n = 65536LL; + int S = 1024; + int L = 512; + int R = 4096; + int _ = 371473914; +END +/* +CASE(6) + long long n = LL; + int S = ; + int L = ; + int R = ; + int _ = ; +END +CASE(7) + long long n = LL; + int S = ; + int L = ; + int R = ; + int _ = ; +END +*/ +} +// END CUT HERE