您的位置:首页 > 其它

HDU 4497 GCD and LCM(分解质因子+排列组合)

2015-09-04 10:26 274 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4497

题意:已知GCD(x, y, z) = G,LCM(x, y, z) = L。告诉你G、L,求满足要求的(x, y, z)有多少组,并且要考虑顺序。

思路:如果L%G != 0显然不存在这样的(x, y, z),相反肯定存在。具体做法就是将L/G分解质因子,得到:L/G = P1^t1 * P2^t2 * ... * Pk^tk,我们来考虑任意一个因子Pi^ti,此时(x/G, y/G, z/G) = (x0, y0,z0)将x0,y0,z0分别分解质因子,得到对应位为Pi^t1, Pi^t2,Pi^t3,为了满足要求,(t1, t2, t3)中一定有一个为0(三个数互质),(t1, t2, t3)中一定有一个为ti且剩下的一个小于等于ti(三个数的最小公倍数为L/G)。min(t1, t2, t3) = 0, max(t1, t2, t3) = ti,所有情况就为6*ti(排列组合或者容斥原理),最后的答案就是(6*t1) * (6*t2) * ... * (6*tk)。

code:

#include <cstdio>

int main()
{
int nCase;
scanf("%d", &nCase);
while (nCase--) {
int L, G;
scanf("%d %d", &G, &L);
if (L % G) {
printf("0\n");
continue;
}
L /= G;
int ans = 1;
for (int i = 2; i * i <= L; ++i) {
if (L % i == 0) {
int t = 0;
while (L % i == 0) {
L /= i;
++t;
}
ans *= (6 * t);
}
}
if (L != 1) ans *= 6;
printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: