Description
Given two positive integers a and b, we can easily calculate the greatest common divisor (GCD) and the least common multiple (LCM) of a and b. But what about the inverse? That is: given GCD and LCM, finding a and b.
Input
The input contains multiple test cases, each of which contains two positive integers, the GCD and the LCM. You can assume that these two numbers are both less than 2^63.
Output
For each test case, output a and b in ascending order. If there are multiple solutions, output the pair with smallest a + b.
Sample Input
3 60
Sample Output
12 15
参考代码
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string>
#include<cstring>
#include<map>
#include<stdlib.h>
#include<time.h>
using namespace std;
typedef long long ll;
ll gc,lc;
map<ll,int>mp;
ll gcd(ll a,ll b){
if(b==0)return a;
return gcd(b,a%b);
}
ll qmul(ll a,ll b,ll mod){
ll res=0;
while(b>0){
if(b&1)res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll mod_pow(ll x,ll n,ll mod){
ll res=1;
while(n>0){
if(n&1)res=qmul(res,x,mod);
x=qmul(x,x,mod);
n>>=1;
}
return res;
}
ll random(ll n){
return (double)rand()/RAND_MAX*n;
}
bool miller_rabin(ll n){
if(n==2)return true;
if(n<2||n%2==0)return false;
ll p=n-1;
int k=0;
while(p%2==0){
p/=2;k++;
}
for(int i=0;i<10;i++){
ll a=random(n-2)+1;
ll x=mod_pow(a,p,n),y;
for(int j=0;j<k;j++){
y=qmul(x,x,n);
if(y==1&&x!=1&&x!=n-1)return false;
x=y;
}
if(y!=1)return false;
}
return true;
}
ll pollard_rho(ll n,ll c){
ll x,y,i=1,k=2;
x=random(n-2)+1;
y=x;
while(1){
i++;
x=(qmul(x,x,n)+c)%n;
ll d=gcd((y-x+n)%n,n);
if(d>1&&d<n)return d;
if(x==y)return n;
if(i==k){
y=x;
k<<=1;
}
}
}
void find(ll n,ll c=120){
if(n==1)return;
if(miller_rabin(n)){
mp[n]++;
return;
}
ll p=n;
ll k=c;
while(p>=n)p=pollard_rho(n,c--);
find(p,k);
find(n/p,k);
}
ll r1,r2;
void dfs(map<ll,int>::iterator it,ll a,ll b){
if(it==mp.end()){
if(a+b<r1+r2){
r1=a;r2=b;
}
return;
}
ll res=1,x=it->first,n=it->second;
while(n--)res*=x;
it++;
dfs(it,a*res,b);
dfs(it,a,b*res);
}
int main(){
while(scanf("%lld%lld",&gc,&lc)!=EOF){
mp.clear();
find(lc/gc);
r1=r2=(1ll<<61);
dfs(mp.begin(),1,1);
if(r1>r2)swap(r1,r2);
printf("%lld %lld\n",r1*gc,r2*gc);
}
return 0;
}