2e8a7a70fd 2014-12-09 kinaba: #include <iostream> 2e8a7a70fd 2014-12-09 kinaba: #include <sstream> 2e8a7a70fd 2014-12-09 kinaba: #include <iomanip> 2e8a7a70fd 2014-12-09 kinaba: #include <vector> 2e8a7a70fd 2014-12-09 kinaba: #include <string> 2e8a7a70fd 2014-12-09 kinaba: #include <map> 2e8a7a70fd 2014-12-09 kinaba: #include <set> 2e8a7a70fd 2014-12-09 kinaba: #include <algorithm> 2e8a7a70fd 2014-12-09 kinaba: #include <numeric> 2e8a7a70fd 2014-12-09 kinaba: #include <iterator> 2e8a7a70fd 2014-12-09 kinaba: #include <functional> 2e8a7a70fd 2014-12-09 kinaba: #include <complex> 2e8a7a70fd 2014-12-09 kinaba: #include <queue> 2e8a7a70fd 2014-12-09 kinaba: #include <stack> 2e8a7a70fd 2014-12-09 kinaba: #include <cmath> 2e8a7a70fd 2014-12-09 kinaba: #include <cassert> 2e8a7a70fd 2014-12-09 kinaba: #include <tuple> 2e8a7a70fd 2014-12-09 kinaba: using namespace std; 2e8a7a70fd 2014-12-09 kinaba: typedef long long LL; 2e8a7a70fd 2014-12-09 kinaba: typedef complex<double> CMP; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: class AliceGame { public: 2e8a7a70fd 2014-12-09 kinaba: long long findMinimumValue(long long x, long long y) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: LL c = (LL)sqrt(x+y); 2e8a7a70fd 2014-12-09 kinaba: for(LL n=max(0LL, c-10); n<=c+10; ++n) 2e8a7a70fd 2014-12-09 kinaba: if(n*n == x+y) 2e8a7a70fd 2014-12-09 kinaba: return solve(int(n), x); 2e8a7a70fd 2014-12-09 kinaba: return -1; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: LL solve(int n, LL x) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: int cnt = 0; 2e8a7a70fd 2014-12-09 kinaba: for(int t=n; t>=1; --t) 2e8a7a70fd 2014-12-09 kinaba: { 2e8a7a70fd 2014-12-09 kinaba: int s = 2*t-1; 2e8a7a70fd 2014-12-09 kinaba: if(s <= x) { 2e8a7a70fd 2014-12-09 kinaba: x -= s; 2e8a7a70fd 2014-12-09 kinaba: cnt ++; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: return cnt; 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: }; 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: // BEGIN CUT HERE 2e8a7a70fd 2014-12-09 kinaba: #include <ctime> 2e8a7a70fd 2014-12-09 kinaba: double start_time; string timer() 2e8a7a70fd 2014-12-09 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2e8a7a70fd 2014-12-09 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2e8a7a70fd 2014-12-09 kinaba: { os << "{ "; 2e8a7a70fd 2014-12-09 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2e8a7a70fd 2014-12-09 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2e8a7a70fd 2014-12-09 kinaba: void verify_case(const long long& Expected, const long long& Received) { 2e8a7a70fd 2014-12-09 kinaba: bool ok = (Expected == Received); 2e8a7a70fd 2014-12-09 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2e8a7a70fd 2014-12-09 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2e8a7a70fd 2014-12-09 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2e8a7a70fd 2014-12-09 kinaba: #define END verify_case(_, AliceGame().findMinimumValue(x, y));} 2e8a7a70fd 2014-12-09 kinaba: int main(){ 2e8a7a70fd 2014-12-09 kinaba: 2e8a7a70fd 2014-12-09 kinaba: CASE(0) 2e8a7a70fd 2014-12-09 kinaba: long long x = 8LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 17LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 2LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(1) 2e8a7a70fd 2014-12-09 kinaba: long long x = 17LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 8LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 3LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(2) 2e8a7a70fd 2014-12-09 kinaba: long long x = 0LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 0LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 0LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(3) 2e8a7a70fd 2014-12-09 kinaba: long long x = 9LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 9LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = -1LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(4) 2e8a7a70fd 2014-12-09 kinaba: long long x = 500000LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 500000LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 294LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(5) 2e8a7a70fd 2014-12-09 kinaba: long long x = 0LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 4LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 0LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: CASE(6) 2e8a7a70fd 2014-12-09 kinaba: long long x = 4LL; 2e8a7a70fd 2014-12-09 kinaba: long long y = 0LL; 2e8a7a70fd 2014-12-09 kinaba: long long _ = 2LL; 2e8a7a70fd 2014-12-09 kinaba: END 2e8a7a70fd 2014-12-09 kinaba: } 2e8a7a70fd 2014-12-09 kinaba: // END CUT HERE