Description:
给定(a),(b),(c),(d),(k)
求:
(sum_{i=a}^{b} sum_{j=c}^{d} gcd(i,j)==k)
(T)组询问
Hint:
均(<=5e4)
Solution:
不就比(zap-query)多了个下界嘛,容斥即可
#include<bits/stdc++.h>
using namespace std;
const int mxn=1e5+5;
int T,tot,mu[mxn],vis[mxn],p[mxn],f[mxn];
int sum[mxn];
void sieve(int lim)
{
mu[1]=1;
for(int i=2;i<=lim;++i) {
if(!vis[i]) mu[i]=-1,p[++tot]=i;
for(int j=1;j<=tot;++j) {
if(p[j]*i>lim) break;
vis[p[j]*i]=1;
if(i%p[j]==0) {
mu[i*p[j]]=0;
break;
}
mu[p[j]*i]=-mu[i];
}
}
for(int i=1;i<=lim;++i) sum[i]=sum[i-1]+mu[i];
}
int solve(int n,int m,int k)
{
if(n>m) swap(n,m); int ans=0;
for(int l=1,r;l<=n;l=r+1) {
r=min(n/(n/l),m/(m/l));
ans+=1ll*(sum[r]-sum[l-1])*(n/l)*(m/l);
}
return ans;
}
int main()
{
scanf("%d",&T);
sieve(50000);
while(T--) {
int n,m,x,y,k;
scanf("%d%d%d%d%d",&n,&m,&x,&y,&k); --n,--x;
n/=k,m/=k,x/=k,y/=k;
printf("%d
",solve(m,y,k)-solve(n,y,k)-solve(m,x,k)+solve(n,x,k));
}
return 0;
}