# BZOJ2301: [HAOI2011]Problem b 莫比乌斯反演

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define LL long long
using namespace std;
char c;
{
a=0;do c=getchar();while(c<'0'||c>'9');
while(c<='9'&&c>='0')a=(a<<3)+(a<<1)+c-'0',c=getchar();
}

int check[50005],mu[50005],p[50005],sum[50005];
inline void Mobius()
{
memset(check,0,sizeof(check));
mu[1]=1;
int tot=0;
for (int i=2;i<=50000;i++)
{
if (!check[i])
{
p[++tot]=i;
mu[i]=-1;
}
for (int j=1;j<=tot;j++)
{
if (i*p[j]>50000) break;
check[i*p[j]]=1;
if (i%p[j]==0)
{
mu[i*p[j]]=0;
break;
}
else mu[i*p[j]]=-mu[i];
}
}
sum[0]=0;
for (int i=1;i<=50000;i++)
sum[i]=sum[i-1]+mu[i];
}
int main()
{
Mobius();
int T;
while (T--)
{
int a,b,X,Y,D;
int pos;
Y/=D,b/=D;
if(X%D)
X/=D,X++;
else X/=D;
if(a%D)
a/=D,a++;
else a/=D;

int x=min(a-1,X-1);
LL ans=0LL;
for (int d=1;d<=x;d=pos+1)
{
pos=min((a-1)/((a-1)/d),(X-1)/((X-1)/d));
ans+=(LL)(sum[pos]-sum[d-1])*((a-1)/d)*((X-1)/d);
}
x=min(b,Y);
for (int d=1;d<=x;d=pos+1)
{
pos=min(b/(b/d),Y/(Y/d));
ans+=(LL)(sum[pos]-sum[d-1])*(b/d)*(Y/d);
}
x=min(a-1,Y);
for (int d=1;d<=x;d=pos+1)
{
pos=min((a-1)/((a-1)/d),Y/(Y/d));
ans-=(LL)(sum[pos]-sum[d-1])*((a-1)/d)*(Y/d);
}
x=min(X-1,b);
for (int d=1;d<=x;d=pos+1)
{
pos=min((X-1)/((X-1)/d),b/(b/d));
ans-=(LL)(sum[pos]-sum[d-1])*((X-1)/d)*(b/d);
}
printf("%lld\n",ans);
}
return 0;
}


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