2a8b395e2e 2012-03-07 kinaba: #include <iostream> 2a8b395e2e 2012-03-07 kinaba: #include <sstream> 2a8b395e2e 2012-03-07 kinaba: #include <iomanip> 2a8b395e2e 2012-03-07 kinaba: #include <vector> 2a8b395e2e 2012-03-07 kinaba: #include <string> 2a8b395e2e 2012-03-07 kinaba: #include <map> 2a8b395e2e 2012-03-07 kinaba: #include <set> 2a8b395e2e 2012-03-07 kinaba: #include <algorithm> 2a8b395e2e 2012-03-07 kinaba: #include <numeric> 2a8b395e2e 2012-03-07 kinaba: #include <iterator> 2a8b395e2e 2012-03-07 kinaba: #include <functional> 2a8b395e2e 2012-03-07 kinaba: #include <complex> 2a8b395e2e 2012-03-07 kinaba: #include <queue> 2a8b395e2e 2012-03-07 kinaba: #include <stack> 2a8b395e2e 2012-03-07 kinaba: #include <cmath> 2a8b395e2e 2012-03-07 kinaba: #include <cassert> 2a8b395e2e 2012-03-07 kinaba: #include <cstring> 2a8b395e2e 2012-03-07 kinaba: #ifdef __GNUC__ 2a8b395e2e 2012-03-07 kinaba: #include <ext/hash_map> 2a8b395e2e 2012-03-07 kinaba: #define unordered_map __gnu_cxx::hash_map 2a8b395e2e 2012-03-07 kinaba: #else 2a8b395e2e 2012-03-07 kinaba: #include <unordered_map> 2a8b395e2e 2012-03-07 kinaba: #endif 2a8b395e2e 2012-03-07 kinaba: using namespace std; 2a8b395e2e 2012-03-07 kinaba: typedef long long LL; 2a8b395e2e 2012-03-07 kinaba: typedef complex<double> CMP; 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: LL gcd(LL a, LL b) 2a8b395e2e 2012-03-07 kinaba: { 2a8b395e2e 2012-03-07 kinaba: while(a) 2a8b395e2e 2012-03-07 kinaba: swap(a, b%=a); 2a8b395e2e 2012-03-07 kinaba: return b; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: class FoxAndGCDLCM { public: 2a8b395e2e 2012-03-07 kinaba: long long get(long long G, long long L) 2a8b395e2e 2012-03-07 kinaba: { 2a8b395e2e 2012-03-07 kinaba: if( L%G != 0 ) 2a8b395e2e 2012-03-07 kinaba: return -1; 2a8b395e2e 2012-03-07 kinaba: LL best = -1; 2a8b395e2e 2012-03-07 kinaba: LL z = L / G; 2a8b395e2e 2012-03-07 kinaba: for(LL p=1; p*p<=z; ++p) 2a8b395e2e 2012-03-07 kinaba: if( z%p==0 && gcd(p,z/p)==1 ) { 2a8b395e2e 2012-03-07 kinaba: LL A = G*p; 2a8b395e2e 2012-03-07 kinaba: LL B = G*(z/p); 2a8b395e2e 2012-03-07 kinaba: if( best == -1 || best > A+B ) 2a8b395e2e 2012-03-07 kinaba: best = A+B; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: return best; 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: }; 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: // BEGIN CUT HERE 2a8b395e2e 2012-03-07 kinaba: #include <ctime> 2a8b395e2e 2012-03-07 kinaba: double start_time; string timer() 2a8b395e2e 2012-03-07 kinaba: { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); } 2a8b395e2e 2012-03-07 kinaba: template<typename T> ostream& operator<<(ostream& os, const vector<T>& v) 2a8b395e2e 2012-03-07 kinaba: { os << "{ "; 2a8b395e2e 2012-03-07 kinaba: for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it) 2a8b395e2e 2012-03-07 kinaba: os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; } 2a8b395e2e 2012-03-07 kinaba: void verify_case(const long long& Expected, const long long& Received) { 2a8b395e2e 2012-03-07 kinaba: bool ok = (Expected == Received); 2a8b395e2e 2012-03-07 kinaba: if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl; 2a8b395e2e 2012-03-07 kinaba: cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } } 2a8b395e2e 2012-03-07 kinaba: #define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock(); 2a8b395e2e 2012-03-07 kinaba: #define END verify_case(_, FoxAndGCDLCM().get(G, L));} 2a8b395e2e 2012-03-07 kinaba: int main(){ 2a8b395e2e 2012-03-07 kinaba: 2a8b395e2e 2012-03-07 kinaba: CASE(0) 2a8b395e2e 2012-03-07 kinaba: long long G = 2LL; 2a8b395e2e 2012-03-07 kinaba: long long L = 20LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = 14LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(1) 2a8b395e2e 2012-03-07 kinaba: long long G = 5LL; 2a8b395e2e 2012-03-07 kinaba: long long L = 8LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = -1LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(2) 2a8b395e2e 2012-03-07 kinaba: long long G = 1000LL; 2a8b395e2e 2012-03-07 kinaba: long long L = 100LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = -1LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(3) 2a8b395e2e 2012-03-07 kinaba: long long G = 100LL; 2a8b395e2e 2012-03-07 kinaba: long long L = 1000LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = 700LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(4) 2a8b395e2e 2012-03-07 kinaba: long long G = 10LL; 2a8b395e2e 2012-03-07 kinaba: long long L = 950863963000LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = 6298430LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: /* 2a8b395e2e 2012-03-07 kinaba: CASE(5) 2a8b395e2e 2012-03-07 kinaba: long long G = LL; 2a8b395e2e 2012-03-07 kinaba: long long L = LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: CASE(6) 2a8b395e2e 2012-03-07 kinaba: long long G = LL; 2a8b395e2e 2012-03-07 kinaba: long long L = LL; 2a8b395e2e 2012-03-07 kinaba: long long _ = LL; 2a8b395e2e 2012-03-07 kinaba: END 2a8b395e2e 2012-03-07 kinaba: */ 2a8b395e2e 2012-03-07 kinaba: } 2a8b395e2e 2012-03-07 kinaba: // END CUT HERE