您的位置:首页 > 其它

HDU5579 LCM & 唯一分解定理 & 排列组合

2017-10-14 23:57 375 查看
Description

Given two positive integers G and L, could you tell me how many solutions of (x, y, z) there are, satisfying that gcd(x, y, z) = G and lcm(x, y, z) = L?

Note, gcd(x, y, z) means the greatest common divisor of x, y and z, while lcm(x, y, z) means the least common multiple of x, y and z.

Note 2, (1, 2, 3) and (1, 3, 2) are two different solutions.
 

Input

First line comes an integer T (T <= 12), telling the number of test cases.

The next T lines, each contains two positive 32-bit signed integers, G and L.

It’s guaranteed that each answer will fit in a 32-bit signed integer.
 

Output

For each test case, print one line with the number of solutions satisfying the conditions above.
 

Sample Input

2

6 72
7 33

Sample Output

72

0

题目大意:
给你两个数L,G,问你有多少有序数组(x,y,z)满足GCD(x,y,z)=G,LCM(x,y,z)=L,首先如果gcd(x,y,z)=G,
思路分析:
当这样的组合存在的时候,所求与  求gcd(x,y,z)=1且lcm(x,y,z)=L/G的方法数是等价的。
  那么:令temp=L/G。

  对temp进行素数分解:temp=p1^t1 * p2^t2 * ……* pn^tn。

  因为temp是这三个数的倍数,因而x,y,z的组成形式为:

  x=p1^i1 * p2^i2 *…… * pn^in;

  y=p1^j1 * p2^j2 *…… * pn^jn;

  z=p1^k1 * p2^k2 * …… * pn^kn;

  对于某一个素因子p:

          因为要满足x,y,z的最大公约数为1,即三个数没有共同的素因子,所以min(i,j,k)=0。

          又因为要满足x,y,z的最小公倍数为temp,即p^t必然要至少存在一个,所以max(i,j,k)=t。

          换言之:至少要有一个p^t,以满足lcm的要求;至多有两个包含p,以满足gcd的要求。

          因而基本的组合方式为(0,p^t,p^k),k=0-->t。

          而因为(1,2,3)和(2,1,3)是不同的方法,所有满足要求的方法中,除了(0,0,t)和(0,t,t)各有3种排列之外,其余的(0,x,t)(1<=x<=t-1)有6种排列。

          对于某一个素因子p总的方法数为6*(t-1)+2*3=6*t。

  在根据组合排列的知识,素数与素数之间是分步的关系,因而总的方法数为:6*t1*6*t2*6*t3*...*6*tn

代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <set>
using namespace std;
typedef long long LL;
const int maxn = 2e5;
int p[maxn];
int main()
{
int T;
scanf("%d", &T);

while(T--)
{
LL L, G;
scanf("%lld%lld", &G, &L);
LL temp = L/G;
int x = sqrt(temp+0.5);
memset(p, 0, sizeof(p));
for(int i = 2; i <= x; i++)
{
while(temp%i ==0)
{
temp/=i;
p[i]++;
}
}
LL ans;
if(temp != 1) ans = 6;//如果L/G本身是素数
else ans = 1;
for(int i = 2; i <= x; i++)
{
if(p[i])
ans *= p[i]*6;
}
if(L%G)
printf("0\n");
else
printf("%lld\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: