# 【BZOJ】【P2301】【HAOI2011】【Problem b】【题解】【莫比乌斯反演】

Code:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cctype>
using namespace std;
typedef long long LL;
const int maxn=50005;
int T,n,m;
bool p[maxn];
int u[maxn],prime[maxn];
int getint(){
int res=0;char c=getchar();
while(!isdigit(c))c=getchar();
while(isdigit(c))res=res*10+c-'0',c=getchar();
return res;
}
void init(){
u[1]=1;
for(int i=2;i<maxn;i++){
if(!p[i]){
prime[++prime[0]]=i;u[i]=-1;
}for(int j=1;i*prime[j]<maxn&&j<=prime[0];j++){
int K=i*prime[j];
p[K]=1;
if(i%prime[j]==0){
u[K]=0;break;
}else u[K]=-u[i];
}
}for(int i=2;i<maxn;i++)u[i]+=u[i-1];
}
LL calc(int n,int m,int k){
n/=k;m/=k;
if(n>m)swap(n,m);
LL ans=0;
for(int i=1,last;i<=n;i=last+1){
last=min(n/(n/i),m/(m/i));
ans+=(LL)(u[last]-u[i-1])*(n/i)*(m/i);
}return ans;
}
int main(){
T=getint();
init();
while(T--){
int a=getint(),b=getint(),c=getint(),d=getint(),k=getint();
LL ans=calc(b,d,k)-calc(a-1,d,k)-calc(b,c-1,k)+calc(a-1,c-1,k);
printf("%lld\n",ans);
}
return 0;
}

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客