第四个测试点错了操,有没有大佬帮忙看看

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

RicardoLuang @ 2024-12-07 12:42:54

#include <iostream>
using namespace std;

int gcd(int a, int b) {
    while (b != 0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int main() {
    int x0, y0;
    cin >> x0 >> y0;

    int n = y0 / x0;

    int count = 0;
    for (int k1 = 1; k1 * k1 <= n; ++k1) {
        if (n % k1 == 0) {
            int k2 = n / k1;
            if (gcd(k1, k2) == 1) {
                if (k1 != k2) {
                    count += 2;
                } else {
                    count += 1;
                }
            }
        }
    }

    cout << count << endl;
    return 0;
}

by RicardoLuang @ 2024-12-07 12:44:42

90分求调


by xc_ty @ 2024-12-07 12:56:43

@RicardoLuang 我看看


by xc_ty @ 2024-12-07 13:05:14

@RicardoLuang

#include <cstdio>

using namespace std;

int gcd(int a,int b)
{

    if (b==0) return a;
    return gcd(b,a%b);

}

int lcm(int a,int b)
{

    return (a*b)/gcd(a,b);  

} 

int x,y,q,t;

int main()
{

    scanf("%d%d",&x,&y);

    for (int i=x; i<=y; i++)
    {

        q=y/(i/x);

        if (gcd(i,q)==x && lcm(i,q)==y) t++;

    }

    printf("%d",t);

    return 0;

}

这是AC代码


by xc_ty @ 2024-12-07 13:08:32

@RicardoLuang 你增加count时,应该是gcd(k1,k2)==x而不是1,他要求最大公因数不是最小公因数


by xc_ty @ 2024-12-07 13:15:54

@RicardoLuang

#include <iostream>
using namespace std;

int gcd(int a, int b) {
    while (b!=0) {
        int temp = b;
        b = a % b;
        a = temp;
    }
    return a;
}

int lcm(int a,int b)
{

    return (a*b)/gcd(a,b);  

} 

int main() {
    int x0, y0;
    cin >> x0 >> y0;

    int count = 0;
    for (int k1 = x0; k1<= y0; ++k1) {
        int k2 = y0 / (k1/x0);
        if (gcd(k1, k2) == x0 && lcm(k1,k2)==y0){
            count += 1;
        }

    }

    cout << count << endl;
    return 0;
}

这是对你程序的修改,首先for循环左边界是x0,有边界是y0,而且不仅要判断最大公因数,还要最小公倍数


by xc_ty @ 2024-12-07 13:18:24

@RicardoLuang p,q如果相等,那么x0,y0也相等,那么只有1种情况,你加一下我好友,我跟你详细说


by RicardoLuang @ 2024-12-07 13:28:26

@xc_ty 斯,报错了喵


by xc_ty @ 2024-12-07 13:35:54

@RicardoLuang 我发给你的两个代码都AC了


|