题解:P1029 [NOIP 2001 普及组] 最大公约数和最小公倍数问题
AFO_kunkun127 · · 题解
题意
题目给定两个正整数
-
\gcd(a, b) = x -
\text{lcm}(a, b) = y
其中,
思路
首先,我们需要理解最大公约数和最小公倍数之间的关系。对于任意两个正整数
根据题目条件,
也就是说,
其中
将
因此,我们需要找到所有满足
参考代码
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll x, ll y)
{
if (y == 0)
{
return x;
}
return gcd(y, x % y);
}
int main()
{
ll x, y, cnt = 0;
cin >> x >> y;
ll t = x * y;
for (ll i = 1; i <= sqrt(t); i++)
{
if (t % i == 0 && gcd(i, t / i) == x)
{
cnt += 2;
if (i * i == t) cnt--;
}
}
cout << cnt << endl;
return 0;
}