c179640bd8 2013-04-26 kinaba: #include <iostream> c179640bd8 2013-04-26 kinaba: #include <sstream> c179640bd8 2013-04-26 kinaba: #include <iomanip> c179640bd8 2013-04-26 kinaba: #include <vector> c179640bd8 2013-04-26 kinaba: #include <string> c179640bd8 2013-04-26 kinaba: #include <map> c179640bd8 2013-04-26 kinaba: #include <set> c179640bd8 2013-04-26 kinaba: #include <algorithm> c179640bd8 2013-04-26 kinaba: #include <numeric> c179640bd8 2013-04-26 kinaba: #include <iterator> c179640bd8 2013-04-26 kinaba: #include <functional> c179640bd8 2013-04-26 kinaba: #include <complex> c179640bd8 2013-04-26 kinaba: #include <queue> c179640bd8 2013-04-26 kinaba: #include <stack> c179640bd8 2013-04-26 kinaba: #include <cmath> c179640bd8 2013-04-26 kinaba: #include <cassert> c179640bd8 2013-04-26 kinaba: using namespace std; c179640bd8 2013-04-26 kinaba: typedef long long LL; c179640bd8 2013-04-26 kinaba: typedef long double LD; c179640bd8 2013-04-26 kinaba: typedef complex<LD> CMP; c179640bd8 2013-04-26 kinaba: c179640bd8 2013-04-26 kinaba: int gcd(int a, int b) c179640bd8 2013-04-26 kinaba: { c179640bd8 2013-04-26 kinaba: while(a) c179640bd8 2013-04-26 kinaba: swap(a, b%=a); c179640bd8 2013-04-26 kinaba: return b; c179640bd8 2013-04-26 kinaba: } c179640bd8 2013-04-26 kinaba: c179640bd8 2013-04-26 kinaba: class FruitTrees { public: c179640bd8 2013-04-26 kinaba: int maxDist(int apple, int kiwi, int grape) c179640bd8 2013-04-26 kinaba: { c179640bd8 2013-04-26 kinaba: int best = 0; c179640bd8 2013-04-26 kinaba: for(int k0=0; k0<kiwi; ++k0) { c179640bd8 2013-04-26 kinaba: int ak = calc(apple, 0, kiwi, k0); c179640bd8 2013-04-26 kinaba: for(int g0=0; g0<grape; ++g0) c179640bd8 2013-04-26 kinaba: best = max(best, min(ak, min(calc(apple, 0, grape, g0), calc(kiwi, k0, grape, g0)))); c179640bd8 2013-04-26 kinaba: } c179640bd8 2013-04-26 kinaba: return best; c179640bd8 2013-04-26 kinaba: } c179640bd8 2013-04-26 kinaba: c179640bd8 2013-04-26 kinaba: int calc(int a, int a0, int b, int b0) c179640bd8 2013-04-26 kinaba: { c179640bd8 2013-04-26 kinaba: //int x = abs(b0-a0); c179640bd8 2013-04-26 kinaba: int x = b0-a0; c179640bd8 2013-04-26 kinaba: int g = gcd(a, b); c179640bd8 2013-04-26 kinaba: return min(x%g, g-x%g); c179640bd8 2013-04-26 kinaba: } c179640bd8 2013-04-26 kinaba: }; c179640bd8 2013-04-26 kinaba: c179640bd8 2013-04-26 kinaba: // BEGIN CUT HERE c179640bd8 2013-04-26 kinaba: #include <ctime> c179640bd8 2013-04-26 kinaba: double start_time; string timer() c179640bd8 2013-04-26 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } c179640bd8 2013-04-26 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) c179640bd8 2013-04-26 kinaba: { os << "{ "; c179640bd8 2013-04-26 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) c179640bd8 2013-04-26 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } c179640bd8 2013-04-26 kinaba: void verify_case(const int& Expected, const int& Received) { c179640bd8 2013-04-26 kinaba: bool ok = (Expected == Received); c179640bd8 2013-04-26 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; c179640bd8 2013-04-26 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } c179640bd8 2013-04-26 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); c179640bd8 2013-04-26 kinaba: #define END verify_case(_, FruitTrees().maxDist(apple, kiwi, grape));} c179640bd8 2013-04-26 kinaba: int main(){ c179640bd8 2013-04-26 kinaba: c179640bd8 2013-04-26 kinaba: CASE(0) c179640bd8 2013-04-26 kinaba: int apple = 1; c179640bd8 2013-04-26 kinaba: int kiwi = 5; c179640bd8 2013-04-26 kinaba: int grape = 8; c179640bd8 2013-04-26 kinaba: int _ = 0; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: CASE(1) c179640bd8 2013-04-26 kinaba: int apple = 3; c179640bd8 2013-04-26 kinaba: int kiwi = 3; c179640bd8 2013-04-26 kinaba: int grape = 6; c179640bd8 2013-04-26 kinaba: int _ = 1; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: CASE(2) c179640bd8 2013-04-26 kinaba: int apple = 40; c179640bd8 2013-04-26 kinaba: int kiwi = 30; c179640bd8 2013-04-26 kinaba: int grape = 20; c179640bd8 2013-04-26 kinaba: int _ = 5; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: CASE(3) c179640bd8 2013-04-26 kinaba: int apple = 899; c179640bd8 2013-04-26 kinaba: int kiwi = 1073; c179640bd8 2013-04-26 kinaba: int grape = 1147; c179640bd8 2013-04-26 kinaba: int _ = 14; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: CASE(4) c179640bd8 2013-04-26 kinaba: int apple = 2000; c179640bd8 2013-04-26 kinaba: int kiwi = 2000; c179640bd8 2013-04-26 kinaba: int grape = 2000; c179640bd8 2013-04-26 kinaba: int _ = 666; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: CASE(5) c179640bd8 2013-04-26 kinaba: int apple = 100; c179640bd8 2013-04-26 kinaba: int kiwi = 36; c179640bd8 2013-04-26 kinaba: int grape = 225; c179640bd8 2013-04-26 kinaba: int _ = -1; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: /* c179640bd8 2013-04-26 kinaba: CASE(6) c179640bd8 2013-04-26 kinaba: int apple = ; c179640bd8 2013-04-26 kinaba: int kiwi = ; c179640bd8 2013-04-26 kinaba: int grape = ; c179640bd8 2013-04-26 kinaba: int _ = ; c179640bd8 2013-04-26 kinaba: END c179640bd8 2013-04-26 kinaba: */ c179640bd8 2013-04-26 kinaba: } c179640bd8 2013-04-26 kinaba: // END CUT HERE