P2522 [HAOI2011]Problem b
题意
与这题相同,只不过加了个容斥。
\(ans=solve(b,d)-solve(a-1,d)-solve(b,c-1)+solve(a-1,c-1)\)
画个这个图就显然了:
code:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=50010;
int T,a,b,c,d,K;
int mu[maxn],sum[maxn];
bool vis[maxn];
vector<int>prime;
inline void shai(int n)
{
vis[1]=1;mu[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i])prime.push_back(i),mu[i]=-1;
for(unsigned int j=0;j<prime.size()&&i*prime[j]<=n;j++)
{
vis[i*prime[j]]=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];
}
inline ll solve(int a,int b,int K)
{
ll res=0;
for(int l=1,r;l<=min(a/K,b/K);l=r+1)
{
r=min(a/(a/l),b/(b/l));
res+=1ll*(a/(l*K))*(b/(l*K))*(sum[r]-sum[l-1]);
}
return res;
}
int main()
{
shai(50000);
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d%d%d",&a,&b,&c,&d,&K);
printf("%lld\n",solve(b,d,K)-solve(b,c-1,K)-solve(a-1,d,K)+solve(a-1,c-1,K));
}
return 0;
}