ADDED SRM/541-U/1A.cpp Index: SRM/541-U/1A.cpp ================================================================== --- SRM/541-U/1A.cpp +++ SRM/541-U/1A.cpp @@ -0,0 +1,185 @@ +#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 AntsMeet { public: + int countAnts(vector x, vector y, string direction) + { + const int N = x.size(); + + map< int, vector< pair > > bye; + for(int i=0; i dead; + for(map > >::iterator it=bye.begin(); it!=bye.end(); ++it) { + set die; + vector< pair >& events = it->second; + for(int i=0; i= 0 ); + } + } + + int dx_of(char d) + { + if(d=='E') return +1; + if(d=='W') return -1; + return 0; + } + + int dy_of(char d) + { + if(d=='N') return +1; + if(d=='S') return -1; + return 0; + } +}; + +// 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(_, AntsMeet().countAnts(x, y, direction));} +int main(){ + +CASE(0) + int x_[] = {0,10,20,30}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,10,20,30}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "NWNE"; + int _ = 2; +END +CASE(1) + int x_[] = {-10,0,0,10}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0,-10,10,0}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "NEWS"; + int _ = 0; +END +CASE(2) + int x_[] = {-1,-1,-1,0,0,0,1,1,1}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {-1,0,1,-1,0,1,-1,0,1}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "ESEWNNEWW"; + int _ = 4; +END +CASE(3) + int x_[] = {4,7,6,2,6,5,7,7,8,4,7,8,8,8,5,4,8,9,1,5,9,3,4,0,0,1,0,7,2,6,9,6,3,0,5,5,1,2,0,4,9,7,7,1,8,1,9,2,7,3}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {2,3,0,6,8,4,9,0,5,0,2,4,3,8,1,5,0,7,3,7,0,9,8,1,9,4,7,8,1,1,6,6,6,2,8,5,1,9,0,1,1,1,7,0,2,5,4,7,5,3}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "SSNWSWSENSWSESWEWSWSENWNNNESWSWSWWSSWEEWWNWWWNWENN" ; + int _ = 25; +END +CASE(4) + int x_[] = {478,-664,759,434,-405,513,565,-396,311,-174,56,993,251,-341,993,-112,242,129,383,513,-78,-341,-148,129,423 +,493,434,-405,478,-148,929,251,56,242,929,-78,423,-664,802,251,759,383,-112,-591,-591,-248,660,660,735,493}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {-186,98,948,795,289,-678,948,-170,-195,290,-354,-424,289,-157,-166,150,706,-678,684,-294,-234,36,36,-294,-216 +,-234,427,945,265,-157,265,715,275,715,-186,337,798,-170,427,706,754,961,286,-216,798,286,961,684,-424,337}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "WNSNNSSWWWEENWESNSWSWSEWWEWEWWWNWESNSSNNSNNWWWNESE"; + int _ = 44; +END +CASE(5) + int x_[] = {0, 1, 0, 10, 9, 11}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = {0, 1, 2, 10, 11, 11}; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = "NWSNES"; + int _ = 0; +END +/* +CASE(6) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int y_[] = ; + vector y(y_, y_+sizeof(y_)/sizeof(*y_)); + string direction = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/541-U/1B.cpp Index: SRM/541-U/1B.cpp ================================================================== --- SRM/541-U/1B.cpp +++ SRM/541-U/1B.cpp @@ -0,0 +1,169 @@ +#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; + +static const int MODVAL = 1000000007; + +class AkariDaisukiDiv1 { public: + int countF(string W, string A, string D, string S, string F, int k) + { + string X = S; + + while(k>0 && X.size()<=F.size()) { + X = W + X + A + X + D; + --k; + } + + int Usushio = naive(X, F, 0, X.size()); + if( k == 0 ) + return Usushio; + + string WW = "", DD=""; + for(int n=0,a,b,c; n +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(_, AkariDaisukiDiv1().countF(Waai, Akari, Daisuki, S, F, k));} +int main(){ + +CASE(0) + string Waai = "a"; + string Akari = "b"; + string Daisuki = "c"; + string S = "x"; + string F = "axb"; + int k = 2; + int _ = 2; +END +CASE(1) + string Waai = "a"; + string Akari = "b"; + string Daisuki = "c"; + string S = "x"; + string F = "abcdefghij"; + int k = 1; + int _ = 0; +END +CASE(2) + string Waai = "a"; + string Akari = "a"; + string Daisuki = "a"; + string S = "b"; + string F = "aba"; + int k = 2; + int _ = 4; +END +CASE(3) + string Waai = "a"; + string Akari = "b"; + string Daisuki = "c"; + string S = "d"; + string F = "baadbdcbadbdccccbaaaadbdcbadbdccbaadbdcba"; + int k = 58; + int _ = 191690599; +END +CASE(4) + string Waai = "a"; + string Akari = "x"; + string Daisuki = "y"; + string S = "b"; + string F = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaab"; + int k = 49; + int _ = 1; +END +CASE(5) + string Waai = "waai"; + string Akari = "akari"; + string Daisuki = "daisuki"; + string S = "usushio"; + string F = "id"; + int k = 10000000; + int _ = 127859200; +END +CASE(6) + string Waai = "vfsebgjfyfgerkqlr"; + string Akari = "ezbiwls"; + string Daisuki = "kjerx"; + string S = "jbmjvaawoxycfndukrjfg"; + string F = "bgjfyfgerkqlrvfsebgjfyfgerkqlrvfsebgjfyfgerkqlrvfs"; + int k = 1575724; + int _ = 483599313; +END +/* +CASE(7) + string Waai = ; + string Akari = ; + string Daisuki = ; + string S = ; + string F = ; + int k = ; + int _ = ; +END +CASE(8) + string Waai = ; + string Akari = ; + string Daisuki = ; + string S = ; + string F = ; + int k = ; + int _ = ; +END +*/ +} +// END CUT HERE DELETED SRM/TCO12-1/1A.cpp Index: SRM/TCO12-1/1A.cpp ================================================================== --- SRM/TCO12-1/1A.cpp +++ SRM/TCO12-1/1A.cpp @@ -1,118 +0,0 @@ -#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 EllysJuice { public: - vector getWinners(vector players) - { - set winners; - - sort(players.begin(), players.end()); - do - winners.insert( play(players) ); - while( next_permutation(players.begin(), players.end()) ); - - winners.erase(""); - return vector(winners.begin(), winners.end()); - } - - string play(const vector& p) - { - int juice[2] = {65536, 65536}; - map total; - for(int i=0; i::iterator it=total.begin(); it!=total.end(); ++it) - if( it->second > best ) { - best_name = it->first; - best = it->second; - tie = false; - } - else if( it->second == best ) - tie = true; - return tie ? "" : best_name; - } -}; - -// 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 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(_, EllysJuice().getWinners(players));} -int main(){ - -CASE(0) - string players_[] = { "elly", "kriss", "stancho", "elly", "stancho" }; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - string __[] = {"elly", "stancho" }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(1) - string players_[] = {"torb", "elly", "stancho", "kriss"}; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - vector _; -END -CASE(2) - string players_[] = {"elly", "elly", "elly", "elly", "elly"}; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - string __[] = {"elly" }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(3) - string players_[] = { "ariadne", "elly", "ariadne", "stancho", "stancho", "kriss", "stancho", "elly" }; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - string __[] = {"ariadne", "elly", "stancho" }; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -CASE(4) - string players_[] = {"b","b","a","a"}; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - string __[] = {"a","b"}; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -/* -CASE(5) - string players_[] = ; - vector players(players_, players_+sizeof(players_)/sizeof(*players_)); - string __[] = ; - vector _(__, __+sizeof(__)/sizeof(*__)); -END -*/ -} -// END CUT HERE DELETED SRM/TCO12-1/1B.cpp Index: SRM/TCO12-1/1B.cpp ================================================================== --- SRM/TCO12-1/1B.cpp +++ SRM/TCO12-1/1B.cpp @@ -1,103 +0,0 @@ -#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 EllysFractions { public: - long long getCount(int N) - { - LL total = 0; - for(int k=2; k<=N; ++k) - total += getExact(k); - return total; - } - - LL getExact(int N) - { - int np = 0; - for(int p=2; p<=N; ++p) - if(is_prime(p)) - ++np; - LL v = 1; - 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 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(_, EllysFractions().getCount(N));} -int main(){ - -CASE(0) - int N = 1; - long long _ = 0LL; -END -CASE(1) - int N = 2; - long long _ = 1LL; -END -CASE(2) - int N = 3; - long long _ = 3LL; -END -CASE(3) - int N = 5; - long long _ = 9LL; -END -CASE(4) - int N = 100; - long long _ = 177431885LL; -END -CASE(5) - int N = 250; - long long _ = -1LL; -END -CASE(6) - int N = 4; - long long _ = 5LL; -END -} -// END CUT HERE DELETED SRM/TCO12-1/1C.cpp Index: SRM/TCO12-1/1C.cpp ================================================================== --- SRM/TCO12-1/1C.cpp +++ SRM/TCO12-1/1C.cpp @@ -1,273 +0,0 @@ -#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; - -struct UnionFind -{ - vector uf, sz; - int nc; - - UnionFind(int N): uf(N), sz(N,1), nc(N) - { for(int i=0; i= sz[b] ) swap(a, b); - uf[a] = b; - sz[b] += sz[a]; - --nc; - } - return (a!=b); - } -}; - -class EllysLights { public: - int getMinimum(string initial, vector switches) - { - int N = switches.size(); - UnionFind uf(N*2+2); - #define TR 0 - #define FL 1 - #define POS(x) ((x)+2) - #define NEG(x) ((x)+N+2) - - for(int i=0; i sw; - for(int k=0; k done; - for(int k=0; k pos(1, k); - vector neg; - for(int j=k+1; j -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(_, EllysLights().getMinimum(initial, switches));} -int main(){ - -CASE(0) - string initial = "YNYNNN"; - string switches_[] = {"YNNYNY", "NYYYNN", "YNYNYN", "NNNNYN", "NYNNNY"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = 2; -END -CASE(1) - string initial = "YNYNYN"; - string switches_[] = {"NNNNNN", "YYYYYY", "NYNNNN", "NNNYNN", "NNNNNY"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = 4; -END -CASE(2) - string initial = "YYN"; - string switches_[] = {"YNY", "NYN"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = -1; -END -CASE(3) - string initial = "NNYNYNYYYNNYYYYN"; - string switches_[] = {"NYNYNYNYNYNYNYNY", - "YNYNYNYNYNYNYNYN", - "NNNNNNNNNNNNNNNN", - "YNNNNNNNNNNNNNNN", - "NYNNNNNNNNNNNNNN", - "NNYNNNNNNNNNNNNN", - "NNNYNNNNNNNNNNNN", - "NNNNYNNNNNNNNNNN", - "NNNNNYNNNNNNNNNN", - "NNNNNNYNNNNNNNNN", - "NNNNNNNYNNNNNNNN", - "NNNNNNNNYNNNNNNN", - "NNNNNNNNNYNNNNNN", - "NNNNNNNNNNYNNNNN", - "NNNNNNNNNNNYNNNN", - "NNNNNNNNNNNNYNNN", - "NNNNNNNNNNNNNYNN", - "NNNNNNNNNNNNNNYN", - "NNNNNNNNNNNNNNNY"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = 6; -END -CASE(4) - string initial = "NYNYNYYYNNYYYNNYNNYYYYYNNYNYYYY"; - string switches_[] = {"NNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", - "NNNNNNNNYNNNYNNNNYYNYNNNNYNNNNN", - "NNNNNNNNNYNNNNNNNNNNNNYNNNNNNNN", - "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNN", - "NYNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNYYNNNNNNNNNNNNNNNY", - "NNNNNNYNNNNNNNNNNNNYNNNNNYNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "YNNNNNNNNNNNNNNNNNNYNNNNNNNNNNN", - "NNNYNNNNNNNNNNNNNNNNNNNYYNNNNNN", - "NYNNNNNNNNNNYNNNNNNNNNNNNNNNYNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNY", - "NNNNNNNNNNYNNNNNNNNNYYYNNNNNNNN", - "NNNYNNNNNNNNNNNNNNNNNNNNNNNYNNN", - "NNNNNNNNYNNNNNNNNNNNNNNNYNNNNNN", - "YNNNYNNNNNNNNNNNNNNNNNNNNNNYNNN", - "NNNNNNNNNNYNNNNNNNNNNNNNNNNNNNN", - "NNNNYNNYNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNYNNNYNNNYNNNNNNNNNNNNNYN"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = 7; -END -CASE(5) - string initial = "NYNYYNYNYYYYNNYNYNNYYNNNNNYNYNNNNNYNNNYN"; - string switches_[] = {"NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNYNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNN", - "NNNNNNNNNYNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNYNNNNYNNNNNNNYNNNNNNN", - "NNNNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNYNNNNNNNNYNNNYNNNNNYNN", - "NNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNYNNYNNNNN", - "NNNNNYNNYNNYNNNNNNNNNNNNNNNNNNNNNYNNNNNN", - "YNNNYNNNNNNNNNNNNNYNNNYNNYNNNNNNNYNNNNNN", - "NNNNNNNNNYYNNNNNNNNNNNNYNNNNYNNNNNNNNNNN", - "NNNNNNNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNY", - "NNNNNNNNNNNNYNNNNNNNNNNNYNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNN", - "NNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNYNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", - "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNNY", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNYNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNN", - "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNN", - "NYNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNN", - "NNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NYNNNNYNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", - "NNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNN", - "NNNNNNNNNNNNYNNYYNNNNNNNNNNNNNNNNNNNNNNN", - "NNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", - "NNNYNNNNNNNNNNNNNNNNYYNNNNNNNNNNNNNNNNNN", - "NNNNNNNNYNNNNNNNNNNNNNNNNNNNYNYNNNNNNNNN", - "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNYNNNNNNYNNN"}; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = -1; -END -/* -CASE(6) - string initial = ; - string switches_[] = ; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = ; -END -CASE(7) - string initial = ; - string switches_[] = ; - vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); - int _ = ; -END -*/ -} -// END CUT HERE ADDED SRM/TCO12-1A/1A.cpp Index: SRM/TCO12-1A/1A.cpp ================================================================== --- SRM/TCO12-1A/1A.cpp +++ SRM/TCO12-1A/1A.cpp @@ -0,0 +1,118 @@ +#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 EllysJuice { public: + vector getWinners(vector players) + { + set winners; + + sort(players.begin(), players.end()); + do + winners.insert( play(players) ); + while( next_permutation(players.begin(), players.end()) ); + + winners.erase(""); + return vector(winners.begin(), winners.end()); + } + + string play(const vector& p) + { + int juice[2] = {65536, 65536}; + map total; + for(int i=0; i::iterator it=total.begin(); it!=total.end(); ++it) + if( it->second > best ) { + best_name = it->first; + best = it->second; + tie = false; + } + else if( it->second == best ) + tie = true; + return tie ? "" : best_name; + } +}; + +// 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 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(_, EllysJuice().getWinners(players));} +int main(){ + +CASE(0) + string players_[] = { "elly", "kriss", "stancho", "elly", "stancho" }; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + string __[] = {"elly", "stancho" }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(1) + string players_[] = {"torb", "elly", "stancho", "kriss"}; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + vector _; +END +CASE(2) + string players_[] = {"elly", "elly", "elly", "elly", "elly"}; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + string __[] = {"elly" }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(3) + string players_[] = { "ariadne", "elly", "ariadne", "stancho", "stancho", "kriss", "stancho", "elly" }; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + string __[] = {"ariadne", "elly", "stancho" }; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +CASE(4) + string players_[] = {"b","b","a","a"}; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + string __[] = {"a","b"}; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +/* +CASE(5) + string players_[] = ; + vector players(players_, players_+sizeof(players_)/sizeof(*players_)); + string __[] = ; + vector _(__, __+sizeof(__)/sizeof(*__)); +END +*/ +} +// END CUT HERE ADDED SRM/TCO12-1A/1B.cpp Index: SRM/TCO12-1A/1B.cpp ================================================================== --- SRM/TCO12-1A/1B.cpp +++ SRM/TCO12-1A/1B.cpp @@ -0,0 +1,103 @@ +#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 EllysFractions { public: + long long getCount(int N) + { + LL total = 0; + for(int k=2; k<=N; ++k) + total += getExact(k); + return total; + } + + LL getExact(int N) + { + int np = 0; + for(int p=2; p<=N; ++p) + if(is_prime(p)) + ++np; + LL v = 1; + 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 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(_, EllysFractions().getCount(N));} +int main(){ + +CASE(0) + int N = 1; + long long _ = 0LL; +END +CASE(1) + int N = 2; + long long _ = 1LL; +END +CASE(2) + int N = 3; + long long _ = 3LL; +END +CASE(3) + int N = 5; + long long _ = 9LL; +END +CASE(4) + int N = 100; + long long _ = 177431885LL; +END +CASE(5) + int N = 250; + long long _ = -1LL; +END +CASE(6) + int N = 4; + long long _ = 5LL; +END +} +// END CUT HERE ADDED SRM/TCO12-1A/1C.cpp Index: SRM/TCO12-1A/1C.cpp ================================================================== --- SRM/TCO12-1A/1C.cpp +++ SRM/TCO12-1A/1C.cpp @@ -0,0 +1,273 @@ +#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; + +struct UnionFind +{ + vector uf, sz; + int nc; + + UnionFind(int N): uf(N), sz(N,1), nc(N) + { for(int i=0; i= sz[b] ) swap(a, b); + uf[a] = b; + sz[b] += sz[a]; + --nc; + } + return (a!=b); + } +}; + +class EllysLights { public: + int getMinimum(string initial, vector switches) + { + int N = switches.size(); + UnionFind uf(N*2+2); + #define TR 0 + #define FL 1 + #define POS(x) ((x)+2) + #define NEG(x) ((x)+N+2) + + for(int i=0; i sw; + for(int k=0; k done; + for(int k=0; k pos(1, k); + vector neg; + for(int j=k+1; j +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(_, EllysLights().getMinimum(initial, switches));} +int main(){ + +CASE(0) + string initial = "YNYNNN"; + string switches_[] = {"YNNYNY", "NYYYNN", "YNYNYN", "NNNNYN", "NYNNNY"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = 2; +END +CASE(1) + string initial = "YNYNYN"; + string switches_[] = {"NNNNNN", "YYYYYY", "NYNNNN", "NNNYNN", "NNNNNY"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = 4; +END +CASE(2) + string initial = "YYN"; + string switches_[] = {"YNY", "NYN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = -1; +END +CASE(3) + string initial = "NNYNYNYYYNNYYYYN"; + string switches_[] = {"NYNYNYNYNYNYNYNY", + "YNYNYNYNYNYNYNYN", + "NNNNNNNNNNNNNNNN", + "YNNNNNNNNNNNNNNN", + "NYNNNNNNNNNNNNNN", + "NNYNNNNNNNNNNNNN", + "NNNYNNNNNNNNNNNN", + "NNNNYNNNNNNNNNNN", + "NNNNNYNNNNNNNNNN", + "NNNNNNYNNNNNNNNN", + "NNNNNNNYNNNNNNNN", + "NNNNNNNNYNNNNNNN", + "NNNNNNNNNYNNNNNN", + "NNNNNNNNNNYNNNNN", + "NNNNNNNNNNNYNNNN", + "NNNNNNNNNNNNYNNN", + "NNNNNNNNNNNNNYNN", + "NNNNNNNNNNNNNNYN", + "NNNNNNNNNNNNNNNY"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = 6; +END +CASE(4) + string initial = "NYNYNYYYNNYYYNNYNNYYYYYNNYNYYYY"; + string switches_[] = {"NNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", + "NNNNNNNNYNNNYNNNNYYNYNNNNYNNNNN", + "NNNNNNNNNYNNNNNNNNNNNNYNNNNNNNN", + "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNN", + "NYNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNYYNNNNNNNNNNNNNNNY", + "NNNNNNYNNNNNNNNNNNNYNNNNNYNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "YNNNNNNNNNNNNNNNNNNYNNNNNNNNNNN", + "NNNYNNNNNNNNNNNNNNNNNNNYYNNNNNN", + "NYNNNNNNNNNNYNNNNNNNNNNNNNNNYNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNYNNNNNNNNNNNNNNNNNNNNNNNNY", + "NNNNNNNNNNYNNNNNNNNNYYYNNNNNNNN", + "NNNYNNNNNNNNNNNNNNNNNNNNNNNYNNN", + "NNNNNNNNYNNNNNNNNNNNNNNNYNNNNNN", + "YNNNYNNNNNNNNNNNNNNNNNNNNNNYNNN", + "NNNNNNNNNNYNNNNNNNNNNNNNNNNNNNN", + "NNNNYNNYNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNYNNNYNNNYNNNNNNNNNNNNNYN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = 7; +END +CASE(5) + string initial = "NYNYYNYNYYYYNNYNYNNYYNNNNNYNYNNNNNYNNNYN"; + string switches_[] = {"NNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNYNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNN", + "NNNNNNNNNYNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNYNNNNYNNNNNNNYNNNNNNN", + "NNNNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNYNNNNNNNNYNNNYNNNNNYNN", + "NNNNNNNNNNYNNNNNNNNNNNNNNYNNNNNYNNYNNNNN", + "NNNNNYNNYNNYNNNNNNNNNNNNNNNNNNNNNYNNNNNN", + "YNNNYNNNNNNNNNNNNNYNNNYNNYNNNNNNNYNNNNNN", + "NNNNNNNNNYYNNNNNNNNNNNNYNNNNYNNNNNNNNNNN", + "NNNNNNNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNNNY", + "NNNNNNNNNNNNYNNNNNNNNNNNYNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNNNNNNN", + "NNNYNNNNNNNNNNNNNNNNNYNNNNNNNNNNYNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNYNNYNNNNNNNNNNNNNNNNNNNNNN", + "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYYNNY", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNYNNNNNYNYNNNNNNNNNNNNNNNNNNNNNNNN", + "NNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNN", + "NYNNNNNNNNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNN", + "NNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NYNNNNYNNNNNNNNNNNNNNNNNNNNYNNNNNNNNNNNN", + "NNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNYNN", + "NNNNNNNNNNNNYNNYYNNNNNNNNNNNNNNNNNNNNNNN", + "NNNNNNNYNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN", + "NNNYNNNNNNNNNNNNNNNNYYNNNNNNNNNNNNNNNNNN", + "NNNNNNNNYNNNNNNNNNNNNNNNNNNNYNYNNNNNNNNN", + "NNNNNNNNNNNNNNNNNNNNNNNNNNYNNYNNNNNNYNNN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = -1; +END +/* +CASE(6) + string initial = ; + string switches_[] = ; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = ; +END +CASE(7) + string initial = ; + string switches_[] = ; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/TCO12-2A-U/1A.cpp Index: SRM/TCO12-2A-U/1A.cpp ================================================================== --- SRM/TCO12-2A-U/1A.cpp +++ SRM/TCO12-2A-U/1A.cpp @@ -0,0 +1,178 @@ +#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; + +typedef int vert; +typedef vert edge; +typedef vector edges; +typedef vector graph; + +bool augment( graph& G, int v, vector& matchTo, bool visited[] ) +{ + for(int i=0; i +int biMatch( graph& G, int L ) +{ + vector matchTo(G.size(), -1); + int ans = 0; + for(vert v=0; v switches, vector lamps) + { + const int N = switches[0].size(); + const int E = switches.size(); + + vector< vector > uneq(N, vector(N)); + + vector id(N, 1); + for(int e=0; e biMatch<100>(G, N) ) + return -1; + + map sizes; + int maxSize = 1; + for(int s=0; s +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(_, SwitchesAndLamps().theMin(switches, lamps));} +int main(){ + +CASE(0) + string switches_[] = {"NYNN", "NNYN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = {"NNNY", "NNYN"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = 1; +END +CASE(1) + string switches_[] = {"YYN", "YNY", "YYN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = {"YNY", "NYY", "YNY"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = 0; +END +CASE(2) + string switches_[] = {"NYYYNYNNYYYNYNNNNY", + "NYYYNYNNYYYNYNNNNY"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = {"YYYNNNYNNYNYNYNYNY", + "YYYNNNYNNYNYYNNYNY"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = -1; +END +CASE(3) + string switches_[] = {"YYNNN", "NNYYN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = {"NYNNY", "NNNYY"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = -1; +END +CASE(4) + string switches_[] = {"YNNYNNYNYY", "NYNNYNYNYN", "YNYNYYYYYN", "NNYYNYNYNN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = {"NYYNYNNYNY", "NYYYNNYNNN", "YYYYNYNNYY", "YNNNNYNYYN"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = 2; +END +CASE(5) +string switches_[] = {"YYNNN", "YNNNN", "NYNNN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); +string lamps_[] = {"YYNNN", "YNNNN", "NNYNN"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = -1; +END +CASE(6) +string switches_[] = {"YYNNN", "YNNNN", "NYNNN"}; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); +string lamps_[] = {"YYNNN", "YNNNN", "YNNNN"}; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = -1; +END +/* +CASE(6) + string switches_[] = ; + vector switches(switches_, switches_+sizeof(switches_)/sizeof(*switches_)); + string lamps_[] = ; + vector lamps(lamps_, lamps_+sizeof(lamps_)/sizeof(*lamps_)); + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/TCO12-2A-U/1B.cpp Index: SRM/TCO12-2A-U/1B.cpp ================================================================== --- SRM/TCO12-2A-U/1B.cpp +++ SRM/TCO12-2A-U/1B.cpp @@ -0,0 +1,142 @@ +#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; + +template +struct DP3 +{ + int N1, N2, N3; + vector data; + DP3(int N1, int N2, int N3, const T& t = T()) + : N1(N1), N2(N2), N3(N3), data(N1*N2*N3, t) { assert(data.size()*sizeof(T)<(1<<26)); } + T& operator()(int i1, int i2, int i3) + { return data[ ((i1*N2)+i2)*N3+i3 ]; } + void swap(DP3& rhs) + { data.swap(rhs.data); } +}; + +class CucumberWatering { public: + long long theMin(vector x, int K) + { + const int N = x.size(); + + vector > pts; + for(int i=0; i dp(N, K+1, N, -12345678987654321LL); + dp(0, 0, 0) = dp(0, 1, 0) = 0; + for(int i=1; i 1 ) + for(int p=0; p +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(_, CucumberWatering().theMin(x, K));} +int main(){ + +CASE(0) + int x_[] = {0, 6, 8, 2}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = 2; + long long _ = 6LL; +END +CASE(1) + int x_[] = {-1000000000, 1000000000, 0}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = 1; + long long _ = 3000000000LL; +END +CASE(2) + int x_[] = {58, 2012}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = 50; + long long _ = 0LL; +END +CASE(3) + int x_[] = {9, -3, 14, 6, 5, -9, 32, 7, -5, 26, 2, 11}; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = 3; + long long _ = 58LL; +END +/* +CASE(4) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = ; + long long _ = LL; +END +CASE(5) + int x_[] = ; + vector x(x_, x_+sizeof(x_)/sizeof(*x_)); + int K = ; + long long _ = LL; +END +*/ +} +// END CUT HERE