HDU 4135 容斥原理

思路:

直接容斥

//By SiriusRen
#include <cstdio>
using namespace std;
#define int long long
const int N=100050;
int cases,a,b,n,tp,s[N];
int solve(int r){
    int ans=0;
    for(int i=1;i<(1<<tp);i++){
        int tmp=1,t=0;
        for(int j=0;j<tp;j++)
            if(i&(1<<j))tmp*=s[j],t++;
        t=t&1?1:-1;
        ans+=r/tmp*t;
    }
    return r-ans;
}
signed main(){
    scanf("%lld",&cases);
    for(int I=1;I<=cases;I++){
        tp=0;
        scanf("%lld%lld%lld",&a,&b,&n);
        for(int i=2;i*i<=n;i++){
            if(n%i)continue;
            while(n%i==0)n/=i;
            s[tp++]=i;
        }if(n!=1)s[tp++]=n;
        printf("Case #%lld: %lld\n",I,solve(b)-solve(a-1));
    }
}

 

posted @ 2018-08-01 15:43  SiriusRen  阅读(123)  评论(0编辑  收藏  举报