莫比乌斯反演总结

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

\[\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\operatorname{lcm}(i, j)\\ = \sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sum\limits_{k |i, j}[\gcd(i, j) = k] \times \frac{ij}{k}\\ = \sum\limits_{k}^{\min(n, m)}k\times\sum\limits_{i = 1}^{\frac{n}{k}}i\sum\limits_{j = 1}^{\frac{m}{k}}j\times[gcd(i, j) = 1]\\ = \sum\limits_{k}^{\min(n, m)}k\times\sum\limits_{i = 1}^{\frac{n}{k}}i\sum\limits_{j = 1}^{\frac{m}{k}}j\sum\limits_{e | i, j}\mu(e)\\ = \sum\limits_{k}^{\min(n, m)}k\times\sum\limits_{e}^{\min(\frac{n}{k}, \frac{m}{k})}\mu(e)\times\sum\limits_{i = 1}^{\frac{n}{ke}}\sum\limits_{j = 1}^{\frac{m}{ke}}ij\\ = \sum\limits_{k}^{\min(n, m)}k\times\sum\limits_{e}^{\min(\frac{n}{k}, \frac{m}{k})}\mu(e)\frac{(1 + \lfloor\frac{n}{ke}\rfloor) \times \lfloor\frac{n}{ke}\rfloor}{2}\frac{(1 + \lfloor\frac{m}{ke}\rfloor) \times \lfloor\frac{m}{ke}\rfloor}{2}\\ \]

数论分块套数论分块即可

约数个数和

\[\sum\limits_{i=1}^n\sum\limits_{j=1}^md(ij)\\ = \sum\limits_{i=1}^n\sum\limits_{j=1}^m\sum\limits_{x|i}\sum\limits_{y|j}[\gcd(x, y) = 1]\\ = \sum\limits_{x}^{n}\sum\limits_{y}^{m}[\gcd(x, y) = 1]\sum\limits_{i}^{\frac{n}{x}}\sum\limits_{j}^{\frac{m}{y}}1\\ = \sum\limits_{x}^{n}\sum\limits_{y}^{m}[\gcd(x, y) = 1]\lfloor\frac{n}{x}\rfloor\lfloor\frac{n}{y}\rfloor\\ = \sum\limits_{x}^{n}\sum\limits_{y}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{n}{y}\rfloor[\gcd(x, y) = 1]\\ = \sum\limits_{x}^{n}\sum\limits_{y}^{m}\lfloor\frac{n}{x}\rfloor\lfloor\frac{m}{y}\rfloor\sum_{i|x, y}\mu(i)\\ = \sum_{i}^{\min(n, m)}\mu(i)\sum\limits_{x}^{\frac{n}{i}}\sum\limits_{y}^{\frac{m}{i}}\lfloor\frac{n}{xi}\rfloor\lfloor\frac{m}{yi}\rfloor\\ = \sum_{i}^{\min(n, m)}\mu(i)\sum\limits_{x}^{\frac{n}{i}}\lfloor\frac{n}{xi}\rfloor\sum\limits_{y}^{\frac{m}{i}}\lfloor\frac{m}{yi}\rfloor\\ = \sum_{i}^{\min(n, m)}\mu(i)(\sum\limits_{x}^{\frac{n}{i}}\lfloor\frac{n}{xi}\rfloor)(\sum\limits_{y}^{\frac{m}{i}}\lfloor\frac{m}{yi}\rfloor)\\ \]

预处理 \(\sum\limits_{j = 1}^{i}\lfloor\frac{i}{j}\rfloor\) 再套数论分块即可

ZAP-Queries

\[\sum\limits_{i = 1}^{a}\sum\limits_{j = 1}^{b}[\gcd(i, j) = d]\\ = \sum\limits_{i = 1}^{\frac{a}{d}}\sum\limits_{j = 1}^{\frac{b}{d}}[\gcd(i, j) = 1]\\ = \sum\limits_{i = 1}^{\frac{a}{d}}\sum\limits_{j = 1}^{\frac{b}{d}}\sum\limits_{e | i, j}\mu(e)\\ = \sum\limits_{e = 1}^{\min(\frac{a}{d}, \frac{b}{d})}\mu(e)\sum\limits_{i = 1}^{\frac{a}{ed}}\sum\limits_{j = 1}^{\frac{b}{ed}}1\\ = \sum\limits_{e = 1}^{\min(\frac{a}{d}, \frac{b}{d})}\mu(e)\lfloor\frac{a}{ed}\rfloor\lfloor\frac{b}{ed}\rfloor\\ \]

