Description
Given the greatest common divisor(GCD) and the least common multiple(LCM) of two integers a and b (a <= b), please find the minimum of b-a.
Input
Input may consist of several test data sets. For each data set, it can be format as below: There is just one line containing two integers, the GCD and the LCM, both are between [1, 10^9].
Input is ended by GCD = LCM = 0.
Output
For each data set, output one integer representing the minimum difference of a and b can be found, and then output a, then b, separated with only one space. If more than one pair of a and b can be found, output the one which has minimum a.
Sample Input
6 36 1 35 0 0
Sample Output
6 12 18 2 5 7 | ||
设x=GCD(a,b),y=LCM(a,b)。
a=x*m
b=x*n, 其中 GCD(m,n)=1
有y=x*m*n;
搜索。
#include <iostream>
#include <cmath>
using namespace std;
int gcd(int x, int y)
{
if (x%y==0)
return y;
else
return gcd(y, x%y);
}
int main()
{
int a,b;
while(cin>>a>>b&&a*a+b*b!=0)
{
int c=b/a,i;
for(i=(int)sqrt((double)c);i>=1;i--)
if(c%i==0 && gcd(i,c/i)==1)
break;
cout<<(c/i-i)*a<<" "<<a*i<<" "<<b/i<<endl;
}
return 0;
}