ADDED SRM/535-U/1A.cpp Index: SRM/535-U/1A.cpp ================================================================== --- SRM/535-U/1A.cpp +++ SRM/535-U/1A.cpp @@ -0,0 +1,107 @@ +#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; + +LL gcd(LL a, LL b) +{ + while(a) + swap(a, b%=a); + return b; +} + +class FoxAndGCDLCM { public: + long long get(long long G, long long L) + { + if( L%G != 0 ) + return -1; + LL best = -1; + LL z = L / G; + for(LL p=1; p*p<=z; ++p) + if( z%p==0 && gcd(p,z/p)==1 ) { + LL A = G*p; + LL B = G*(z/p); + if( best == -1 || best > A+B ) + best = A+B; + } + return best; + } +}; + +// 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 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(_, FoxAndGCDLCM().get(G, L));} +int main(){ + +CASE(0) + long long G = 2LL; + long long L = 20LL; + long long _ = 14LL; +END +CASE(1) + long long G = 5LL; + long long L = 8LL; + long long _ = -1LL; +END +CASE(2) + long long G = 1000LL; + long long L = 100LL; + long long _ = -1LL; +END +CASE(3) + long long G = 100LL; + long long L = 1000LL; + long long _ = 700LL; +END +CASE(4) + long long G = 10LL; + long long L = 950863963000LL; + long long _ = 6298430LL; +END +/* +CASE(5) + long long G = LL; + long long L = LL; + long long _ = LL; +END +CASE(6) + long long G = LL; + long long L = LL; + long long _ = LL; +END +*/ +} +// END CUT HERE ADDED SRM/535-U/1B.cpp Index: SRM/535-U/1B.cpp ================================================================== --- SRM/535-U/1B.cpp +++ SRM/535-U/1B.cpp @@ -0,0 +1,122 @@ +#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 FoxAndBusiness { public: + double minimumCost(int K, int totalWork, vector a, vector p) + { + double L=0, R=1e+60; + for(int _=0; _<10000; ++_) + { + double C = (L+R)/2; + (possible(C, K, a, p) ? R : L) = C; + } + return R * totalWork; + } + + bool possible(double C, int K, const vector& a, const vector& p) + { + vector v; + 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 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(_, FoxAndBusiness().minimumCost(K, totalWork, a, p));} +int main(){ + +CASE(0) + int K = 1; + int totalWork = 10; + int a_[] = {10}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {20}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 3800.0; +END +CASE(1) + int K = 1; + int totalWork = 100; + int a_[] = {50, 60}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {1000, 2000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 107200.0; +END +CASE(2) + int K = 2; + int totalWork = 300; + int a_[] = {10, 20, 47}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {15, 20, 98765}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 77500.0; +END +CASE(3) + int K = 4; + int totalWork = 1000; + int a_[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {20, 30, 40, 58, 60, 70, 80, 90, 100, 150}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = 531764.705882353; +END +CASE(4) + int K = 25; + int totalWork = 100000; + int a_[] = {17016,47415,83045,39455,34491,97616,91719,95101,85384,68562,36351,24654,36129,18257,87966,10190,67335,56121,20172,16228,16294,69902,83879,76475,81100,55688,5409,16764,33750,98625,56617,76728,50765,48650,58685,85248,46583,22525,8200,32280,59445,3381,85440,51313,26000,21507,39766,61104,76355,92950}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {59966,49102,35349,70206,65668,60145,10705,28056,9582,11619,8537,42449,30488,14162,1272,9064,817,15046,99637,16513,94277,42777,28196,34907,20309,47607,41378,33097,36974,74425,46978,13157,15027,12723,66607,26639,59698,37250,28554,78762,19543,55908,47550,42978,74731,4218,69250,16558,66512,12939}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = -1; +END +CASE(5) + int K = 1; + int totalWork = 100000; + int a_[] = {1}; + vector a(a_, a_+sizeof(a_)/sizeof(*a_)); + int p_[] = {100000}; + vector p(p_, p_+sizeof(p_)/sizeof(*p_)); + double _ = -1; +END +} +// END CUT HERE