求一个区间和N互质的数的个数。容斥原理,用set存N的质因数。
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
void cmax(int& a,int b){ if(b>a)a=b; }
void cmin(int& a,int b){ if(b<a)a=b; }
typedef long long ll;
const int oo=0x3f3f3f3f;
const ll OO=1LL<<61;
const int MOD=1000007;
const int maxn=100005;
set<ll>g;
ll A,B,N,ans;
ll dfs(set<ll>::iterator s,ll x){
ll res=0;
for(set<ll>::iterator it=s;it!=g.end();++it){
ll t=x/(*it);
it++;
res+=t-dfs(it,t);
it--;
}
return res;
}
void fac(ll n){
g.clear();
for(int i=2;i*i<=n;i++){
while(n%i==0){
g.insert(i);
n/=i;
}
}
if(n>1)
g.insert(n);
}
ll solve(ll n){
ll ans=n-dfs(g.begin(),n);
return ans;
}
int main(){
//freopen("E:\\read.txt","r",stdin);
int T;
scanf("%d",&T);
for(int cas=1;cas<=T;cas++){
scanf("%I64d %I64d %I64d",&A,&B,&N);
fac(N);
ans=solve(B)-solve(A-1);
printf("Case #%d: %I64d\n",cas,ans);
}
return 0;
}