Luo Gu P1029 [NOIP2001 Popularization Group] The problem of the greatest common divisor and the least common multiple

Luo Gu P1029 [NOIP2001 Popularization Group] The problem of the greatest common divisor and the least common multiple

1. The product of the greatest common divisor and the least common multiple is the product of the original two numbers

2. Euclid's algorithm is also called to find the greatest common divisor

#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

//朴素方法的原理:
//枚举一个数,判断它能否被读入的两个数的积整除,
//如果可以再判断它和两个数的积除以它所得的数
//的最大公约数和读入的最大公约数是否相同,如果相同则ans++

long long x,y,ans=0;

int gcd(int a,int b){
    
    
    if(a%b==0)return b;
    else gcd(b,a%b);
}
int main(){
    
    
    cin>>x>>y;//x为最大公约数 y为最小公倍数
    if(x==y)ans--;
    long long mul=x*y;
    for(int i=1;i<=sqrt(mul);i++){
    
    
        if(mul%i==0&&gcd(i,mul/i)==x) ans+=2;
    }
    cout<<ans;
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325982897&siteId=291194637