求最大公约数和最小公倍数的标准解法(记住)

1012 最大公约数和最小公倍数问题

 

2001年NOIP全国联赛普及组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 白银 Silver
 
 
 
题目描述 Description

输入二个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数

条件:  1.P,Q是正整数

2.要求P,Q以x0为最大公约数,以y0为最小公倍数.

试求:满足条件的所有可能的两个正整数的个数.

输入描述 Input Description

二个正整数x0,y0

输出描述 Output Description

满足条件的所有可能的两个正整数的个数

样例输入 Sample Input

3 60

样例输出 Sample Output

4

#include <bits/stdc++.h>
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 main() {
    int x,y;
    cin >> x >> y;
    int ans = 0;
    for(int i=x;i <= y;i = i+x)
    {
        int j = x*y/i;
        if(gcd(i,j) == x && lcm(i,j) == y)
            ans++;
    }
    cout << ans <<endl;

}

最大公约数和最小公倍数的乘积和a,b乘积相等

原文地址:https://www.cnblogs.com/cunyusup/p/7738882.html