ADDED SRM/524-U/2C-U.cpp Index: SRM/524-U/2C-U.cpp ================================================================== --- SRM/524-U/2C-U.cpp +++ SRM/524-U/2C-U.cpp @@ -0,0 +1,162 @@ +#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 MultiplesWithLimit { public: + string minMultiples(int N, vector forbiddenDigits) + { + vector allowed; + for(int i=0; i<=9; ++i) + if( find(forbiddenDigits.begin(), forbiddenDigits.end(), i) == forbiddenDigits.end() ) + allowed.push_back(i); + + vector< vector< pair > > G(N); + for(int v=0; v dist_to_zero(N, INF); + queue Q; + dist_to_zero[0] = 0; + Q.push(0); + while( !Q.empty() ) + { + int v = Q.front(); + Q.pop(); + for(int i=0; i0 || allowed[i]>0) + { + int vv = (v*10 + allowed[i]) % N; + if( dist_to_zero[vv] < best ) { + best = dist_to_zero[vv]; + besta = allowed[i]; + } + } + if( best == INF ) + return "IMPOSSIBLE"; + all += char('0' + besta); + v = (v*10 + besta) % N; + cerr << v << " " << dist_to_zero[v] << " " << besta << endl; + } while( v ); + + if( all.size() <= 8 ) + return all; + else { + stringstream ss; + ss << all.substr(0,3) << "..." << all.substr(all.size()-3) << "(" << all.size() << " digits)"; + return ss.str(); + } + } + + int POW(int v, int e, int m) + { + int r = 1%m; + for(; e; e>>=1,v=v*v%m) + if( e&1 ) + r = r*v%m; + return r; + } + + static const int INF = 0x1fffffff; +}; + +// 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 string& Expected, const string& 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(_, MultiplesWithLimit().minMultiples(N, forbiddenDigits));} +int main(){ + +CASE(0) + int N = 8; + int forbiddenDigits_[] = {2,3,4,5,6,7,8,9}; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = "1000"; +END +CASE(1) + int N = 9; + int forbiddenDigits_[] = {1,3,4,5,6,7,8,9}; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = "222...222(9 digits)"; +END +CASE(2) + int N = 524; + int forbiddenDigits_[] = {5,2,4}; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = "3668"; +END +CASE(3) + int N = 10000; + int forbiddenDigits_[] = {0}; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = "IMPOSSIBLE"; +END +CASE(4) + int N = 1; + int forbiddenDigits_[] = {0,1,2,3,4,5,6,7,8,9}; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = "IMPOSSIBLE"; +END +/* +CASE(5) + int N = ; + int forbiddenDigits_[] = ; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = ; +END +CASE(6) + int N = ; + int forbiddenDigits_[] = ; + vector forbiddenDigits(forbiddenDigits_, forbiddenDigits_+sizeof(forbiddenDigits_)/sizeof(*forbiddenDigits_)); + string _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/525-U/1A.cpp Index: SRM/525-U/1A.cpp ================================================================== --- SRM/525-U/1A.cpp +++ SRM/525-U/1A.cpp @@ -0,0 +1,184 @@ +#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 DropCoins { public: + int getMinimum(vector board, int K) + { + static const int INF = 0x3fffffff; + int best = INF; + int H = board.size(); + int W = board[0].size(); + vector< vector > sum(H, vector(W+1)); + 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(_, DropCoins().getMinimum(board, K));} +int main(){ + +CASE(0) + string board_[] = {".o.." +,"oooo" +,"..o."} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 3; + int _ = 2; +END +CASE(1) + string board_[] = {".....o" +,"......" +,"oooooo" +,"oooooo" +,"......" +,"o....."} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 12; + int _ = 3; +END +CASE(2) + string board_[] = {"...." +,".oo." +,".oo." +,"...."} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 3; + int _ = -1; +END +CASE(3) + string board_[] = {"......." +,"..ooo.." +,"ooooooo" +,".oo.oo." +,"oo...oo"} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 12; + int _ = 4; +END +CASE(4) + string board_[] = {"................." +,".ooooooo...oooo.." +,".ooooooo..oooooo." +,".oo.......oo..oo." +,".oo.......oo..oo." +,".ooooo.....oooo.." +,".ooooooo...oooo.." +,".....ooo..oo..oo." +,"......oo..oo..oo." +,".ooooooo..oooooo." +,".oooooo....oooo.." +,"................."} +; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 58; + int _ = 6; +END +CASE(5) + string board_[] = {"oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", + "oooooooooooooooooooooooooooooo", +}; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = 1; + int _ = 58; +END +/* +CASE(6) + string board_[] = ; + vector board(board_, board_+sizeof(board_)/sizeof(*board_)); + int K = ; + int _ = ; +END +*/ +} +// END CUT HERE ADDED SRM/525-U/1B.cpp Index: SRM/525-U/1B.cpp ================================================================== --- SRM/525-U/1B.cpp +++ SRM/525-U/1B.cpp @@ -0,0 +1,168 @@ +#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 Rumor { public: + int getMinimum(string knowledge, vector graph) + { + const int N = knowledge.size(); + const int ALL = (1< g(N); + for(int v=0; v +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(_, Rumor().getMinimum(knowledge, graph));} +int main(){ + +CASE(0) + string knowledge = "YNN"; + string graph_[] = {"NYN" +,"NNY" +,"NNN"}; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 3; +END +CASE(1) + string knowledge = "YNNY"; + string graph_[] = {"NYYN" +,"YNNY" +,"YNNY" +,"NYYN"}; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 1; +END +CASE(2) + string knowledge = "YYYY"; + string graph_[] = {"NYNN" +,"YNYN" +,"NYNY" +,"NNYN"}; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 0; +END +CASE(3) + string knowledge = "YYYYYN"; + string graph_[] = {"NYYYYN" +,"YNYYYN" +,"YYNYYN" +,"YYYNYN" +,"YYYYNN" +,"NNNNNN"}; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = -1; +END +CASE(4) + string knowledge = "NNNY"; + string graph_[] = {"NNNN" +,"YNNN" +,"YNNN" +,"NYYN"}; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 3; +END +CASE(5) + string knowledge = "NNNNNNNYYY"; + string graph_[] = {"NYNNYNNYNN" +,"NNYNYNNNNY" +,"YYNNNYNNNN" +,"YNNNYNYNNN" +,"NNYNNYNNYN" +,"NNNNYNNNYY" +,"NYNYNYNNNN" +,"NNNNNNYNYY" +,"NNNYNNNYNY" +,"NYYNNNNYNN"} +; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 2; +END +CASE(6) +string knowledge = "YYYNNN"; + string graph_[] = { + "NNNYNY", + "NNNYYN", + "NNNNYY", + "NNNNNN", + "NNNNNN", + "NNNNNN" + }; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = 2; +END +/* +CASE(7) + string knowledge = ; + string graph_[] = ; + vector graph(graph_, graph_+sizeof(graph_)/sizeof(*graph_)); + int _ = ; +END +*/ +} +// END CUT HERE