ADDED SRM/599-U/1A.cpp Index: SRM/599-U/1A.cpp ================================================================== --- SRM/599-U/1A.cpp +++ SRM/599-U/1A.cpp @@ -0,0 +1,95 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +class BigFatInteger { public: + int minOperations(int A, int B) + { + vector s; + for(int p=2; p*p<=A; ++p) + { + int c = 0; + while(A%p==0) A/=p, ++c; + if(c) s.push_back(c*B); + } + if(A>1) + s.push_back(1*B); + + int cnt = s.size(); + vector cur(s.size(), 1); + while(cur != s) + { + 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(_, BigFatInteger().minOperations(A, B));} +int main(){ + +CASE(0) + int A = 6; + int B = 1; + int _ = 2; +END +CASE(1) + int A = 162; + int B = 1; + int _ = 4; +END +CASE(2) + int A = 999983; + int B = 9; + int _ = 5; +END +CASE(3) + int A = 360; + int B = 8; + int _ = 8; +END +CASE(4) + int A = 1000000; + int B = 1000000; + int _ = -1; +END +CASE(5) + int A = 2*3*5*7*11*13*17; + int B = 1000000; + int _ = -1; +END + +} +// END CUT HERE ADDED SRM/599-U/1B.cpp Index: SRM/599-U/1B.cpp ================================================================== --- SRM/599-U/1B.cpp +++ SRM/599-U/1B.cpp @@ -0,0 +1,142 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +typedef long long LL; +typedef complex CMP; + +int gcd(int a, int b) +{ + while(a) + swap(a, b%=a); + return b; +} + +class FindPolygons { public: + double minimumPolygon(int L) + { + if(L<=2) + return -1; + if(L%2==1) // euclid%2 == manhattan%2. tour is always even. + return -1; + + map>> pyt; + for(int k=1; k<=L; ++k) { + auto& pk = pyt[k]; + pk.emplace_back(0, k); + pk.emplace_back(k, 0); + pk.emplace_back(0, -k); + pk.emplace_back(-k, 0); + } + for(int m=1; m*m<=L/2; ++m) + for(int n=1; n=0 && ay>=0) + for(auto& bv : pyt[b]) + { + int bx=bv.first; + int by=bv.second; + int cx=-ax-bx; + int cy=-ay-by; + if(ax*by!=ay*bx && cx*cx+cy*cy == c*c) { + best = min(best, c-a); + break; + } + } + } + } + if(best < L) + return best; + // Rectangle + if(L%4==0) + return 0; + return 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(_, FindPolygons().minimumPolygon(L));} +int main(){ +CASE(0) + int L = 4; + double _ = 0.0; +END +CASE(1) + int L = 5; + double _ = -1.0; +END +CASE(2) + int L = 12; + double _ = 2.0; +END +CASE(3) + int L = 4984; + double _ = 819.0; +END +CASE(4) + int L = 5000; + double _ = -999; +END +CASE(4) + int L = 20; + double _ = -999; +END +/* +CASE(5) + int L = ; + double _ = ; +END +*/ +} +// END CUT HERE