hdu4135(容斥原理求质数,队列实现)

传送门

 

ac代码(队列实现版本):

#include<bits/stdc++.h>
#define per(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long ll;
//#define int long long
const ll inf =2333333333333333LL;
const double eps=1e-8;
int read(){
    char ch=getchar();
    int res=0,f=0;
    while(ch<'0' || ch>'9'){f=(ch=='-'?-1:1);ch=getchar();}
    while(ch>='0'&&ch<='9'){res=res*10+(ch-'0');ch=getchar();}
    return res*f;
}
// ------------------------head
#define mod 1000000007
const int N=1000005;
int T,zhi[16],zcnt;
void init(int n){
    zcnt=0;
    for(int i=2;i*i<=n;i++){
        if(n%i==0){
            zhi[zcnt++]=i;
            while(n%i==0)n/=i;
        }
    }
    if(n>1){zhi[zcnt++]=n;}
}
ll func(ll m){
    ll que[10000],qcnt=0;
    int k;
    que[qcnt++]=-1;
    per(i,0,zcnt-1){
        k=qcnt;
        per(j,0,k-1){
            que[qcnt++]=que[j]*zhi[i]*(-1);
        }
    }
    ll sum=0;
    per(i,1,qcnt-1)sum+=m/que[i]; 
    return sum;
}

signed main()
{
    ll a,b;
    int n;
    scanf("%d",&T);
    int cas=0;
    while(T--){
        cas++;
        scanf("%lld %lld %d",&a,&b,&n);
        init(n);
        printf("Case #%d: %lld\n",cas,b-func(b)-(a-1-func(a-1)));
    }

    return 0;
}

 

posted @ 2018-09-22 15:31  WindFreedom  阅读(329)  评论(0编辑  收藏  举报