1793. Numbers

 
Time Limit: 1sec    Memory Limit:32MB
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
 Copy sample input to clipboard
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值