Description
给出某两个整数a和b(a<=b)的最大公约数GCD和最小公倍数LCM,请找出满足的a和b,使得b-a的值最小。
Input
输入数据只有一行,包括两个整数GCD和LCM。输入保证至少存在一组解。
Output
输出包含一个整数,为最小的b-a的值。
Sample Input
6 36
Sample Output
6
Data Constraint
Hint
对于100%的数据,1<=a<=b<=10^9。
分析
设原数为x,y 则有x*y/gcd=lcm,且x/gcd与y/gcd互质,(x/gcd*y/gcd)*gcd=lcm,故我们只要求出两个互质数相乘=lcm/gcd即可
#代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <complex>
#include <cstdio>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#define INF 0x7fffffff
#define sqr(x) ((x) * (x))
#define pi acos(-1)
int read()
{
int x = 0, f = 1;
char ch = getchar();
while (ch < '0' || ch > '9'){if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int gcd(int x,int y)
{
return y == 0 ? x : gcd(y, x % y);
}
int main()
{
int a = read(), b = read();
int s = b / a;
int x,y;
for (int i = floor(sqrt(s)); i >= 1; i--)
if (s % i == 0 && gcd(i, s / i) == 1)
{
x = i;
y = s / x;
break;
}
printf("%d\n", a * (y - x));
}