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.
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.
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吉林通化全国邀请赛——题目重现
题解:首先想到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.
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.
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吉林通化全国邀请赛——题目重现
阅读全文
0 0
- hdu4497 GCD and LCM 容斥原理
- hdu4497——GCD and LCM(数论&容斥原理or排列组合)
- hdu4497 GCD and LCM
- hdu4497 GCD and LCM
- hdu4497 lcm and gcd
- HDU4497 GCD and LCM
- HDU4497 GCD and LCM 数论 素数分解
- HDU4497:GCD and LCM(数论)
- hdu 4497 GCD and LCM 容斥原理
- hdu 4497 GCD and LCM(唯一分解+容斥原理)
- GCD and LCM 质因数分解与容斥原理
- Hdu4497 GCD and LCM 素数筛法+分解质因数
- GCD 容斥原理
- 唯一分解定理+容斥-HDU4497
- GCD and LCM
- hdu5341 Gcd and Lcm
- GCD and LCM Aizu
- The GCD and LCM
- 模板的分离编译问题
- C++(if,switch语句)
- 1117. Eddington Number(25)
- pulltorefresh 上拉刷新 下拉加载
- HDU-2544-最短路
- hdu4497 GCD and LCM 容斥原理
- MapReduce再学习:资源管理框架YARN
- 小白成长记——Android进阶之线程thread的两种实现
- python3爬虫数据解析实战
- 【多媒体编解码】AAC
- 排序算法之堆排序
- HDU 1496 Equations(简单哈希)
- 如何利用Halcon中的快速傅里叶变换(FFT)
- 数据库系统原理(自考4、5、6、7)