![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Problem Statement for FoxAndGCDLCM | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
![]() | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior
The key point behind this question is prodect(AB)=gcd*lcm. Now since the number crosses
10^12 which means gcd*lcm may excedd the bound. So first inference if (lcm%gcd!=0) then there is no pair A and B.
Now first we divide lcm/gcd let the value be temp. Now the numbers should be such that when
multiplied gives temp and is coprime to each other. We choose such A,B such that A+B is minimised. The answer is (A+B)*gcd.
******** Code goes here ***************
#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define pb push_back
#define ss second
#define ff first
#define ll long long int
#define inf 5000000000000000000
ll get(ll g, ll l)
{
if(l%g!=0)
return -1;
ll val=l/g;
ll temp=sqrt(val);
ll mi=inf;
for(int i=1;i<=temp;i++)
{
if(val%i==0)
{
ll f1=i;
ll f2=val/i;
if(__gcd(f1,f2)==1)
{
mi=min(inf,f1+f2);
}
}
}
return mi*g;
}
int main()
{
ll g,l;
cin>>g>>l;
cout<<get(g,l)<<endl;
return 0;
}
|
Saturday, 5 March 2016
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment