[数论]HOJ 2010 GCD & LCM Inverse

传送门:GCD & LCM Inverse

GCD & LCM Inverse

My Tags  (Edit)
  Source : HUST Monthly Contest of April
  Time limit : 2 sec   Memory limit : 32 M

Submitted : 761, Accepted : 213

We are all familiar with the Euclid's algorithm. Given two positive integers a, b. we can easily find the greatest common divisor (GCD) and least common multiple (LCM) of a, b through this method. But what about the inverse? That is: given GCD and LCM of a and b, can you find a, b ?

Input

The input will contain multiple test cases, each of which contains two positive integers: GCD and LCM. You can safely assume these integers will be less than 2^63-1.

Output

For each test case, output a, b in ascending order. If there are multiple solutions, output the pair with smallest a+b.

Sample Input

3 60

Sample Output

12 15

LangLang @ Achilles Team


解题报告:

题意:已知两个数的gcd和lcm求这两个数a, b,多解时要求a+b最小

思路:a/gcd * b/gcd = lcm/gcd,还有一个常识性质的性质:a*b为定值时,a和b相差越小,a+b越小。所以从lcm/gcd的平方根开始枚举a即可。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
long long gcd (long long a,long long b){
    return b==0?a:gcd(b,a%b);
}
int main(){
    long long a,b;
    while(scanf("%lld%lld",&a,&b)==2){
        long long tmp=b/a,i;
        for(i=sqrt(1.0*tmp);i>=1;i--)
            if(tmp%i==0){
                b=tmp/i;
                if(gcd(i,b)==1)
                    break;
            }
        printf("%lld %lld\n",a*i,a*b);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值