UVA - 11388 GCD LCM 很水的一到入门题

讲道理,学 c 语言的时候肯定学过 求 GCD 和 LCM,
一般是求出 a, b 的GCD后, LCM = a * b / GCD ;
这样就ok了, (LCM / GCD) = ( a / GCD ) * ( b / GCD );
而且 ( a / GCD ) 和 ( b / GCD ) 互质

** 额,,我在输入 G 和 L 是忘记判断合法性,错了,也就是 样例2,,,2333

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
// cout << "  ===  " << endl;

using namespace std;
typedef long long ll;
const int maxn = 1000000 + 7, INF = 0x3f3f3f3f, mod = 1e9+7;
int T;
ll G, L;

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}

int main () {
    scanf("%d", &T);
    while(T--) {
        scanf("%lld%lld", &G, &L);
        if( (L/G) * G != L) { cout << -1 << endl; continue; } 
        else  L /= G;

        int f = 0;
        for(ll i = 1; i * i <= L; ++i) {
            ll t = L / i;
            if(i*t == L && gcd(i, t) == 1 ) {
                f = 1;
                cout << i*G << " " << t*G << endl;
                break;
            }
        }
        if(!f) cout << -1 << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值