直接数论分块即可

能量采集

原问题即求

\[(2 \times\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\gcd(i, j)) - nm \]

其中

\[\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\gcd(i, j)\\ = \sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sum\limits_{k = 1}^{\min(n, m)}k[\gcd(i, j) = k]\\ = \sum\limits_{k = 1}^{\min(n, m)}k \times\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}[\gcd(i, j) = k]\\ = \sum\limits_{k = 1}^{\min(n, m)}k \times\sum\limits_{i = 1}^{\frac{n}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}[\gcd(i, j) = 1]\\ = \sum\limits_{k = 1}^{\min(n, m)}k \times\sum\limits_{i = 1}^{\frac{n}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}\sum\limits_{e | i, j} \mu(e)\\ = \sum\limits_{k = 1}^{\min(n, m)}k\times\sum\limits_{e = 1}^{\frac{\min(n, m)}{k}}\mu(e)\sum\limits_{i = 1}^{\frac{n}{ke}}\sum\limits_{j = 1}^{\frac{m}{ke}}1\\ = \sum\limits_{k = 1}^{\min(n, m)}k\times\sum\limits_{e = 1}^{\frac{\min(n, m)}{k}}\mu(e)\lfloor\frac{n}{ke}\rfloor\lfloor\frac{m}{ke}\rfloor\\ \]

数论分块套数论分块即可

LCMs

\(\operatorname{number}(i) = \sum_{j = 0}^{n - 1} [A_j = i]\)\(m = \max_{j = 0}^{n - 1}(A_j)\)

\[\sum\limits_{i=0}^{n-2}\ \sum\limits_{j=i+1}^{n-1}\ \mathrm{lcm}(A_i,A_j)\\ = \frac{(\sum\limits_{i = 1}^{m}\sum\limits_{j = 1}^{m}\mathrm{lcm}(i, j)\times\mathrm{number}(i)\times\mathrm{number}(j)) - \sum\limits_{i = 0}^{n - 1}A_i}{2} \]

其中

\[\sum\limits_{i = 1}^{m}\sum\limits_{j = 1}^{m}\mathrm{lcm}(i, j)\times\mathrm{number}(i)\times\mathrm{number}(j)\\ =\sum\limits_{i = 1}^{m}\sum\limits_{j = 1}^{m}\mathrm{number}(i)\times\mathrm{number}(j)\times\frac{ij}{\gcd(i, j)}\\ =\sum\limits_{i = 1}^{m}\sum\limits_{j = 1}^{m}\mathrm{number}(i)\times\mathrm{number}(j)\times\sum\limits_{k}^{m}\frac{ij}{k}[\gcd(i, j) = k]\\ =\sum\limits_{k}^{m}\sum\limits_{i = 1}^{m}\sum\limits_{j = 1}^{m}\mathrm{number}(i)\times\mathrm{number}(j)\times\frac{ij}{k}[\gcd(i, j) = k]\\ =\sum\limits_{k}^{m}k\times\sum\limits_{i = 1}^{\frac{m}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}\mathrm{number}(ik)\times\mathrm{number}(jk)\times ij[\gcd(i, j) = 1]\\ =\sum\limits_{k}^{m}k\times\sum\limits_{i = 1}^{\frac{m}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}\mathrm{number}(ik)\times\mathrm{number}(jk)\times ij\sum\limits_{e | i, j}\mu(e)\\ =\sum\limits_{k}^{m}k\times\sum\limits_{e}^{\frac{m}{k}}\mu(e) \times e^{2}\sum\limits_{i = 1}^{\frac{m}{ke}}\sum\limits_{j = 1}^{\frac{m}{ke}}\mathrm{number}(ike)\times\mathrm{number}(jke)\times ij\\ =\sum\limits_{e}^{m}\mu(e) \times e^{2}\sum\limits_{k}^{\frac{m}{e}}k\times\sum\limits_{i = 1}^{\frac{m}{ke}}\sum\limits_{j = 1}^{\frac{m}{ke}}\mathrm{number}(ike)\times\mathrm{number}(jke)\times ij\\ =\sum\limits_{e}^{m}\mu(e) \times e^{2}\sum\limits_{k}^{\frac{m}{e}}k\times\sum\limits_{i = 1}^{\frac{m}{ke}}\mathrm{number}(ike)\times i\sum\limits_{j = 1}^{\frac{m}{ke}}\mathrm{number}(jke)\times j\\ =\sum\limits_{e}^{m}\mu(e) \times e^{2}\sum\limits_{k}^{\frac{m}{e}}k\times(\sum\limits_{i = 1}^{\frac{m}{ke}}\mathrm{number}(ike)\times i)^{2}\\ \]

