Artifact Content
Not logged in

Artifact 9d97bb46a523cc677f49d1cab7127247d0fa30d7


#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<LD> 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<LL> 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<divs.size(); ++i) {
			LL Y = divs[i];
			if(Y<=H)
			{
				LL R = (2*S)/Y;
				for(int j=0; j<divs.size(); ++j) {
					LL X = divs[j];
					if(X<=W && R%X == 0)
					{
						LL T = R/X;
						LL twoP = T - (X-1) - W*(Y-1);
						if( (twoP&1) == 1 )
							continue;
						LL P = twoP/2;
						if( P<0 || H*W<=P )
							continue;
						LL x = P%W;
						LL y = P/W;
						if( x+X<=W && y+Y<=H ) {

							best = min(best, X*Y);
						}
					}
				}
			}
		}
		return best == INF ? -1 : best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::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