Files
python/TangDou/LuoGuBook/P1029_3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

47 lines
1.1 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
/**
测试数据:
3 60
答案4
*/
//最大公约数
LL gcd(LL x, LL y) {
return y ? gcd(y, x % y) : x;
}
//最小公倍数
int lcm(int x, int y) {
return y / gcd(x, y) * x; //注意顺序防止乘法爆int
}
int cnt;
LL x; //最大公约数
LL y; //最小公倍数
int main() {
cin >> x >> y;
//理论依据gcd(p,q)*lcm(p,q)=p*q
//枚举最小公倍数y的约数,这个约数可能是p,也可能是y/p
for (LL k = 1; k * k <= y; k++)
if (y % k == 0) {
//可能小因子是p也可能大因子是p,但大小因子别一样,那样会算重了。
LL p = k;
LL q = x * y / k; //q是靠计算出来的。q=x*y/p
if (gcd(p, q) == x) cnt++;
//p=y/k,q=x*y/(y/k)=k*x 两组不能重复,就是 y/k!=k
if (k == y / k)continue;
//p是大因子的可能性
p = y / k;
q = k * x;
if (gcd(p, q) == x) cnt++;
}
cout << cnt << endl;
return 0;
}