打暴力,求优化(RE

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

hhy8399 @ 2024-09-06 18:42:33

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

int x,y,ans;
int pd[10000][10000];

int lcm(int a, int b)
{
    int temp = a * b;
    temp = temp / __gcd(a, b);
    return temp;
}

int main()
{
    cin >> x >> y;
    for(int i = 1;i <= y;i++)
    {
        for(int j = 1;j <= y;j++)
        {
            if(pd[j][i] == 2)
            {
                ans ++;
                continue;
            }
            if(__gcd(i,j) == x && lcm(i,j) == y)
            {
                ans ++;
                pd[i][j] = 2;
                continue;
            }
            pd[i][j] = 1;
        }
    }
    cout << ans << endl;
    return 0;
}

by lhz2022 @ 2024-09-06 19:08:11

10^5 你开 10^4 不re才怪了呢


|