题意
(Tle 5 imes 10^4) 次询问,每次询问 (a,b,c,d,kle 5 imes 10^4),求
[sum _{i=a}^bsum _{j=c}^d[gcd(i,j)=k]
]
分析
重新学了一次(可能跟第一次学没什么区别)莫比乌斯反演相关,这题还是很简单的。问题可以转化为求四次
[sum _{i=1}^nsum _{j=1}^m[gcd(i,j)=1]
]
下面过程中设 (nle m) 。
[egin{aligned}
sum _{i=1}^nsum _{j=1}^m[gcd(i,j)=k]&=sum _{i=1}^nsum _{j=1}^msum _{k|i,k|j}sum _{d|frac{i}{k},d|frac{j}{k}}mu(d) \
&=sum _{d=1}^nsum _{i=1}^{lfloorfrac{n}{k}
floor}sum _{j=1}^{lfloorfrac{m}{k}
floor}mu (d) \
&=sum _{d=1}^nmu (d) lfloorfrac{lfloorfrac{n}{k}
floor}{d}
floor lfloorfrac{lfloorfrac{m}{k}
floor}{d}
floor
end{aligned}
]
注意到 (lfloorfrac{n}{d} floor) 这种形式只有 (2sqrt n) 种取值(对于 (dle sqrt n) ,显然;对于 (d>sqrt n) ,(lfloorfrac{n}{d} floorle sqrt n) ,也最多只有 (sqrt n) 种),所以 (lfloorfrac{lfloorfrac{n}{k} floor}{d} floor lfloorfrac{lfloorfrac{m}{k} floor}{d} floor) 最多只有 (4sqrt n) 种取值,跳一下即可做到单次询问 (O(sqrt n)) ,只要预处理 (mu) 函数的前缀和即可。
代码
#include<bits/stdc++.h>
using namespace std;
inline int read() {
int x=0,f=1;
char c=getchar_unlocked();
for (;!isdigit(c);c=getchar_unlocked()) if (c=='-') f=-1;
for (;isdigit(c);c=getchar_unlocked()) x=x*10+c-'0';
return x*f;
}
typedef long long giant;
const int maxn=5e4+1;
bool np[maxn];
int p[maxn],mu[maxn],ps=0;
giant calc(int n,int m) {
giant ret=0;
if (n>m) swap(n,m);
for (int i=1,j;i<=n;i=j+1) {
j=min(n/(n/i),m/(m/i));
ret+=(giant)(mu[j]-mu[i-1])*(n/i)*(m/i);
}
return ret;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
mu[1]=1;
for (int i=2;i<maxn;++i) {
if (!np[i]) p[++ps]=i,mu[i]=-1;
for (int j=1;j<=ps && i*p[j]<maxn;++j) {
np[i*p[j]]=true;
if (i%p[j]==0) break;
mu[i*p[j]]=-mu[i];
}
}
for (int i=2;i<maxn;++i) mu[i]+=mu[i-1];
int T=read();
while (T--) {
int a=read(),b=read(),c=read(),d=read(),k=read();
a=(a-1)/k,b/=k,c=(c-1)/k,d/=k;
giant ans=(calc(b,d)-calc(b,c))-(calc(a,d)-calc(a,c));
printf("%lld
",ans);
}
return 0;
}