90pts求调

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

Jack_2011_Liu @ 2024-07-27 17:42:56

#include <cstdio>
#include <iostream>
#include <cmath>
using namespace std;
long long gcd(long long a, long long b)
{
    if (b == 0) return a;
    else return gcd(b, a % b);
}
long long lcm(long long a,long long b)
{
    return a * b / gcd(a, b);
}
bool issquare(long long inp)
{
    if (pow(ceil(sqrt(inp)), 2) == inp) return true;
    else return false;
}
int main()
{
    long long x, y;
    cin >> x >> y;
    long long xy = x * y;
    long long ans = 0;
    for (long long i = x; i * i <= xy; i += x)
    {
        if (gcd(i, xy / i) == x && lcm(i, xy / i) == y)
        {
            ans += 2;
        }
    }
    if (issquare(xy) && (long long)sqrt(xy) % x == 0 && y % (long long)sqrt(xy) == 0) ans--;
    cout << ans << endl;
    return 0;
}

第三个测试点过不了


by haimingbei @ 2024-07-27 17:44:53

@Jack_2011_Liu 感觉比你的好懂一些

#include<bits/stdc++.h>
using namespace std;
int main(){
    int x0,y0,ans=0;
    cin>>x0>>y0;
    int n=x0*y0;
    for(int i=x0;i<=y0;i++){
        if(n%i==0){
            int a=n/i;
            bool f=1;
            if(a%x0==0 && i%x0==0 && y0%a==0 && y0%i==0){
                int x=min(i,a),y=max(i,a);
                for(int j=x0+1;j<=x;j++)
                    if(i%j==0 && a%j==0)f=0;
                for(int k=y;k<y0;k++)
                    if(k%i==0 && k%a==0)f=0;
                if(f==1)ans++;
            }   
        }
    }
    cout<<ans;
    return 0;
}

by Jack_2011_Liu @ 2024-07-27 17:46:20

@haimingbei 这遇到大数据不会TLE吗


by haimingbei @ 2024-07-27 17:47:58

@Jack_2011_Liu 数据范围就那么多,你提交试试


by ydx202345 @ 2024-08-05 09:27:01

#include<bits/stdc++.h>
using namespace std;
int pou(int a,int b){
    if(a==0)    return b;
    if(b==0)    return a;
    if(a<b) return pou(b%a,a);
    if(a>b) return pou(a%b,b);
    return a;
}
int main(){
    int x0,y0,cnt=0;
    cin>>x0>>y0;
    for(int P=x0;P<=y0;P++){
        int Q=x0*y0/P;
        int r=pou(P,Q);
        if(P*Q==x0*y0 && r==x0){
            cnt++;      
        }
    }
    cout<<cnt<<endl;
    return 0;
}

by ydx202345 @ 2024-08-05 09:28:11

longlong不需要


|