对于每个 \(ke\) 记录 \(\sum\limits_{i = 1}^{\frac{m}{ke}}\mathrm{number}(ike)\times i\) , 枚举 \(e\)\(k\), 看似会超时实则调和级数加主定理

数表

考虑没有限制, 则原题为

\[\sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sum\limits_{k | i, j} k\\ = \sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sum\limits_{k | \gcd(i, j)} k\\ = \sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sigma(\gcd(i, j))\\ = \sum\limits_{i = 1}^{n}\sum\limits_{j = 1}^{m}\sum\limits_{k = 1}^{\min(n, m)}\sigma(k)[\gcd(i, j) = k]\\ = \sum\limits_{k = 1}^{\min(n, m)} \sum\limits_{i = 1}^{\frac{n}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}\sigma(k)[\gcd(i, j) = 1]\\ = \sum\limits_{k = 1}^{\min(n, m)}\sigma(k) \sum\limits_{i = 1}^{\frac{n}{k}}\sum\limits_{j = 1}^{\frac{m}{k}}\sum\limits_{e | i, j}\mu(e)\\ = \sum\limits_{k = 1}^{\min(n, m)}\sigma(k)\sum\limits_{e = 1}^{\min(\frac{n}{k}, \frac{m}{k})}\mu(e) \sum\limits_{i = 1}^{\frac{n}{ke}}\sum\limits_{j = 1}^{\frac{m}{ke}}1\\ \sum\limits_{k = 1}^{\min(n, m)}\sigma(k)\sum\limits_{e = 1}^{\min(\frac{n}{k}, \frac{m}{k})}\mu(e) \lfloor\frac{n}{ke}\rfloor\lfloor\frac{m}{ke}\rfloor\\ \]

但这个样子对于不同的 \(n\)\(m\) 至少要用 \(\mathrm{O}(n\sqrt{n})\) 的时间求解, 不满足多组询问的要求, 所以考虑变换, 将常数项往后提

\[\therefore \sum\limits_{k = 1}^{\min(n, m)}\sigma(k)\sum\limits_{e = 1}^{\min(\frac{n}{k}, \frac{m}{k})}\mu(e) \lfloor\frac{n}{ke}\rfloor\lfloor\frac{m}{ke}\rfloor\\ = \sum\limits_{i}^{min (n, m)}\lfloor\frac{n}{i}\rfloor\lfloor\frac{m}{i}\rfloor \sum\limits_{k | i}\sigma(k)\mu(\frac{i}{k})\\ \]

这里可以考虑对不同的 \(i\) 维护 \(\sum\limits_{k | i}\sigma(k)\mu(\frac{i}{k})\) , 并对 \(i\) 求前缀和, 这样对于一次操作我们就可以用 \(\mathrm{O}(\sqrt{n})\) 的时间复杂度去求解。 再考虑题目限制, 即要求 \(\sigma(k)\) 不能超过 \(a\) , 那么我们考虑对所有会参与计算的 \(\sigma(k)\mu(\frac{i}{k})\) 排序, 因为调和级数, 所以这里时间复杂度是 \(O(n\log^{2}{n})\) 的, 然后再对询问的 \(a\) 排序, 那么每次只会增加一些 \(\sigma(k)\mu(\frac{i}{k})\) 可以产生贡献, 我们把刚才的前缀和改为 BIT , 每次就在上面修改和查询即可, 时间复杂度 \(O(q\sqrt{n}\log{n} + n\log^{2}{n})\)

posted @ 2023-09-28 21:11  小篪篪  阅读(6)  评论(0编辑  收藏  举报
Live2D