JZOJ 1013. 【东莞市选2008】GCD与LCM

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));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值