天天看點

hdu 4135 Co-prime【容斥原理】

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 <= 10 15) and (1 <=N <= 10 9).

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
           
Hint
In the first test case, the five integers in range [1,10] which are relatively prime to 2 are {1,3,5,7,9}. 
           

本篇文章轉自: https://yq.aliyun.com/articles/307808

題目翻譯:T組資料,每組給出A,B,N,求區間【A,B】裡面有多少個數與N互質!

解題思路:求出所有的數字的與N不互質的情況,然後用總量減去!求不互質的情況,顯然就是利用容斥原理,先求出N的質因子,然後去掉【A,B】中含有這些質因子的數,此時應該注意會重複去掉一些數字,記得加上即可,容斥原理的寫法有好幾種,感覺每種都很牛,比如用數組實作,DFS實作,以及用位運算實作!

歐拉函數得到的是小于n與n互質的個數,這裡是個區間。由于區間較大,不可能對[a,b]進行周遊,

考慮計算區間[1,a-1]中與n互質的個數num1,[1,b]中與n互質的個數num2,最終結果就是兩者

相減的結果。

現在考慮如何計算區間[1,m]中n互質的個數num,num等于 (m - 與n不互質的個數)。

與n不互質的數就是[1,m]中n的素因子的倍數。

例如m = 12,n = 30的情況。

30的素因子數為2、3、5。

[1,12]中含有2的倍數的有:(2、4、6、8、10、12) = n/2 = 6個

[1,12]中含有3的倍數的有:(3、6、9、12) = n/3 = 4個

[1,12]中含有5的倍數的有:(5、10) = n/5 = 2個

與n不互質的數個數就是上邊三個集合取并集的部分。這裡用到了容斥定理,我用的增長的隊列數組

來實作容斥定理,

數組實作:

#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;
}
           

繼續閱讀