98499b8c6b 2011-11-12 kinaba: #include <iostream> 98499b8c6b 2011-11-12 kinaba: #include <sstream> 98499b8c6b 2011-11-12 kinaba: #include <iomanip> 98499b8c6b 2011-11-12 kinaba: #include <vector> 98499b8c6b 2011-11-12 kinaba: #include <string> 98499b8c6b 2011-11-12 kinaba: #include <map> 98499b8c6b 2011-11-12 kinaba: #include <set> 98499b8c6b 2011-11-12 kinaba: #include <algorithm> 98499b8c6b 2011-11-12 kinaba: #include <numeric> 98499b8c6b 2011-11-12 kinaba: #include <iterator> 98499b8c6b 2011-11-12 kinaba: #include <functional> 98499b8c6b 2011-11-12 kinaba: #include <complex> 98499b8c6b 2011-11-12 kinaba: #include <queue> 98499b8c6b 2011-11-12 kinaba: #include <stack> 98499b8c6b 2011-11-12 kinaba: #include <cmath> 98499b8c6b 2011-11-12 kinaba: #include <cassert> 98499b8c6b 2011-11-12 kinaba: #include <cstring> 98499b8c6b 2011-11-12 kinaba: using namespace std; 98499b8c6b 2011-11-12 kinaba: typedef long long LL; 98499b8c6b 2011-11-12 kinaba: typedef complex<double> CMP; 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: class CorrectMultiplication { public: 98499b8c6b 2011-11-12 kinaba: long long getMinimum(int a, int b, int c) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: return solve(a,b,c); 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: LL solve(LL a, LL b, LL c) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: if( a > b ) 98499b8c6b 2011-11-12 kinaba: swap(a,b); 98499b8c6b 2011-11-12 kinaba: LL vv = (1LL<<62); 98499b8c6b 2011-11-12 kinaba: for(LL A=1; A*A <= (a+b+c)*2; ++A) 98499b8c6b 2011-11-12 kinaba: vv = min(vv, best_of(A,a,b,c)); 98499b8c6b 2011-11-12 kinaba: return vv; 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: LL best_of(LL A, LL a, LL b, LL c) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: LL Bl = 1; 98499b8c6b 2011-11-12 kinaba: LL Br = a+b+c; 98499b8c6b 2011-11-12 kinaba: while( Bl+3 < Br ) 98499b8c6b 2011-11-12 kinaba: { 98499b8c6b 2011-11-12 kinaba: LL Bc1 = (Bl*2+Br*1) / 3; 98499b8c6b 2011-11-12 kinaba: LL Bc2 = (Bl*1+Br*2) / 3; 98499b8c6b 2011-11-12 kinaba: LL cc1 = abs(A-a)+abs(Bc1-b)+abs(A*Bc1-c); 98499b8c6b 2011-11-12 kinaba: LL cc2 = abs(A-a)+abs(Bc2-b)+abs(A*Bc2-c); 98499b8c6b 2011-11-12 kinaba: if(cc1 < cc2) 98499b8c6b 2011-11-12 kinaba: Br = Bc2; 98499b8c6b 2011-11-12 kinaba: else 98499b8c6b 2011-11-12 kinaba: Bl = Bc1; 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: LL vv = (1LL<<62); 98499b8c6b 2011-11-12 kinaba: for(LL B=Bl; B<=Br; ++B) 98499b8c6b 2011-11-12 kinaba: vv = min(vv, abs(A-a)+abs(B-b)+abs(A*B-c)); 98499b8c6b 2011-11-12 kinaba: return vv; 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: }; 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: // BEGIN CUT HERE 98499b8c6b 2011-11-12 kinaba: #include <ctime> 98499b8c6b 2011-11-12 kinaba: double start_time; string timer() 98499b8c6b 2011-11-12 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 98499b8c6b 2011-11-12 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 98499b8c6b 2011-11-12 kinaba: { os << "{ "; 98499b8c6b 2011-11-12 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 98499b8c6b 2011-11-12 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 98499b8c6b 2011-11-12 kinaba: void verify_case(const long long& Expected, const long long& Received) { 98499b8c6b 2011-11-12 kinaba: bool ok = (Expected == Received); 98499b8c6b 2011-11-12 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 98499b8c6b 2011-11-12 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 98499b8c6b 2011-11-12 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 98499b8c6b 2011-11-12 kinaba: #define END verify_case(_, CorrectMultiplication().getMinimum(a, b, c));} 98499b8c6b 2011-11-12 kinaba: int main(){ 98499b8c6b 2011-11-12 kinaba: 98499b8c6b 2011-11-12 kinaba: CASE(0) 98499b8c6b 2011-11-12 kinaba: int a = 19; 98499b8c6b 2011-11-12 kinaba: int b = 28; 98499b8c6b 2011-11-12 kinaba: int c = 522; 98499b8c6b 2011-11-12 kinaba: long long _ = 2LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(1) 98499b8c6b 2011-11-12 kinaba: int a = 10; 98499b8c6b 2011-11-12 kinaba: int b = 30; 98499b8c6b 2011-11-12 kinaba: int c = 500; 98499b8c6b 2011-11-12 kinaba: long long _ = 11LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(2) 98499b8c6b 2011-11-12 kinaba: int a = 11111; 98499b8c6b 2011-11-12 kinaba: int b = 11111; 98499b8c6b 2011-11-12 kinaba: int c = 123454321; 98499b8c6b 2011-11-12 kinaba: long long _ = 0LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(3) 98499b8c6b 2011-11-12 kinaba: int a = 1000; 98499b8c6b 2011-11-12 kinaba: int b = 100; 98499b8c6b 2011-11-12 kinaba: int c = 10; 98499b8c6b 2011-11-12 kinaba: long long _ = 1089LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(4) 98499b8c6b 2011-11-12 kinaba: int a = 399; 98499b8c6b 2011-11-12 kinaba: int b = 522; 98499b8c6b 2011-11-12 kinaba: int c = 199999; 98499b8c6b 2011-11-12 kinaba: long long _ = 24LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: CASE(5) 98499b8c6b 2011-11-12 kinaba: int a = 1000000000; 98499b8c6b 2011-11-12 kinaba: int b = 1000000000; 98499b8c6b 2011-11-12 kinaba: int c = 1000000000; 98499b8c6b 2011-11-12 kinaba: long long _ = -1LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: /* 98499b8c6b 2011-11-12 kinaba: CASE(6) 98499b8c6b 2011-11-12 kinaba: int a = ; 98499b8c6b 2011-11-12 kinaba: int b = ; 98499b8c6b 2011-11-12 kinaba: int c = ; 98499b8c6b 2011-11-12 kinaba: long long _ = LL; 98499b8c6b 2011-11-12 kinaba: END 98499b8c6b 2011-11-12 kinaba: */ 98499b8c6b 2011-11-12 kinaba: } 98499b8c6b 2011-11-12 kinaba: // END CUT HERE