裸懵逼乌斯反演,稍微再容斥一下
#include<bits/stdc++.h>
#define MAXN 50001
using namespace std; int n,a,b,c,d,k;
int mu[MAXN],sum[MAXN];
int p[MAXN],tag[MAXN];
void init(){
mu[1]=sum[1]=1;
for(int i=2;i<MAXN;++i){
if(!tag[i])
p[++p[0]]=i,mu[i]=-1;
for(int j=1;j<=p[0];++j){
int to=i*p[j];
if(to>=MAXN) break;
tag[to]=1,mu[to]=-mu[i];
if(!(i%p[j])){
mu[to]=0;
break;
}
}
sum[i]=sum[i-1]+mu[i];
}
}
long long calc(int x,int y){
if(x>y) swap(x,y);
long long ans=0;
for(int L=1,R=0;L<=x;L=R+1){
R=min(x/(x/L),y/(y/L));
ans+=1ll*(sum[R]-sum[L-1])*(x/L)*(y/L);
}
return ans;
}
int main(){
init();
scanf("%d",&n);
while(n--){
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
printf("%lld\n",calc((a-1)/k,(c-1)/k)+calc(b/k,d/k)
-calc((a-1)/k,d/k)-calc(b/k,(c-1)/k));
}
return 0;
}