洛谷P1029 最小公约数和最大公倍数问题【数论】

时间:2022-05-07 09:01:14

题目https://www.luogu.org/problemnew/show/P1029

题意:

给定两个数$x$和$y$,问能找到多少对数$P$$Q$,使得他们的最小公约数是$x$最大公倍数是$y$

思路:

我们知道两个数的最小公倍数是他们的乘积除以最大公约数。

也就是说我们可以把$P,Q$表示成

$P = k_1x, Q = k_2x, y = \frac{PQ}{x}$

即$k_{1}k_{2}x = y$,且$k_1,k_2$互质

那么我们只用在$\frac{x}{y}$中找到有多少互质的因子就可以了。

要注意可能$y$不整除$x$,答案就是0

 #include<stdio.h>
#include<stdlib.h>
#include<map>
#include<set>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cmath>
#include<queue> #define inf 0x7f7f7f7f
using namespace std;
typedef long long LL;
typedef pair<int, int> pr; LL x, y;
LL gcd(LL a, LL b)
{
if(b == )return a;
else{
return gcd(b, a % b);
}
} int main()
{
scanf("%lld %lld", &x, &y);
if(y % x) {
printf("0\n");
return ;
}
LL k1k2 = y / x;
int ans = ;
for(LL k1 = ; k1 <= sqrt(k1k2); k1++){
if(k1k2 % k1)continue;
LL k2 = k1k2 / k1;
if(gcd(k1 * x, k2 * x) == x){
ans++;
//printf("%lld %lld\n", k1, k2);
}
}
printf("%d\n", ans * ); return ;
}