题目链接:Click here

Solution:

简单容斥,我们先把\(N\)分解质因数,我们知道\(1\sim x\)里能整除\(i\)的数的个数为\(\lfloor \frac{x}{i} \rfloor\),那么直接容斥即可

Code:

#include<bits/stdc++.h>
using namespace std;
int tim,cnt,p[11];
long long read(){
    long long x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
    while(isdigit(ch)){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
long long calc(long long Lim){
    int M=1<<cnt;long long v=1,ans=Lim;
    for(int i=1;i<M;i++){
        int re=0;v=1;
        for(int j=1;j<=cnt;j++)
            if((i>>(j-1))&1) v=v*1ll*p[j],++re;
        long long tmp=Lim/v;
        if(re&1) ans=ans-tmp;
        else ans=ans+tmp;
    }return ans;
}
void solve(){
    long long l=read(),r=read(),n=read();
    cnt=0;
    for(int i=2;i*i<=n;i++){
        if(n%i) continue;
        p[++cnt]=i;
        while(n%i==0) n/=i;
    }
    if(n!=1) p[++cnt]=n;
    printf("Case #%d: %lld\n",++tim,calc(r)-calc(l-1));
}
signed main(){
    int t=read();
    while(t--) solve();
    return 0;
}
02-01 01:32