Luogu P1829 [国家集训队]Crash的数字表格 / JZPTAB

P1829 [国家集训队]Crash的数字表格 / JZPTAB

题意

给出 n,mn, m 求解:

i=1nj=1mlcm(i,j)\sum_{i=1}^n\sum_{j=1}^m\text{lcm}(i, j)

1n,m1071\leqslant n, m\leqslant 10^7

思路

对原式进行数论变换:

i=1nj=1mlcm(i,j)=i=1nj=1mijgcd(i,j)=提取公因式d=1min(n,m)i=1nj=1mijd[gcd(i,j)=d]=i=i/d,j=j/dd=1min(n,m)i=1ndj=1mdidjdd[gcd(id,jd)=d]=d=1min(n,m)i=1ndj=1mdijd[gcd(i,j)=1]=d=1min(n,m)di=1ndj=1mdijε(gcd(i,j))=d=1min(n,m)di=1ndj=1mdij(μ1)(gcd(i,j))=d=1min(n,m)di=1ndj=1mdijtgcd(i,j)μ(t)=d=1min(n,m)dt=1min(nd,md)i=1nd[ti]j=1md[tj]ijμ(t)=i=i/t,j=j/td=1min(n,m)dt=1min(nd,md)i=1ndtj=1mdtitjtμ(t)=d=1min(n,m)dt=1min(nd,md)t2μ(t)i=1ndtij=1mdtj=d=1min(n,m)dt=1min(nd,md)t2μ(t)(1+ndt)(ndt)2(1+mdt)(mdt)2\begin{aligned} \sum_{i=1}^n\sum_{j=1}^m\text{lcm}(i, j)&=\sum_{i=1}^n\sum_{j=1}^m\frac{i\cdot j}{\text{gcd}(i,j)}\\ &\xlongequal{\text{提取公因式}}\sum_{d=1}^{\min(n,m)}\sum_{i=1}^n\sum_{j=1}^m\frac{i\cdot j}{d}\cdot [\text{gcd}(i,j)=d]\\ &\xlongequal{i=i/d,j=j/d}\sum_{d=1}^{\min(n,m)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}\frac{id\cdot jd}{d}\cdot [\text{gcd}(id,jd)=d]\\ &=\sum_{d=1}^{\min(n,m)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}i\cdot j\cdot d\cdot [\text{gcd}(i,j)=1]\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}i\cdot j\cdot \varepsilon(\text{gcd}(i,j))\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}i\cdot j\cdot (\mu\ast\texttt{1})(\text{gcd}(i,j))\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}i\cdot j\cdot \sum_{t|\text{gcd}(i,j)}\mu(t)\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{t=1}^{\min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor}[t|i]\sum_{j=1}^{\left\lfloor\frac{m}{d}\right\rfloor}[t|j]\cdot i\cdot j\cdot\mu(t)\\ &\xlongequal{i=i/t,j=j/t}\sum_{d=1}^{\min(n,m)}d\sum_{t=1}^{\min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}\sum_{i=1}^{\left\lfloor\frac{n}{dt}\right\rfloor}\sum_{j=1}^{\left\lfloor\frac{m}{dt}\right\rfloor} it\cdot jt\cdot\mu(t)\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{t=1}^{\min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}t^2\mu(t)\sum_{i=1}^{\left\lfloor\frac{n}{dt}\right\rfloor}i\sum_{j=1}^{\left\lfloor\frac{m}{dt}\right\rfloor}j\\ &=\sum_{d=1}^{\min(n,m)}d\sum_{t=1}^{\min(\left\lfloor\frac{n}{d}\right\rfloor,\left\lfloor\frac{m}{d}\right\rfloor)}t^2\mu(t)\cdot\frac{(1+\left\lfloor\frac{n}{dt}\right\rfloor)(\left\lfloor\frac{n}{dt}\right\rfloor)}{2}\cdot\frac{(1+\left\lfloor\frac{m}{dt}\right\rfloor)(\left\lfloor\frac{m}{dt}\right\rfloor)}{2}\\ \end{aligned}

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

最终的式子可以使用二维 数论分块 解决,用线性筛筛出 μ\mu 函数,顺便维护 t2μ(t)t^2\mu(t) 的前缀和。

总复杂度 O(NN)=O(N)O(\sqrt N\cdot\sqrt N)=O(N)


Luogu P1829 [国家集训队]Crash的数字表格 / JZPTAB
https://wty-yy.github.io/posts/2613/
作者
wty
发布于
2021年8月17日
许可协议