您的位置:首页 > 其它

hdu 4497 GCD and LCM (唯一分解定理 + 计数)

2016-08-02 21:02 447 查看

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2262    Accepted Submission(s): 1000


[align=left]Problem Description[/align]
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.
 

[align=left]Input[/align]
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.
 

[align=left]Output[/align]
For each test case, print one line with the number of solutions satisfying the conditions above.
 

[align=left]Sample Input[/align]

2
6 72
7 33

 

[align=left]Sample Output[/align]

72
0

 

[align=left]Source[/align]
2013 ACM-ICPC吉林通化全国邀请赛——题目重现

大体题意:

告诉你G和L,求出有多少个(X,Y,Z)对组合 ,满足, 他们的最大公约数为G,最小公倍数是L。

思路:

比赛时没有做出来,借鉴的学长的博客~~,其实推理的差不多了,就是无法实现最后的一步,还是练的少啊= = !

如果L不能整除G的话,那么答案就是0。

否则可以把G,2G,3G,4G,,,,,L 都除以G,变成1,2,3,4,5,,,n。

所以n = L/G。

把n 质因数分解:

得到: p1^t1 + p2^t2 + p3 ^ t3 + .......

那么符合条件的x,y,z满足:

x = p1^i1 + p2 ^i2 + p3 ^ i3 + .....
y = p1^j1 + p2 ^j2 + p3 ^ j3 + .....
z = p1^k1 + p2 ^k2 + p3 ^ k3 + .....

首先x,y,z最大公约数是1的话,那么i1,j1,k1 至少一个为0,至少一个为t1,但又不能全是:

所以所有的组合情况为:

 0 0 t1 --> 总共3种组合

 0 t1 t1 --> 总共3种组合

t1 0 1~t1-1  总共t1 - 1种组合!

那么总组合为  6*t1 - 6 + 3+ 3 = 6*t1种组合!

那么用唯一分解定理分解n 即可! 枚举t!

n 最大是int 2^ 31  那么只需要枚举 2^16进行分解即可!

详细见代码:
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int maxn = 1000 + 10;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;

int len = 1 << 16;

int main(){
int T;
scanf("%d",&T);
while(T--){
int a,b;
scanf("%d %d",&a,&b);
if (b % a != 0){
printf("0\n");
continue;
}
ll ans = 1ll;
int n = b/a;
for (int i = 2; i <= len; ++i){
if (n % i == 0){
int sum = 0;
while(n % i == 0)n /= i,++sum;
ans *= sum*6;
}
}
if (n > 1)ans *= 6;
printf("%I64d\n",ans);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: