[NOIP 2001] 最大公约数和最小公倍数问题

题解

这题的标程写错了,它平方统计了两次,但是我写的是对的。

补,一组数据:

input

5 5

output

2

嗯,只要懂gcd,lcm这题应该都会写。

代码

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

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

int main()
{
    ll x,y;
    cin>>x>>y;
    ll m=x*y;
    ll sq=sqrt(m);
    bool flag=0;
    ll ans=0;
    for (ll i=x;i<=sq;i++) {
        ll j=m/i;
        ll g=gcd(i,j);
        if (g==x&&i*j/g==y) {
            ans++;
            if (i==sq) {
                flag=1;
            }
        }
    }
    ans*=2;
    if (flag) ans--;
    cout<<ans<<endl;
    return 0;
}
posted @ 2020-03-01 17:24  xyee  阅读(261)  评论(0编辑  收藏  举报