d921147eef 2012-07-01 kinaba: #include <iostream> d921147eef 2012-07-01 kinaba: #include <sstream> d921147eef 2012-07-01 kinaba: #include <iomanip> d921147eef 2012-07-01 kinaba: #include <vector> d921147eef 2012-07-01 kinaba: #include <string> d921147eef 2012-07-01 kinaba: #include <map> d921147eef 2012-07-01 kinaba: #include <set> d921147eef 2012-07-01 kinaba: #include <algorithm> d921147eef 2012-07-01 kinaba: #include <numeric> d921147eef 2012-07-01 kinaba: #include <iterator> d921147eef 2012-07-01 kinaba: #include <functional> d921147eef 2012-07-01 kinaba: #include <complex> d921147eef 2012-07-01 kinaba: #include <queue> d921147eef 2012-07-01 kinaba: #include <stack> d921147eef 2012-07-01 kinaba: #include <cmath> d921147eef 2012-07-01 kinaba: #include <cassert> d921147eef 2012-07-01 kinaba: using namespace std; d921147eef 2012-07-01 kinaba: typedef long long LL; d921147eef 2012-07-01 kinaba: typedef long double LD; d921147eef 2012-07-01 kinaba: typedef complex<LD> CMP; d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: class RectangularSum { public: d921147eef 2012-07-01 kinaba: long long minimalArea(int height_, int width_, long long S) d921147eef 2012-07-01 kinaba: { d921147eef 2012-07-01 kinaba: LL H = height_; d921147eef 2012-07-01 kinaba: LL W = width_; d921147eef 2012-07-01 kinaba: static const LL INF = 0x7fffffffffffffffLL; d921147eef 2012-07-01 kinaba: LL best = INF; d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: vector<LL> divs; d921147eef 2012-07-01 kinaba: for(LL x=1; x*x<=2*S; ++x) d921147eef 2012-07-01 kinaba: if(2*S%x==0) { d921147eef 2012-07-01 kinaba: divs.push_back(x); d921147eef 2012-07-01 kinaba: divs.push_back(2*S/x); d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: // sum = L1 + (L1 + wX) + (L1 + 2wX) + ... + (L1 + (Y-1)wX) d921147eef 2012-07-01 kinaba: // = L1*Y + wX(Y-1)Y/2 d921147eef 2012-07-01 kinaba: // = (L1+wX(Y-1)/2) * Y d921147eef 2012-07-01 kinaba: // L1 = p + (p+1) + ... + (p+X-1) d921147eef 2012-07-01 kinaba: // = p*X + X(X-1)/2 d921147eef 2012-07-01 kinaba: // d921147eef 2012-07-01 kinaba: // sum = (p + (X-1)/2 + w(Y-1)/2) * X * Y d921147eef 2012-07-01 kinaba: // 2sum = (2p + X-1 + w(Y-1)) * X * Y d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: for(int i=0; i<divs.size(); ++i) { d921147eef 2012-07-01 kinaba: LL Y = divs[i]; d921147eef 2012-07-01 kinaba: if(Y<=H) d921147eef 2012-07-01 kinaba: { d921147eef 2012-07-01 kinaba: LL R = (2*S)/Y; d921147eef 2012-07-01 kinaba: for(int j=0; j<divs.size(); ++j) { d921147eef 2012-07-01 kinaba: LL X = divs[j]; d921147eef 2012-07-01 kinaba: if(X<=W && R%X == 0) d921147eef 2012-07-01 kinaba: { d921147eef 2012-07-01 kinaba: LL T = R/X; d921147eef 2012-07-01 kinaba: LL twoP = T - (X-1) - W*(Y-1); d921147eef 2012-07-01 kinaba: if( (twoP&1) == 1 ) d921147eef 2012-07-01 kinaba: continue; d921147eef 2012-07-01 kinaba: LL P = twoP/2; d921147eef 2012-07-01 kinaba: if( P<0 || H*W<=P ) d921147eef 2012-07-01 kinaba: continue; d921147eef 2012-07-01 kinaba: LL x = P%W; d921147eef 2012-07-01 kinaba: LL y = P/W; d921147eef 2012-07-01 kinaba: if( x+X<=W && y+Y<=H ) { d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: best = min(best, X*Y); d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: return best == INF ? -1 : best; d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: }; d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: // BEGIN CUT HERE d921147eef 2012-07-01 kinaba: #include <ctime> d921147eef 2012-07-01 kinaba: double start_time; string timer() d921147eef 2012-07-01 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } d921147eef 2012-07-01 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) d921147eef 2012-07-01 kinaba: { os << "{ "; d921147eef 2012-07-01 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) d921147eef 2012-07-01 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } d921147eef 2012-07-01 kinaba: void verify_case(const long long& Expected, const long long& Received) { d921147eef 2012-07-01 kinaba: bool ok = (Expected == Received); d921147eef 2012-07-01 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; d921147eef 2012-07-01 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } d921147eef 2012-07-01 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); d921147eef 2012-07-01 kinaba: #define END verify_case(_, RectangularSum().minimalArea(height, width, S));} d921147eef 2012-07-01 kinaba: int main(){ d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: CASE(0) d921147eef 2012-07-01 kinaba: int height = 2; d921147eef 2012-07-01 kinaba: int width = 3; d921147eef 2012-07-01 kinaba: long long S = 8LL; d921147eef 2012-07-01 kinaba: long long _ = 4LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(1) d921147eef 2012-07-01 kinaba: int height = 3; d921147eef 2012-07-01 kinaba: int width = 3; d921147eef 2012-07-01 kinaba: long long S = 10LL; d921147eef 2012-07-01 kinaba: long long _ = -1LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(2) d921147eef 2012-07-01 kinaba: int height = 3; d921147eef 2012-07-01 kinaba: int width = 3; d921147eef 2012-07-01 kinaba: long long S = 36LL; d921147eef 2012-07-01 kinaba: long long _ = 9LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(3) d921147eef 2012-07-01 kinaba: int height = 25; d921147eef 2012-07-01 kinaba: int width = 25; d921147eef 2012-07-01 kinaba: long long S = 16000LL; d921147eef 2012-07-01 kinaba: long long _ = 32LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(4) d921147eef 2012-07-01 kinaba: int height = 1000000; d921147eef 2012-07-01 kinaba: int width = 1000000; d921147eef 2012-07-01 kinaba: long long S = 1000000000000LL; d921147eef 2012-07-01 kinaba: long long _ = 2LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(5) d921147eef 2012-07-01 kinaba: int height = 1000000; d921147eef 2012-07-01 kinaba: int width = 1000000; d921147eef 2012-07-01 kinaba: long long S = 123456789LL; d921147eef 2012-07-01 kinaba: long long _ = -999LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: CASE(6) d921147eef 2012-07-01 kinaba: int height = 2; d921147eef 2012-07-01 kinaba: int width = 3; d921147eef 2012-07-01 kinaba: long long S = 6LL; d921147eef 2012-07-01 kinaba: long long _ = 4LL; d921147eef 2012-07-01 kinaba: END d921147eef 2012-07-01 kinaba: d921147eef 2012-07-01 kinaba: } d921147eef 2012-07-01 kinaba: // END CUT HERE