BZOJ 2301 Problem b 发表于 2017-01-05 | 更新于 2018-06-17 LinkSolution裸的反演 Code1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859//Code by Lucida#include<bits/stdc++.h>#define red(x) scanf("%d",&x)//#define debug(x) std::cout<<#x<<'='<<x<<std::endltemplate <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}const int N=5e4,MAXN=N+10;typedef long long LL;using std::min;using std::swap;int mu[MAXN],prime[MAXN],pcnt,Mu[MAXN];void Prep(){ static bool notp[MAXN]; mu[1]=1; for(int i=2;i<=N;i++) { if(!notp[i]) { prime[++pcnt]=i; mu[i]=-1; } for(int j=1;j<=pcnt && prime[j]*i<=N;j++) { notp[i*prime[j]]=1; if(i%prime[j]==0) { mu[i*prime[j]]=0; break; } else mu[i*prime[j]]=-mu[i]; } } for(int i=1;i<=N;i++) Mu[i]=Mu[i-1]+mu[i];}LL Count(int x,int y)//互质的个数{ LL res=0; if(x>y) swap(x,y); for(int i=1,last=1;i<=x;i=last+1) { last=min(x/(x/i),y/(y/i)); res+=(LL)(Mu[last]-Mu[i-1])*(x/i)*(y/i); } return res;}int main(){ //freopen("input","r",stdin); Prep(); int T;red(T); while(T--) { int a,b,c,d,K;red(a),red(b),red(c),red(d),red(K); printf("%lld\n",Count(b/K,d/K)+Count((a-1)/K,(c-1)/K)-Count((a-1)/K,d/K)-Count(b/K,(c-1)/K)); } return 0;}