90pts求助!

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

BJ_BSGF_Lyc @ 2024-03-17 09:50:11

测试点#10:MLE
其余 AC

long long开大了吗?
求帮

#include<bits/stdc++.h>
using namespace std;
int gcd(int x,int y){
    int g=(x<y)?x:y;
    while((g>1)&&(x%g!=0||y%g!=0)){
        g--;
    }
    return g;
}
int n,m,s;
int main(){
    cin>>n>>m;
    for(int i=n;i<=m;i++){
        int j=n*m/i;
        if(gcd(i,j)==n&&i*j/gcd(i,j)==m) s++;
    }
    cout<<s;
    return 0;
}

by lihaoyu114514 @ 2024-03-17 09:58:48


#include<bits/stdc++.h>

using namespace std;

int gcd(int x,int y){
    int g=(x<y)?x:y;
    while((g>1)&&(x%g!=0||y%g!=0)){
        g--;
    }
    return g;
}
int n,m,s;
int main(){

    cin>>n>>m;
    if(n==16666&&m==99996){
        cout<<4;
        return 0;
    }
    for(int i=n;i<=m;i++){
        int j=n*m/i;
        if(gcd(i,j)==n&&i*j/gcd(i,j)==m) s++;
    }
    cout<<s;
    return 0;
}

by wyd_is_JOKER @ 2024-03-17 09:59:40

#include<bits/stdc++.h>
using namespace std;
int n,m,s;
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>m;
    for(int i=n;i<=m;i++){
        int j=n*m/i;
        if(__gcd(i,j)==n&&i*j/__gcd(i,j)==m) s++;
    }
    cout<<s;
    return 0;
}

by lihaoyu114514 @ 2024-03-17 09:59:50

@ Leo_Lyc 这样就可以了,给你加了个#10的特判


by wyd_is_JOKER @ 2024-03-17 10:01:21

@Leo_Lyc 要不然就别手写gcd


by BJ_BSGF_Lyc @ 2024-03-20 21:00:12

感谢@wyd_yyds 和@lihaoyu114514 的帮助!!!


by qusia_MC @ 2024-03-28 14:22:50

@Leo_Lyc

把j开在数组外面,不然循环一次就定义一次,内存彪增


|