Luogu P2522 [HAOI2011]Problem b

P2522 [HAOI2011]Problem b

题意

给出 NN 组数据,每组数据有 a,b,c,d,ka, b, c, d, k,求解:

x=aby=cd[gcd(x,y)=k]\sum_{x=a}^b\sum_{y=c}^d[\text{gcd}(x,y)=k]

1N,k5×1041\leqslant N, k\leqslant 5\times 10^4
1ab5×1041\leqslant a\leqslant b\leqslant 5\times 10^4
1cd5×1041\leqslant c\leqslant d\leqslant 5\times 10^4

思路

对原式进行变换,变换技巧见Mobius反演

x=aby=cd[gcd(x,y)=k]=x=akbky=ckdk[gcd(x,y)=1]=a=ak,b=bk,c=ck,d=dkx=aby=cd[gcd(x,y)=1]=x=aby=cdε(gcd(x,y)=1)=x=aby=cd(μ1)(gcd(x,y)=1)=x=aby=cdzgcd(x,y)μ(z)=z=1min(b,d)x=azbzy=czdzμ(z)=z=1min(b,d)(bzaz+1)(dzcz+1)μ(z)=cz=c+z1z=c1z+1az=a+z1z=a1z+1z=1min(b,d)(bza1z)(dzc1z)μ(z)\begin{aligned} \sum_{x=a}^b\sum_{y=c}^d[\text{gcd}(x,y)=k] &=\sum_{x=\left\lceil\frac{a}{k}\right\rceil}^{\left\lfloor\frac{b}{k}\right\rfloor}\sum_{y=\left\lceil\frac{c}{k}\right\rceil}^{\left\lfloor\frac{d}{k}\right\rfloor}[\text{gcd}(x, y)=1]\\ &\xlongequal{a=\left\lceil\frac{a}{k}\right\rceil, b=\left\lfloor\frac{b}{k}\right\rfloor, c=\left\lceil\frac{c}{k}\right\rceil, d=\left\lfloor\frac{d}{k}\right\rfloor}\sum_{x=a}^b\sum_{y=c}^d[\text{gcd}(x,y)=1]\\ &=\sum_{x=a}^b\sum_{y=c}^d\varepsilon(\text{gcd}(x, y)=1)\\ &=\sum_{x=a}^b\sum_{y=c}^d(\mu\ast \texttt{1})(\text{gcd}(x, y)=1)\\ &=\sum_{x=a}^b\sum_{y=c}^d\sum_{z|\text{gcd}(x, y)}\mu(z)\\ &=\sum_{z=1}^{\min(b, d)}\sum_{x=\left\lceil\frac{a}{z}\right\rceil}^{\left\lfloor\frac{b}{z}\right\rfloor}\sum_{y=\left\lceil\frac{c}{z}\right\rceil}^{\left\lfloor\frac{d}{z}\right\rfloor}\mu(z)\\ &=\sum_{z=1}^{\min(b, d)}(\left\lfloor\frac{b}{z}\right\rfloor-\left\lceil\frac{a}{z}\right\rceil+1)(\left\lfloor\frac{d}{z}\right\rfloor-\left\lceil\frac{c}{z}\right\rceil+1)\mu(z)\\ &\xlongequal[\left\lceil\frac{c}{z}\right\rceil=\left\lfloor\frac{c+z-1}{z}\right\rfloor=\left\lfloor\frac{c-1}{z}\right\rfloor+1]{\left\lceil\frac{a}{z}\right\rceil=\left\lfloor\frac{a+z-1}{z}\right\rfloor=\left\lfloor\frac{a-1}{z}\right\rfloor+1} \sum_{z=1}^{\min(b,d)}(\left\lfloor\frac{b}{z}\right\rfloor-\left\lfloor\frac{a-1}{z}\right\rfloor)(\left\lfloor\frac{d}{z}\right\rfloor-\left\lfloor\frac{c-1}{z}\right\rfloor)\mu(z) \end{aligned}

其中使用的数论函数 ε,μ,1\varepsilon, \mu, \texttt{1}积性函数-例子

于是只需要用欧拉筛,线性预处理出 μ\mu 函数的前缀和,然后用数论分块就能解决了

注: 数论分块这里是取四个的最小值。

总复杂度 O(Nmin(b,d))O(N\cdot\sqrt{\min(b, d)})


Luogu P2522 [HAOI2011]Problem b
https://wty-yy.github.io/posts/64029/
作者
wty
发布于
2021年8月17日
许可协议