【BZOJ2301】【HAOI2011】—Problem b(莫比乌斯反演)
题意:求
直接看那个的做法就可以了
容斥一下就可以了
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
inline int read(){
char ch=getchar();
int res=0,f=1;
while(!isdigit(ch)){if(ch=='-')f=-f;ch=getchar();}
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=getchar();
return res*f;
}
const int N=100005;
int vis[N],pr[N],mu[N],sum[N],tot;
inline void init(){
mu[1]=1;
for(int i=2;i<N;i++){
if(!vis[i])pr[++tot]=i,mu[i]=-1;
for(int j=1;j<=tot&&i*pr[j]<N;j++){
vis[pr[j]*i]=1;
if(i%pr[j]==0)break;
mu[i*pr[j]]=-mu[i];
}
}
for(int i=1;i<N;i++)sum[i]=sum[i-1]+mu[i];
}
inline ll calc(int b,int d){
ll ans=0;int p=min(b,d);
for(int i=1,nxt;i<=p;i=nxt+1){
nxt=min((b/(b/i)),(d/(d/i)));
ans+=(ll)(sum[nxt]-sum[i-1])*(b/i)*(d/i);
}
return ans;
}
signed main(){
int T=read();init();
for(int cas=1;cas<=T;cas++){
int a=read(),b=read(),c=read(),d=read(),k=read();
if(k==0){puts("0");continue;}
ll ans=0;a=(a-1)/k,b/=k,c=(c-1)/k,d/=k;
ans=calc(b,d)-calc(b,c)-calc(a,d)+calc(a,c);
cout<<ans<<'\n';
}
}