# [BZOJ2301]Problem b 莫比乌斯反演+容斥

i=1nj=1mgcd(i,j)==k$\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}gcd(i,j)==k$

F(d)=i=1nj=1m(d|gcd(i,j))$F(d)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^{m}(d|gcd(i,j))$

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
bool vis[50001];
int mu[50001],prime[50001],n,a,b,c,d,k;
ll sum[50001];
void get_mobious()
{int i,j;
memset(vis,0,sizeof(vis));
mu[1]=1;
int tot=0;
for (i=2;i<=50000;i++)
{
if (vis[i]==0)
{
prime[++tot]=i;
mu[i]=-1;
}
for (j=1;j<=tot;j++)
{
if (i*prime[j]>50000) break;
vis[i*prime[j]]=1;
if (i%prime[j]==0)
{
mu[i*prime[j]]=0;
break;
}
else mu[i*prime[j]]=-mu[i];
}
}
sum[0]=0;
for (i=1;i<=50000;i++)
sum[i]=sum[i-1]+mu[i];
}
ll cal(int x,int y)
{int r,i,pos;
ll s=0;
r=min(x,y);
for (i=1;i<=r;i=pos+1)
{
pos=min(x/(x/i),y/(y/i));
s+=(sum[pos]-sum[i-1])*(x/i)*(y/i);
}
return s;
}
int main()
{
cin>>n;
get_mobious();
while (n--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
ll ans1=cal(b/k,d/k);
ll ans2=cal((a-1)/k,d/k);
ll ans3=cal(b/k,(c-1)/k);
ll ans4=cal((a-1)/k,(c-1)/k);
printf("%lld\n",ans1-ans2-ans3+ans4);
}
}

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