Set the answer to ANS1,ANS2
ANS1=A1*GCD,ANS2=A2*GCD,A1,A2 coprime
Gcd*a1*b1=lcm,gcd*a2*b2=lcm
A1*b1=lcm= (ANS1*ANS2)/gcd=a1*a2
The a1=b2,a2=b1 of the lawsuit.
In other words, ANS1=GCD*K1,ANS2=GCD*K2
Require K1,K2 as close as possible, and K1,k2 coprime, and, K2*K2=LCM/GCD
Need to use Pollard_rho decomposition factorization, and then brute force search for K1,K2. Used the Pollard_rho template of the Kuangbin great God.
There is a pit: input gcd and LCM, the direct output GCD,LCM can.
#include <stdio.h>#include<string.h>#include<stdlib.h>#include<time.h>#include<iostream>#include<cmath>#include<algorithm>using namespacestd;Const ints= -;Long LongMult_mod (Long LongALong LongBLong Longc) {a%=C; b%=C; Long Longret=0; while(b) {if(b&1) {ret+=a;ret%=C;} A<<=1; if(a>=c) a%=C; b>>=1; } returnret;}Long LongPow_mod (Long LongXLong LongNLong LongMoD) { if(n==1)returnX%MoD; X%=MoD; Long Longtmp=x; Long Longret=1; while(n) {if(n&1) ret=Mult_mod (RET,TMP,MOD); TMP=Mult_mod (TMP,TMP,MOD); N>>=1; } returnret;}BOOLCheckLong LongALong LongNLong LongXLong Longt) { Long Longret=Pow_mod (a,x,n); Long Longlast=ret; for(intI=1; i<=t;i++) {ret=Mult_mod (ret,ret,n); if(ret==1&&last!=1&&last!=n-1)return true; Last=ret; } if(ret!=1)return true; return false;}BOOLMiller_rabin (Long LongN) { if(n<2)return false; if(n==2)return true; if((n&1)==0)return false; Long Longx=n-1; Long Longt=0; while((x&1)==0) {x>>=1; t++;} for(intI=0; i<s;i++) { Long LongA=rand ()% (n1)+1; if(check (a,n,x,t))return false; } return true;}Long Longfactor[ -];intTol;Long LonggcdLong LongALong Longb) { if(a==0)return 1; if(a<0)returnGCD (-b); while(b) {Long Longt=a%b; A=b; b=T; } returnA;}Long LongPollard_rho (Long LongXLong Longc) { Long LongI=1, k=2; Long LongX0=rand ()%x; Long Longy=x0; while(1) {i++; X0= (Mult_mod (x0,x0,x) +c)%x; Long LongD=GCD (yx0,x); if(d!=1&&D!=X)returnD; if(y==x0)returnx; if(i==k) {y=x0;k+=K;} }}voidFINDFAC (Long LongN) { if(Miller_rabin (n)) {Factor[tol++]=N; return; } Long Longp=N; while(p>=n) P=pollard_rho (P,rand ()% (N-1)+1); FINDFAC (P); FINDFAC (n/p);}Long Longr[ -];intnum;Long LongK;voidDfsLong LongNowintXLong LongN) { if(NOW>SQRT (n))return; K=Max (K,now); for(inti=x;i<=num;i++) DFS (now*r[i],i+1, n);}intMain () {Long LongGcd,lcm,n; while(SCANF ("%lld%lld", &GCD,&LCM)! =EOF) { if(gcd==LCM) {printf ("%lld%lld\n", GCD,LCM); Continue; } Tol=0; N=lcm/GCD; FINDFAC (n); Sort (Factor,factor+tol); Num=0; for(intI=0; i<= -; i++) r[i]=1; R[num]=factor[0]; for(intI=1; i<tol;i++) { if(factor[i]==factor[i-1]) r[num]=r[num]*Factor[i]; Else{num++; R[num]=Factor[i]; }} k=1; Dfs1,0, N); printf ("%lld%lld\n", gcd*k,gcd* (n/k)); } return 0;}
POJ 2429 GCD & LCM Inverse