P1029 题解
P1029 最大公约数和最小公倍数问题 题解
题面
输入两个正整数
思路
数学分析
如果
我们就可以设
由图片中的短除法,我们就可以得出
于是,我们枚举
相关例题
已知
由
由
由
由
代码
在测试点 4 中,输入为 12 4096,0。
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int x, y;
cin >> x >> y; // 输入
if (y % x > 0) // 特判
{
cout << 0;
return 0;
}
int num = y / x, cnt = 0;
for (int i = 1; i <= num; ++i) // 枚举 y / x 的因数
{
if (num % i == 0 && num % (num / i) == 0)
{
if (__gcd(i, num / i) == 1) // 判断 n, m 是否互素
{
cnt += 1; // 计数
}
}
}
cout << cnt; // 输出答案
return 0;
}