求解

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

Yangzikun1125 @ 2024-12-19 13:59:43

#include <iostream>
#include <stack>
#include <queue>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int lcm(int a,int b)
{
    int temp=a*b;
    temp=temp/__gcd(a,b);
    return temp;
}

int p,q,x,y,sum;
int main ()
{
    cin>>x>>y;
    for (int i=min(x,y);i<=max(x,y);i++)
    {
        for (int j=min(x,y);j<=max(x,y);j++)
        {
            if(__gcd(i,j)==x&&lcm(i,j)==y)
            {
                sum++;
            }
        }   
    }
    cout<<sum;
    return 0;
}

by long_long_2014 @ 2024-12-20 14:00:49

#include<iostream>
#include<math.h>
using namespace std;
int gcd1(int a1,int b1)
{
    int r;
    if(a1<b1)
       swap(a1,b1);
    while(a1%b1!=0)
    {
        r=a1%b1;
        a1=b1;
        b1=r; 
    }
    return b1;
}

int main()
{
    int a,b,c,i,j,p,q,sum;
    cin>>a>>b;
    sum=0;

    if (a>b)
       swap(a,b);
    if (b%a==0)
    {
        c=b/a;
        for(i=1;i<=c;i++) 
        {
            if(c%i==0)
            {
                p=i;
                q=c/i;
                j=gcd1(p,q);
                if(j==1)    
                    sum++;
            } 
        }   
    }
    cout<<sum<<endl;
    return 0;
}

|