bzoj2301 - 莫比乌斯反演

bzoj2301

#include <bits/stdc++.h>

using namespace std;

const int N=50000+10;

typedef long long LL;
int prime[N],tot;
int mu[N];
int sum[N];
void Mu(){
mu[1]=1;
for (int i=2;i<N;i++){
if (!prime[i]){
prime[++tot] = i;
mu[i]=-1;
}
for (int j=1;prime[j]*i<N;j++){
prime[ prime[j]*i ] = 1;
if (i%prime[j]==0){
mu[i*prime[j]] = 0;
break;
}
mu[i*prime[j]] = -mu[i];
}
}
for (int i=1;i<N;i++)sum[i]=sum[i-1]+mu[i];
}
LL calc(int a,int b){
if (a>b)swap(a,b);
int last=0;
LL ans=0;
for (int i=1;i<=a;i=last+1){
last=min(a/(a/i),b/(b/i));
ans+=(LL)(a/i)*(b/i)*(sum[last]-sum[i-1]);
}
return ans;
}
void work(){
int a,b,c,d,K;
//cin>>a>>b>>c>>d>>K;
scanf("%d%d%d%d%d",&a,&b,&c,&d,&K);
LL ans = calc(b/K,d/K)-calc(b/K,(c-1)/K)-calc((a-1)/K,d/K)+calc((a-1)/K,(c-1)/K);
printf("%lld\n",ans);
//
//cout<<calc(b/K,d/K)-calc(b/K,(c-1)/K)-calc((a-1)/K,d/K)+calc((a-1)/K,(c-1)/K)<<endl;
//cout<<calc(b/K,d/K)<<" "<<calc(b/K,(c-1)/K)<<" "<<calc((a-1)/K,d/K)<<" "<<calc((a-1)/K,(c-1)/K)<<endl;
}
int main()
{
//freopen("1.txt","r",stdin);
Mu();
int Case;cin>>Case;
while (Case--){
work();
}
return 0;
}

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