hdu4497 GCD and LCM 容斥原理

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
 

题意:三个数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
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值