H y p e r l i n k Hyperlink Hyperlink
https://www.luogu.org/problem/P2522
https://www.lydsy.com/JudgeOnline/problem.php?id=2301
D e s c r i p t i o n Description Description
这道题:P3455 [POI2007]ZAP-Queries的多范围版本。。。
S o l u t i o n Solution Solution
容斥一下就 o k ok ok了。。。
莫比乌斯反演
C o d e Code Code
#include<cstdio>
#include<cctype>
#include<algorithm>
#define N 50000
#define LL long long
using namespace std;
LL mu[N+10],f[N+10];int prime[N>>3],m,t,a,b,c,d,k;
bool vis[N+10];
inline LL read()
{
LL f=0,d=1;char c;
while(c=getchar(),!isdigit(c)) if(c=='-') d=-1;f=(f<<3)+(f<<1)+c-48;
while(c=getchar(),isdigit(c)) f=(f<<3)+(f<<1)+c-48;
return d*f;
}
inline LL solve(int a,int b,int k)
{
if(a>b) a^=b,b=a^b,a^=b;
LL res=0;
for(register int l=1,r=0;l<=a;l=r+1)
{
r=min(a/(a/l),b/(b/l));
res+=(LL)(mu[r]-mu[l-1])*(LL)(1ll*a/(l*k)*(b/(l*k)));
}
return res;
}
signed main()
{
mu[1]=1;
for(register int i=2;i<=N;i++)
{
if(vis[i]==0) {prime[++m]=i;mu[i]=-1;}
for(register int j=1;j<=m&&prime[j]*i<=N;j++)
{
vis[prime[j]*i]=true;
if(i%prime[j]==0) break;
mu[prime[j]*i]=-mu[i];
}
}
for(register int i=1;i<=N;i++) mu[i]+=mu[i-1];
for(t=read();t--;)
{
a=read();b=read();c=read();d=read();k=read();
printf("%lld\n",solve(b,d,k)-solve(b,c-1,k)-solve(a-1,d,k)+solve(a-1,c-1,k));
}
}