Artifact Content
Not logged in

Artifact 1549db04e8126d785195da0b282fa68b128af7a4


#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>
#ifdef __GNUC__
#include <ext/hash_map>
#define unordered_map __gnu_cxx::hash_map
#else
#include <unordered_map>
#endif
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

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

class FoxAndGCDLCM { public:
	long long get(long long G, long long L)
	{
		if( L%G != 0 )
			return -1;
		LL best = -1;
		LL z = L / G;
		for(LL p=1; p*p<=z; ++p)
			if( z%p==0 && gcd(p,z/p)==1 ) {
				LL A = G*p;
				LL B = G*(z/p);
				if( best == -1 || best > A+B )
					best = A+B;
			}
		return best;
	}
};

// 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> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const long long& Expected, const long long& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, FoxAndGCDLCM().get(G, L));}
int main(){

CASE(0)
	long long G = 2LL; 
	long long L = 20LL; 
	long long _ = 14LL; 
END
CASE(1)
	long long G = 5LL; 
	long long L = 8LL; 
	long long _ = -1LL; 
END
CASE(2)
	long long G = 1000LL; 
	long long L = 100LL; 
	long long _ = -1LL; 
END
CASE(3)
	long long G = 100LL; 
	long long L = 1000LL; 
	long long _ = 700LL; 
END
CASE(4)
	long long G = 10LL; 
	long long L = 950863963000LL; 
	long long _ = 6298430LL; 
END
/*
CASE(5)
	long long G = LL; 
	long long L = LL; 
	long long _ = LL; 
END
CASE(6)
	long long G = LL; 
	long long L = LL; 
	long long _ = LL; 
END
*/
}
// END CUT HERE