# [莫比乌斯反演] BZOJ 2301 [HAOI2011]Problem b

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;

inline char nc(){
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}

char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int maxn=50000;
int mobius[50005],sum[50005];
int vst[50005],prime[50005],num=0;

inline void Pre()
{
mobius[1]=1;
for (int i=2;i<=maxn;i++)
{
if (!vst[i])
mobius[i]=-1,prime[++num]=i;
for (int j=1;j<=num && prime[j]*i<=maxn;j++)
{
vst[i*prime[j]]=1;
if (i%prime[j]==0)
{
mobius[prime[j]*i]=0; break;
}
else
mobius[prime[j]*i]=-mobius[i];
}
}
for (int i=1;i<=maxn;i++)
sum[i]=sum[i-1]+mobius[i];
}

int K;

inline int F(int n,int m)
{
n/=K; m/=K;
if (n>m) swap(n,m);
int ret=0,j;
for (int i=1;i<=n;i=j+1)
{
j=min(n/(n/i),m/(m/i));
ret+=(sum[j]-sum[i-1])*(n/i)*(m/i);
}
return ret;
}

int main()
{
int Q,a,b,c,d,ans;
Pre();
while (Q--)
{
ans=F(b,d)+F(a-1,c-1)-F(a-1,d)-F(b,c-1);
printf("%d\n",ans);
}
return 0;
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客