ADDED SRM/547-U/1A.cpp Index: SRM/547-U/1A.cpp ================================================================== --- SRM/547-U/1A.cpp +++ SRM/547-U/1A.cpp @@ -0,0 +1,111 @@ +#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 Pillars { public: + double getExpectedLength(int w, int x, int y) + { + if(x>y) swap(x,y); + + double e = 0; + int num = 1; + for(int y_minus_x=1-x; y_minus_x<=y-1; ++y_minus_x) { + e += hypot(y_minus_x, w) * num; + if(y_minus_x<0) + ++num; + else if(y_minus_x >= y-x) + --num; + } + return e/y/x; + } +}; + +// 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(_, Pillars().getExpectedLength(w, x, y));} +int main(){ +CASE(0) + int w = 1; + int x = 1; + int y = 1; + double _ = 1.0; +END +CASE(1) + int w = 1; + int x = 5; + int y = 1; + double _ = 2.387132965131785; +END +CASE(1) + int w = 1; + int x = 1; + int y = 5; + double _ = 2.387132965131785; +END +CASE(2) + int w = 2; + int x = 3; + int y = 15; + double _ = 6.737191281760445; +END +CASE(2) + int w = 2; + int x = 15; + int y = 3; + double _ = 6.737191281760445; +END +CASE(3) + int w = 10; + int x = 15; + int y = 23; + double _ = 12.988608956320535; +END +CASE(3) + int w = 10; + int x = 23; + int y = 15; + double _ = 12.988608956320535; +END +CASE(4) + int w = 1000; + int x = 100000; + int y = 100000; + double _ = -1; +END +CASE(5) + int w = 1; + int x = 1; + int y = 1; + double _ = 1; +END + +} +// END CUT HERE ADDED SRM/547-U/1B.cpp Index: SRM/547-U/1B.cpp ================================================================== --- SRM/547-U/1B.cpp +++ SRM/547-U/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 RectangularSum { public: + long long minimalArea(int height_, int width_, long long S) + { + LL H = height_; + LL W = width_; + static const LL INF = 0x7fffffffffffffffLL; + LL best = INF; + + vector divs; + for(LL x=1; x*x<=2*S; ++x) + if(2*S%x==0) { + divs.push_back(x); + divs.push_back(2*S/x); + } + + // sum = L1 + (L1 + wX) + (L1 + 2wX) + ... + (L1 + (Y-1)wX) + // = L1*Y + wX(Y-1)Y/2 + // = (L1+wX(Y-1)/2) * Y + // L1 = p + (p+1) + ... + (p+X-1) + // = p*X + X(X-1)/2 + // + // sum = (p + (X-1)/2 + w(Y-1)/2) * X * Y + // 2sum = (2p + X-1 + w(Y-1)) * X * Y + + 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(_, RectangularSum().minimalArea(height, width, S));} +int main(){ + +CASE(0) + int height = 2; + int width = 3; + long long S = 8LL; + long long _ = 4LL; +END +CASE(1) + int height = 3; + int width = 3; + long long S = 10LL; + long long _ = -1LL; +END +CASE(2) + int height = 3; + int width = 3; + long long S = 36LL; + long long _ = 9LL; +END +CASE(3) + int height = 25; + int width = 25; + long long S = 16000LL; + long long _ = 32LL; +END +CASE(4) + int height = 1000000; + int width = 1000000; + long long S = 1000000000000LL; + long long _ = 2LL; +END +CASE(5) + int height = 1000000; + int width = 1000000; + long long S = 123456789LL; + long long _ = -999LL; +END +CASE(6) + int height = 2; + int width = 3; + long long S = 6LL; + long long _ = 4LL; +END + +} +// END CUT HERE