80分,超时求助

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

djc2012 @ 2024-11-07 12:42:08

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

by likerui @ 2024-11-25 19:46:33

@djc2012 你要把n改成sqrt(n)或 i*i<=n把ans++改成ans+=2;不然10^5*10^5会超时


by djc2012 @ 2024-11-26 12:35:16

90分,WA


by tyy_again @ 2024-12-03 12:46:08

@djc2012

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x,y,z,num=0,p;
    cin>>x>>y;
    z=x*y;
    for(p=x;p<=y;p++)
        if(z/p*p==z&&__gcd(p,z/p)==x)
            num++;
    cout<<num;
    return 0;
}

|