超时了,求大佬优化(简单点,我是蒟蒻)

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

aptx283211 @ 2023-12-02 13:42:56

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

int measure(int x1,int y1){
    int z=y1;
    while(x1%y1!=0){
        z=x1%y1;
        x1=y1;
        y1=z;   
    }
    return z;
}
int main(){
    int x,y,sum=0,a=sum;
    cin>>x>>y;
    for(int i=1;i<=y;i++){
        for(int j=1;j<=y;j++){
            if(measure(i,j)==x){
                if(i*j/x==y){
                    sum++;
                }
            }
        }
        if(a<sum){
            a=sum;
            continue;
        }
    }
    cout<<sum;

} 

by kkksc82 @ 2023-12-02 14:01:25

@aptx283211

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

|