HDOJ 4135 Co-prime 容斥原理
原创
©著作权归作者所有:来自51CTO博客作者hpuhjl的原创作品,请联系作者获取转载授权,否则将追究法律责任
Problem Description
Given a number N, you are asked to count the number of integers between A and B inclusive which are relatively prime to N.
Two integers are said to be co-prime or relatively prime if they have no common positive divisors other than 1 or, equivalently, if their greatest common divisor is 1. The number 1 is relatively prime to every integer.
Input
The first line on input contains T (0 < T <= 100) the number of test cases, each of the next T lines contains three integers A, B, N where (1 <= A <= B <= 1015) and (1 <=N <= 109).
Output
For each test case, print the number of integers between A and B inclusive which are relatively prime to N. Follow the output format below.
Sample Input
2 1 10 2 3 15 5
Sample Output
Case #1: 5 Case #2: 10
题目翻译:T组数据,每组给出A,B,N,求区间【A,B】里面有多少个数与N互质!
解题思路:求出所有的数字的与N不互质的情况,然后用总量减去!求不互质的情况,显然就是利用容斥原理,先求出N的质因子,然后去掉【A,B】中含有这些质因子的数,此时应该注意会重复去掉一些数字,记得加上即可,容斥原理的写法有好几种,感觉每种都很牛,比如用数组实现,DFS实现,以及用位运算实现!
数组实现:
#include<cstdio>
#define LL long long
LL p[10],k;//p数组用来保存n的质因子,LL型n不会超过10个
void getp(LL n){
k=0;
for(LL i=2;i*i<=n;i++){
if(n%i==0) p[k++]=i;
while(n%i==0) n/=i;
}
if(n>1) p[k++]=n;//防止有比根号n大的质因子,k保存质因子个数
}
LL nop(LL m){
LL i,j,que[10000],top=0,t,sum;
que[top++]=-1;//队列数组保存n所有质因子任意不相同组合的乘积
for(i=0;i<k;i++){
t=top;//t保存当前que长度,方便下面的循环来使用
for(j=0;j<t;j++){
que[top++]=que[j]*p[i]*(-1);
}//质因子的个数:奇加偶减,因此乘以-1来换号
}
for(i=1,sum=0;i<top;i++)//sum来累加所有个数
sum+=m/que[i];
return sum;
}
int main(){
LL a,b,n;
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++){
scanf("%lld%lld%lld",&a,&b,&n); //求1-m中多少个数字与n互质
getp(n);//求n的质因子
printf("Case #%d: %lld\n",i,b-nop(b)-(a-1-nop(a-1)));//总数减去
}
return 0;
}
DFS实现:
#include<stdio.h>
#include<math.h>
int p[10],top;
long long ansa,ansb,ans,a,b;
void DFS(int n,bool tag,long long num){
if(n==top){
if(tag==1){
ansa-=a/num;
ansb-=b/num;
}
else{
ansa+=a/num;
ansb+=b/num;
}
return;
}
DFS(n+1,tag,num);
DFS(n+1,!tag,num*p[n]);
}
int main(){
int i,j,n,T,k,cnt;
cnt=1;
scanf("%d",&T);
while(T--){
scanf("%I64d%I64d%d",&a,&b,&n);
a--;
ansa=ansb=0;
top=0;
for(i=2;i*i<=n;i++){
if (n%i==0){
while(n%i==0) n=n/i;
p[top++]=i;
}
}
if(n>1)
p[top++]=n;
DFS(0,0,1);
printf("Case #%d: %I64d\n",cnt++,ansb-ansa);
}
return 0;
}
位运算
#include<cstdio>
#define LL long long
LL p[10],k;//p数组用来保存n的质因子,LL型n不会超过10个
void getp(LL n){
k=0;
for(LL i=2;i*i<=n;i++){
if(n%i==0) p[k++]=i;
while(n%i==0) n/=i;
}
if(n>1) p[k++]=n;//防止有比根号n大的质因子,k保存质因子个数
}
LL nop(LL m){
LL i,j,sum=0,flag,num;
for(i=1;i<1<<k;i++){
flag=0;
num=1;
for(j=0;j<k;j++)
if(i&(1<<j))
flag++,num*=p[j];
if(flag&1) sum+=m/num;
else sum-=m/num;
}
return sum;
}
int main(){
LL a,b,n;
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++){
scanf("%lld%lld%lld",&a,&b,&n); //求1-m中多少个数字与n互质
getp(n);//求n的质因子
printf("Case #%d: %lld\n",i,b-nop(b)-(a-1-nop(a-1)));//总数减去
}
return 0;
}
神递归
#include<cstdio>
#define LL long long
LL p[10],k;//p数组用来保存n的质因子,LL型n不会超过10个
void getp(LL n){
k=0;
for(LL i=2;i*i<=n;i++){
if(n%i==0) p[k++]=i;
while(n%i==0) n/=i;
}
if(n>1) p[k++]=n;//防止有比根号n大的质因子,k保存质因子个数
}
LL nop(LL m,LL t){
LL i,sum=0;
for(i=t;i<k;i++)
sum+=m/p[i]-nop(m/p[i],i+1);
return sum;
}
int main(){
LL a,b,n;
int t,i;
scanf("%d",&t);
for(i=1;i<=t;i++){
scanf("%lld%lld%lld",&a,&b,&n); //求1-m中多少个数字与n互质
getp(n);//求n的质因子
printf("Case #%d: %lld\n",i,b-nop(b,0)-(a-1-nop(a-1,0)));//总数减去
}
return 0;
}