hdu4497 GCD and LCM 容斥原理

来源:互联网 发布:北京工业大学 网络 编辑:程序博客网 时间:2024/06/05 16:58

http://acm.hdu.edu.cn/showproblem.php?pid=4497



GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2794    Accepted Submission(s): 1222


Problem 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
 

Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
 

题意:三个数x,y,z,它们的最大公约数是g,最小公倍数是l。求有多少组x,y,z满足要求。

题解:首先想到gcd(x,y,z)=g,那么gcd(x/g,y/g,z/g)=1,lcm(x,y,z)=l/g。所以若l/g!=0,一定无解。现在考虑有解的情况,令x=x/g,y=y/g,z=z/g,根据唯一分解定理:x=p1^a1*p2^a1*...*pk^ak,y=p1^b1*p2^b1*...*pk^bk,,z=p1^c1*p2^c2*...*pk^ck,l/g=p1^m1*p2^m2*...*pk^mk。因为gcd(x,y,z)=1,所以min(ai,bi,ci)=0。因为lcm(ai,bi,ci)=l/g,所以max(ai,bi,ci)=mi。所以ai,bi,ci,一个取0,一个取mi,一个取0-mi。这样之后问题就解决一半了,接下来是怎么计数的问题。可以用到容斥原理,(mi+1)^3为全部的方案,2*mi^3表示三个数字都不含0以及三个数字都不含mi,再加上(mi-1)^3,补上多减的重复的地方。最后答案*6,为排列数。

代码:

#include<bits/stdc++.h>#define debug cout<<"aaa"<<endl#define mem(a,b) memset(a,b,sizeof(a))#define LL long long#define lson l,mid,root<<1#define rson mid+1,r,root<<1|1#define MIN_INT (-2147483647-1)#define MAX_INT 2147483647#define MAX_LL 9223372036854775807i64#define MIN_LL (-9223372036854775807i64-1)using namespace std;const int N = 100000 + 5;const int mod = 1000000000 + 7;int main(){int t,gcd,lcm,n,cnt,ans;scanf("%d",&t);while(t--){scanf("%d%d",&gcd,&lcm);if(lcm%gcd!=0){puts("0");continue;}ans=1;n=lcm/gcd;for(int i=2;i*i<=n;i++){if(n%i==0){cnt=0;while(n%i==0){n/=i;cnt++;}ans=ans*((cnt+1)*(cnt+1)*(cnt+1)-cnt*cnt*cnt*2+(cnt-1)*(cnt-1)*(cnt-1));}}if(n>1){ans*=6;}printf("%d\n",ans);}return 0;}

GCD and LCM

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2794    Accepted Submission(s): 1222


Problem 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
 

Source
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
 

原创粉丝点击