Artifact Content
Not logged in

Artifact 6a8dec603d2a8fec9078e09a10469d61c6ba3408


#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>
#include <cstring>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

int gcd(int a, int b) { while(a) swap(a,b%=a); return b; }

class SettingTents {
public:
	int countSites(int N, int M) 
	{
		int cnt = 0;
		for(int w=1; w<=N; ++w)     // for each
			for(int h=1; h<=M; ++h) // bounding box...
			{
				int numWH = (N+1-w)*(M+1-h);

				// w/2 - k(h-2j) = x
				// h/2 + kw      = y
				for(int j=1; j<h; ++j) // the case when both two diags contribute to the bounds
					if( (w*w+h*(h-2*j)) % (2*w) == 0 )
					{
						int x = (w*w+h*(h-2*j)) / (2*w);
						if( 0<=x && x<=w )
							cnt += numWH;
					}

				for(int y=0; 2*y<h; ++y) // the case when only one diag contribut to the bounds
					if( (w*w - (2*y-h)*h) % (2*w) == 0 )
					{
						int x = (w*w - (2*y-h)*h) / (2*w);
						if( x<w )
							cnt += 2*numWH;
					}

				if( w==h ) // special case
					cnt += numWH;
			}
		return cnt;
	}
};

// 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> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
int verify_case(const int &Expected, const int &Received) { if (Expected == Received) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } return 0;}

template<int N> struct Case_ { Case_(){start_time=clock();} };
char Test_(...);
int Test_(Case_<0>) {
	int N = 2; 
	int M = 2; 
	int RetVal = 6; 
	return verify_case(RetVal, SettingTents().countSites(N, M)); }
int Test_(Case_<1>) {
	int N = 1; 
	int M = 6; 
	int RetVal = 6; 
	return verify_case(RetVal, SettingTents().countSites(N, M)); }
int Test_(Case_<2>) {
	int N = 6; 
	int M = 8; 
	int RetVal = 527; 
	return verify_case(RetVal, SettingTents().countSites(N, M)); }

template<int N> void Run_() { cerr << "Test Case #" << N << "..." << flush; Test_(Case_<N>()); Run_<sizeof(Test_(Case_<N+1>()))==1 ? -1 : N+1>(); }
template<>      void Run_<-1>() {}
int main() { Run_<0>(); }
// END CUT HERE