1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long long LL; 4 int t,cnt,prime[15];LL a,b,n; 5 LL solve(LL x){//求与n不互质的总个数 6 int num;LL ans=0,tp; 7 for(int i=1;i<(1<<cnt);++i){//用二进制来表示每一个质因子是否被使用,即有2^cnt-1种可能,此时cnt较小,题目中1e9最多也就8个素因子,二进制优化 8 tp=1,num=0; 9 for(int j=0;j<cnt;++j) 10 if(i&(1<<j))num++,tp*=prime[j];//表示选择哪几个素因子 11 if(num&1)ans+=x/tp;//奇加 12 else ans-=x/tp;//偶减 13 } 14 return x-ans; 15 } 16 int main(){ 17 while(~scanf("%d",&t)){ 18 for(int i=1;i<=t;++i){ 19 scanf("%lld%lld%lld",&a,&b,&n);cnt=0; 20 for(LL j=2;j*j<=n;++j){//求出n内的全部质因子 21 if(n%j==0){ 22 prime[cnt++]=j; 23 while(n%j==0)n/=j; 24 } 25 } 26 if(n>1)prime[cnt++]=n; 27 printf("Case #%d: %lld\n",i,solve(b)-solve(a-1));//区间差 28 } 29 } 30 return 0; 31 }