求助,样例过不了

P1029 [NOIP2001 普及组] 最大公约数和最小公倍数问题

WJX120423 @ 2023-12-19 13:10:42

#include<bits/stdc++.h>
using namespace std;
long long x,y;
long long gcd(long long a,long long b)
{
    if(b==0)
    {
        return a;
    }
    else
    {
        return gcd(b,a%b);
    }
}
long long bei(long long a,long long b)
{
    return a/gcd(a,b)*b;
}
bool judge(long long a,long long b)
{
    if(gcd(a,b)==x&&bei(a,b)==y)
    {
        return true;
    }
}
int main() 
{
    cin>>x>>y;
    long long ans=0;
    long long maxn=max(x,y);
    for(long long i=1;i<=maxn;i++)
    {
        for(long long j=1;j<=maxn;j++)
        {
            if(judge(i,j)==true)
            {
                ans++;
            }
        }
    }
    cout<<ans;
    return 0;
}

by zrl123456 @ 2023-12-19 13:14:25

#include <bits/stdc++.h>
using namespace std;
long long x,y,sum;
int main(){
    cin>>x>>y;
    for(long long i=1;i*i<=x*y;i++)
        if((x*y)%i==0&&__gcd(x*y/i,i)==x&&i*i!=x*y) sum+=2;
            else if(i*i==x*y&&__gcd(i,i)==x) sum++;
    cout<<sum;
    return 0;
}

by zrl123456 @ 2023-12-19 13:16:14

在C++中,有 __gcd()函数


by WJX120423 @ 2023-12-19 13:19:07

@zrl123456 谢谢


|