hdu 4135 Co-prime【容斥原理】,hdu4135co-prime
Co-primeTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1668 Accepted Submission(s): 636
Problem DescriptionGiven 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.
InputThe 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).
OutputFor 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
21 10 23 15 5
Sample Output
Case #1: 5Case #2: 10題目翻譯:T組資料,每組給出A,B,N,求區間【A,B】裡面有多少個數與N互質!解題思路:求出所有的數位與N不互質的情況,然後用總量減去!求不互質的情況,顯然就是利用容斥原理,先求出N的質因子,然後去掉【A,B】中含有這些質因子的數,此時應該注意會重複去掉一些數字,記得加上即可,容斥原理的寫法有好幾種,感覺每種都很牛,比如用數組實現,DFS實現,以及用位元運算實現!數組實現:#include<iostream>#include<string.h>#include<stdio.h>using namespace std;__int64 a[10],num;void init(__int64 n){ //求一個數的質因子 __int64 i; num=0; for(i=2;i*i<=n;i++){ if(n%i==0){ a[num++]=i; while(n%i==0) n=n/i; } } if(n>1) a[num++]=n; //這裡要記得}__int64 Noprime(__int64 m){ //用隊列數組實現容斥原理 __int64 que[10000],i,j,k,t=0,sum=0; que[t++]=-1; for(i=0;i<num;i++){ k=t; for(j=0;j<k;j++) que[t++]=que[j]*a[i]*(-1); } for(i=1;i<t;i++) sum=sum+m/que[i]; return sum;}int main(){ __int64 T,x,y,n,i=1,sum; scanf("%I64d",&T); while(T--){ scanf("%I64d%I64d%I64d",&x,&y,&n); init(n); sum=y-Noprime(y)-(x-1-Noprime(x-1)); printf("Case #%I64d: ",i++); printf("%I64d\n",sum); } 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<iostream>#include<cstdio>#include<cstring>using namespace std;int p[10];void Init(int m){ int i,n=0; for(i=2;i*i<=m;i++) if(m%i==0){ p[++n]=i; while(m%i==0)m/=i; } if(m!=1) p[++n]=m; p[0]=n;//p[0]儲存p數組元素個數}__int64 Noprime(int N,__int64 x,int t){ __int64 i,ret=0; for(i=t;i<=p[0];i++)//此處第一次i從1開始,以後每次加1; ret+=x/p[i]-Noprime(N,x/p[i],i+1); return ret;}int main(){ int t,T,N; __int64 A,B; scanf("%d",&T); for(t=1;t<=T;t++){ scanf("%I64d%I64d%d",&A,&B,&N); Init(N); printf("Case #%d: %I64d\n",t,B-Noprime(N,B,1)-(A-1-Noprime(N,A-1,1))); } return 0;}位元運算
#include<iostream>#include<cstdio>#include<cstring>using namespace std;#define LL long longLL prime[9];LL Noprime(LL num,int m){LL ans=0,tmp,i,j,flag;for(i=1;i<(LL)(1<<m);i++){ //用二進位來1,0來表示第幾個素因子是否被用到,如m=3,三個因子是2,3,5,則i=3時二進位是011,表示第2、3個因子被用到tmp=1,flag=0;for(j=0;j<m;j++)if(i&((LL)(1<<j)))//判斷第幾個因子目前被用到flag++,tmp*=prime[j];if(flag&1)//容斥原理,奇加偶減ans+=num/tmp;elseans-=num/tmp;}return ans;}int main(){int T,t=0,m;LL n,a,b,i;scanf("%d",&T);while(T--){scanf("%I64d%I64d%I64d",&a,&b,&n);m=0;for(i=2;i*i<=n;i++) //對n進行素因子分解if(n&&n%i==0){prime[m++]=i;while(n&&n%i==0)n/=i;}if(n>1)prime[m++]=n;printf("Case #%d: %I64d\n",++t,(b-Noprime(b,m))-(a-1-Noprime(a-1,m)));}return 0;}