『你已经见过的无聊数论』

补充一点数论求和的常用技巧。

奇怪的技巧

非常规整除分块

\(\mathbf{Question:}\) \(n\leq 10^{18}\),求\(\displaystyle \sum_{i=1}^{\left\lfloor \sqrt n \right\rfloor}\left\lfloor \dfrac{n}{i^2} \right\rfloor\)

回顾一下一般的整除分块,当\(i\in\left[x, \left\lfloor \dfrac{n}{ \lfloor n/x \rfloor } \right\rfloor \right]\)时,\(\left\lfloor \dfrac{n}{i} \right\rfloor\)的值均为\(\left\lfloor \dfrac{n}{x} \right\rfloor\),那么显然想到用\(i^2\)取代换\(i\),得到:

\(i^2\in\left[x, \left\lfloor \dfrac{n}{ \lfloor n/x \rfloor } \right\rfloor \right]\)时,\(\left\lfloor \dfrac{n}{i^2} \right\rfloor\)的值都为\(\left\lfloor \dfrac{n}{x} \right\rfloor\),显然可以解出\(i\in\left[\sqrt x,\sqrt{\left\lfloor \dfrac{n}{ \lfloor n/x \rfloor } \right\rfloor}\right]\)

于是每次得到一个左边界\(l\)作为\(\sqrt x\),然后计算出右边界就可以整除分块了。

for (ll l = 1, r; l * l <= n; l = r + 1)
	r = sqrt( n / (n/l/l) ), Sum += ( r - l + 1 ) * ( n / l / l )

数论卷积的分配律

已知\(f(n),g(n)\)是数论函数,\(h(n)\)完全积性函数,那么:\((f\cdot h)\times (g\cdot h)=(f\times g)\cdot h\)

\(\mathbf{Proof:}\)

\[\Big( (f\cdot h)\times (g\cdot h) \Big)(n)=\sum_{d|n}f(d)h(d)g\left( \frac{n}{d} \right)h\left(\frac{n}{d}\right) \\ = h(n)\sum_{d|n}f(d)g\left( \frac{n}{d} \right)=\Big( (f\times g)\cdot h \Big)(n) \]

杜教筛

\(\mathbf{Question:}\) 已知数论函数\(f(n)\),现在想快速求其前缀和\(\mathrm{S}_f(n)=\displaystyle\sum_{i=1}^n f(i)\)

解决方法是构造\(\text{Dirichlet}\)卷积,不妨将其卷上\(g(n)\),那么:

\[\mathrm{S}_{g\times f}(n)=\sum_{i=1}^n\sum_{ab=i}f(a)g(b)=\sum_{i=1}^ng(i)\sum_{j=1}^{\lfloor n/i \rfloor} f(j)=\sum_{i=1}^ng(i)\mathrm{S}_f\left( \left \lfloor \frac{n}{i} \right \rfloor \right) \\ \mathrm{S}_f(n)g(1)=\mathrm{S}_{f\times g}(n)-\sum_{i=2}^ng(i)\mathrm{S}_f\left( \left \lfloor \frac{n}{i} \right \rfloor \right) \]

如果\(\mathrm{S}_{f\times g}(n)\)可以和\(\mathrm{S}_g(n)\)可以快速求的话,那么我们就可以对上式整除分块,时间复杂度\(\mathcal O\left(n^\lambda+n^{1-\frac{\lambda}{2}}\right)\)\(n^\lambda\)为线性预处理的前缀和数量,一般来说取\(\lambda=\dfrac{2}{3}\),时间复杂度为\(\mathcal O\left(n^{\frac{2}{3}}\right )\),代码要结合\(\mathrm{Hash}\)表记忆化。

\(\mathbf{Proof:}\)

对于一个固定的\(n\),本质上杜教筛求出了所有\(\mathrm{S}_f\left(\left\lfloor\dfrac{n}{t}\right\rfloor\right)(t\in[1,n])\)的值(这一部分的值只有\(\mathcal O(\sqrt n)\)个,分别是\(1\sim \sqrt n\)\(\sqrt {\dfrac{n}{1}}\sim\sqrt {\dfrac{n}{\sqrt n}}\)),并且当\(\mathrm{S}_f\left(\left\lfloor\dfrac{n}{t}\right\rfloor\right)(t\in[2,n])\)的值已知的时候,求\(\mathrm{S}_f(n)\)的值是\(O(\sqrt n)\)的,所以我们可以得到:

\[T(n) = \mathcal O(\sqrt n)+ \sum_{t=1}^{\sqrt n}T\left(\sqrt t\right)+T\left( \sqrt{\frac{n}{t}} \right) \]

然而,我们只需将递归式向下展开一层即可计算。因为再向下展开也是\(\left\lfloor\dfrac{n}{t}\right\rfloor\)的形式,根据记忆化搜索,只会计算一次,于是总时间复杂度的表达式形如:

\[\begin{aligned} T(n) & = \mathcal O(\sqrt n)+ \sum_{t=1}^{\sqrt n}\mathcal O\left(\sqrt t\right)+\mathcal O\left( \sqrt{\frac{n}{t}} \right) \\ & \sim \mathcal O\left( \int_1^{\sqrt n}\sqrt t+ \sqrt{\frac{n}{t}}\ \text dt\right) = \mathcal O\left(n^{\frac{3}{4}} \right) \end{aligned} \]

如果线性筛处理出\(n^{\lambda}\left(\lambda\geq \dfrac{1}{2}\right)\)个值,那么时间复杂度就可以优化到:

\[T(n)=\mathcal O(n^{\lambda})+ \sum_{t=1}^{n^{1-\lambda}}\mathcal O\left(\sqrt{\frac{n}{t}}\right)\sim \mathcal O\left(\int_{1}^{n^{1-\lambda}}\sqrt{ \frac{n}{t} }\ \text d t\right)=\mathcal O\left( n^{\lambda}+ n^{1-\frac{\lambda}{2}} \right) \]

上面的复杂度分析默认\(\mathrm{S}_{f\times g}(n)\)\(\mathrm{S}_g(n)\)都可以\(\mathcal O(1)\)求,事实上没有\(\mathrm{S}_{f\times g}(n)\)没有必要,只要求的时间不超过\(\mathcal O\left(n^{\frac{2}{3}}\right)\)就不影响整体复杂度,例如可以再嵌套一个杜教筛。

例题

SPOJ LCMSUM / HDU 4944

\(\mathbf{Question1:}\) \(n\leq 10^6,T\leq 3\times 10^5\),求\(\displaystyle \sum_{i=1}^n\mathrm{lcm}(i,n)\)

\(\mathbf{Solution:}\)

\[\begin{aligned} \sum_{i=1}^n\text{lcm}(i,n) & = \sum_{i=1}^n\frac{i\times n}{\gcd(i,n)} \\ & = n\times \sum_{d|n} \frac{1}{d}\sum_{d|i}i[\gcd(i,n)=d] \\ & = n\times\sum_{d|n}\sum_{i=1}^{n/d}i\left[\gcd\left(i,\frac{n}{d}\right)=1\right] \\ & = n\times\sum_{d|n}\sum_{i=1}^{n/d}i \sum_{k|\gcd\left(i,\frac{n}{d}\right)}\mu(k) \\ & = n\times \sum_{d|n}\sum_{k|\frac{n}{d}}\mu(k)k\mathrm S_1\left(\frac{n}{dk}\right) \\ & = n\times \sum_{T|n}\mathrm S _1\left( \frac{n}{T} \right)\sum_{k|T}k\mu(k) \end{aligned} \]

这样子的贡献式子都可以调和级数地枚举处理,时间复杂度\(\mathcal O(n\log n)\)

\(\mathbf{Question2:}\) \(n,T\leq 5\times 10^5\),求\(\displaystyle \sum_{i=1}^n\sum_{j=i}^n\sum_{d|(i,j)}\frac{ij}{\gcd\left(\frac{i}{d},\frac{j}{d}\right)}\)

\(\mathbf{Solution:}\)

降智思维题。如果直接开始推的话最后会因为\(j\)的求和下界比较恶心而解决不了问题,但事实上有简单的处理方法:

\[\text{let }f(n)=\sum_{i=1}^n \sum_{k|(i,n)}\frac{i\times n}{\gcd\left(\frac{i}{k},\frac{n}{k}\right)} \\ f(n)=\sum_{k|n}\sum_{i=1}^{n/k }\frac{i\times k\times n}{\gcd\left(i,\frac{n}{k}\right)}=\sum_{k|n}k^2\sum_{i=1}^{n/k}\frac{i\times \frac{n}{k}}{\gcd\left(i,\frac{n}{k}\right)}=\sum_{k|n}k^2\sum_{i=1}^{n/k}\mathrm{lcm}\left(i,\frac{n}{k}\right) \]

显然\(f\)是上一题那个函数和\(\epsilon_2\)\(\mathrm{dirichlet}\)卷积,也可以\(\mathcal O(n\log n)\)刷表,那么原式就是\(\displaystyle \sum_{i=1}^n f(n)\),可以\(\mathcal O(1)\)回答询问。

简单的数学题

\(\mathbf{Question:}\) \(n\leq 10^{10}\),求\(\displaystyle \sum_{i=1}^n\sum_{j=1}^n ij\gcd(i,j)\)

\(\mathbf{Solution1:}\)

\(\mathrm{S}_k(n)=\displaystyle \sum_{i=1}^ni^k\)\(f(T)=T^2\varphi(T)\),考虑莫比乌斯反演

\[\begin{aligned} \sum_{i=1}^n\sum_{j=1}^n ij\gcd(i,j) & = \sum_{d=1}^nd\sum_{i=1}^{\lfloor n/d \rfloor}\sum_{j=1}^{\lfloor n/d \rfloor}ij[\gcd(i,j)=d] \\ & = \sum_{d=1}^nd^3\sum_{i=1}^{\lfloor n/d \rfloor}\sum_{j=1}^{\lfloor n/d \rfloor}ij\sum_{k|\gcd(i,j)}\mu (k) \\ & = \sum_{d=1}^n d^3\sum_{k=1}^{\lfloor n/d \rfloor}\mu(k)k^2\mathrm{S}_1^2\left( \left \lfloor \frac{n}{dk} \right \rfloor \right) \\ & = \sum_{T=1}^n\mathrm{S}_1^2\left( \left \lfloor \frac{n}{T} \right \rfloor \right) T^2 \sum_{d|T}d\mu\left( \frac{T}{d} \right) \\ & = \sum_{T=1}^n\mathrm{S}_1^2\left( \left \lfloor \frac{n}{T} \right \rfloor \right) T^2 \varphi(T) = \sum_{T=1}^n\mathrm{S}_1^2\left( \left \lfloor \frac{n}{T} \right \rfloor \right)f(T) \end{aligned} \]

对前式整除分块,考虑用杜教筛快速求\(f(T)\)的前缀和,显然:

\[f=\epsilon_2\cdot \varphi\Longleftrightarrow f \times \epsilon _2 =\epsilon_2\cdot (\varphi\times \tau)=\epsilon_3 \]

这里本质上的构造技巧其实是:\(\epsilon_k \times \epsilon_k =\tau \cdot \epsilon_k\),显然\(\displaystyle \sum_{i=1}^n\epsilon_2(i)=\dfrac{n(n+1)(2n+1)}{6},\sum_{i=1}^n\epsilon_3(i)=\dfrac{n^2(n+1)^2}{4}\)都是很好求的,所以可以杜教筛。

\(\mathbf{Solution2:}\)

直接考虑欧拉反演

\[\begin{aligned} \sum_{i=1}^n\sum_{j=1}^n ij\gcd(i,j) & = \sum_{i=1}^n\sum_{j=1}^nij\sum_{d|\gcd(i,j)} \varphi(d) \\ & = \sum_{d=1}^n \varphi(d)d^2\mathrm{S}_1^2\left( \left \lfloor \frac{n}{d} \right \rfloor \right) \end{aligned} \]

Lucas的数论

\(\mathbf{Question:}\) \(n,m\leq 10^{9}\),求\(\displaystyle \sum_{i=1}^n\sum_{j=1}^m \tau(ij)\),其中\(\tau(ij)=\sigma_0(ij)\)表示约数个数。

\(\mathbf{Solution:}\)

一个关于积性函数在两数之积处取值的有趣式子:\(\tau(nm)=\displaystyle\sum_{i|n}\sum_{j|m}[\gcd(i,j)=1]\)

\(\mathbf{Proof:}\)

不妨设\(n=\displaystyle \prod_{i=1}^k p_i^{\alpha_i},m=\prod_{i=1}^n p_i^{\beta_i},nm=\prod_{i=1}^kp_i^{\alpha_i+\beta_i}\),根据算术基本定理,\(\tau(nm)=\displaystyle \prod_{i=1}^k(\alpha_i+\beta_i+1)\)

\(i=\displaystyle \prod_{i=1}^k p_i^{u_i},m=\prod_{i=1}^n p_i^{v_i}\),观察右式,注意到

\[\sum_{i|n}\sum_{j|m}[\gcd(i,j)=1]=\sum_{i|n}\sum_{j|m}\prod_{t=1}^k [ u_t = 0 \text{ or } v_t = 0 ] \]

\(u_t=0\)时,显然\(v_t\)\(\beta_t+1\)种取值,当\(v_t=0\)时,显然\(u_t\)\(\alpha_t+1\)种取值,两者同时取零被算重,所以右式的值总共有\(\displaystyle \prod_{i=1}^k(\alpha_i+\beta_i+1)\)种,和左式的值相同。

那么可以考虑莫比乌斯反演

\[\begin{aligned} \sum_{i=1}^n\sum_{j=1}^m \tau(ij) & = \sum_{i=1}^n\sum_{j=1}^m\sum_{a|i}\sum_{b|j} \sum_{k|\gcd(a,b)} \mu(k) \\ & = \sum _{i=1}^n\sum_{j=1}^m\sum_{k|\gcd(i,j)} \mu(k)\sum_{k|a}\sum_{k|b}[a|i][a|j] \\ & = \sum _{i=1}^n\sum_{j=1}^m\sum_{k|\gcd(i,j)} \mu(k)\sum_{a=1}^{ i/k } \sum_{b=1}^{ j/k }\left[a\Big|\frac{i}{k} \right] \left[b\Big|\frac{j}{k} \right] \\ & = \sum _{i=1}^n\sum_{j=1}^m\sum_{k|\gcd(i,j)} \mu(k)\tau\left(\frac{i}{k}\right)\tau\left(\frac{j}{k}\right) \\ &= \sum_{k=1}^{\min(n,m)}\mu(k) \sum_{i=1}^{\lfloor n/k \rfloor} \tau(i)\sum_{j=1}^{\lfloor m/k \rfloor} \tau(j) \end{aligned} \]

根据\(\mu\times I=e\)可以用杜教筛求\(\mu\)的前缀和,根据\(\tau\times \mu=I^2\times \mu=I\times e=I\)可以调用\(\mu\)的杜教筛,然后再次杜教筛求\(\tau\)的的前缀和。

事实上\(\mathrm S_\tau(n)\)不用再写一个杜教筛,因为:

\[\mathrm S_\tau(n)=\sum_{i=1}^n\tau(i)=\sum_{i=1}^n\sum_{j|i}1=\sum_{j=1}^n\left\lfloor \frac{n}{j} \right\rfloor \]

所以我们可以线性筛\(\mathcal O\left(n^{\frac{2}{3}}\right)\)个前缀和,然后剩下的整除分块计算,时间复杂度也是\(\mathcal O\left(n^{\frac{2}{3}}\right)\)。(不预处理直接暴力的话也是和杜教筛的时间复杂度分析一样的,是\(\mathcal O\left(n^{\frac{3}{4}}\right)\)

最简根式

\(\mathbf{Question:}\) \(n\leq 10^{12}\),求\(\displaystyle \sum_{i=1}^n\sum_{j=1}^n \eta(\gcd(i,j))\),其中\(\eta(n)=\begin{cases} 0, & n \text{ is a square-free number}, \\ 1, & \text{otherwise} \end{cases}\)

\(\mathbf{Solution:}\)

让我们先快进到莫比乌斯反演

\[\begin{aligned} \sum_{i=1}^n\sum_{j=1}^n\eta(\gcd(i,j)) & = \sum_{i=1}^n\sum_{j=1}^n[ \mu(\gcd(i,j)) = 0 ] \\ & = \cdots\cdots \\ & =\sum_{T=1}^n\left \lfloor \frac{n}{T} \right \rfloor ^ 2\sum_{d|T}[\mu(d)=0]\mu\left( \frac{n}{d} \right) \end{aligned} \]

现在要求函数\(f(n)=\displaystyle \sum_{d|n}[\mu(d)=0]\mu\left(\frac{n}{d}\right)\)的前缀和,就可以对原式整除分块。不妨考虑\(\text{dirichlet}\)卷积,设\(\lambda(n)=[\mu(n)=0]\),那么\(f = \lambda\times \mu\Rightarrow f\times I=\lambda\times e\),显然\(I\)的前缀和是好求的,\(\lambda\times e=\lambda\)的前缀和要设法快速求解,那样我们就可以杜教筛。

一个重要的发现是:

\[\mathrm{S}_\lambda(n)=\sum_{i=1}^n[\mu(i)=0]=n-\sum_{i=1}^n\mu^2(i) \]

这时候要定义\(\chi(n)=\max\limits_{d^2|n} d\),显然\(\mu^2(i)=[\chi(n)=1]=e(\chi(n))\),那么对其再次莫比乌斯反演:

\[\mathrm{S}_\lambda(n)=n-\sum_{i=1}^ne(\chi(i))=n-\sum_{i=1}^n\sum_{t|\chi(i)}\mu(t) \\ = n - \sum_{i=1}^n\sum_{t^2|i}\mu(t) = n - \sum_{t=1}^{\lfloor \sqrt{n} \rfloor}\mu(t)\left\lfloor \frac{n}{t^2} \right\rfloor \]

这个东西直接求时间复杂度是\(\mathcal O(\sqrt n)\),不影响杜教筛复杂度,显然也可以优化到\(O\left(n^{\frac{1}{3}}\right)\)

然而这个东西只能做到\(\mathcal{O}\left(Tn^{\frac{2}{3}}\right)\),并不是正解。我愿称之为莫比乌斯反演的白给。

考虑直接容斥,\(\gcd\)没有平方因子的数对是就是:\(\gcd\)至少有一个平方因子的数对数 \(-\) \(\gcd\)至少有两个平方因子的数对数 \(+\ \cdots\)

写成式子就是:

\[-\sum_{i=2}^{\lfloor \sqrt n \rfloor}\mu(i) \left\lfloor \frac{n}{i^2} \right\rfloor ^ 2 \]

直接算的话\(\mathcal O\left(\sqrt n + Tn^{\frac{1}{3}} \right)\),结合杜教筛优化到\(\mathcal O\left(T\left(n^{\frac{1}{2}\times \frac{2}{3}}+n^{\frac{1}{3}}\right)\right)=\mathcal O \left(Tn^{\frac{1}{3}}\right)\).

CF809E Surprise me!

\(\mathbf{Question:}\) 给定一棵树,点有点权\(a_i\)\(\{a_i\}\)是一个\(1\sim n\)的排列,随机选点\(u,v(u\not = v)\),求\(\mathbb{E}\Big( \varphi(a_ua_v)\mathrm{dis}(u,v) \Big)\)\(n\leq 2\times 10^5\)

\(\mathbf{Solution:}\)

考虑积性函数\(\varphi\)在两数之积处的取值,可知:\(\varphi(ab)=\dfrac{\varphi(a)\varphi(b)\gcd(a,b)}{\varphi(\gcd(a,b))}\)

\(b_{a_i}=i\)为排列\(\{a_i\}\)的逆置换,然后开始莫比乌斯反演:

\[\begin{aligned} \mathbb{E}\Big( \varphi(a_ua_v)\mathrm{dis}(u,v) \Big) & = \frac{1}{n\times (n+1)}\sum_{i=1}^n\sum_{j=1}^n\varphi(a_ia_j)\mathrm{dis}(i,j) \\ \sum_{i=1}^n\sum_{j=1}^n\varphi(a_ia_j)\mathrm{dis}(i,j) & = \sum_{i=1}^n\sum_{j=1}^n\frac{\varphi(a_i)\varphi(a_j)\gcd(a_i,a_j)}{\varphi(\gcd(a_i,a_j))}\times \mathrm{dis}(i,j) \\ & = \cdots\cdots \\ & = \sum_{T=1}^n\left( \sum_{d|T}\frac{d}{\varphi(d)}\mu\left( \frac{T}{d} \right) \right)\sum_{i=1}^{\lfloor n/T \rfloor}\sum_{j=1}^{\lfloor n/T \rfloor} \varphi(iT)\varphi(jT)\mathrm{dis}(b_{iT},b_{jT}) \end{aligned} \]

\(\zeta=(\epsilon\cdot \varphi ^{-1} )\times \mu\),显然可以\(\mathcal O(n\log n)\)预处理。显然可以枚举每一个\(T\),把点集\(S(T)=\{b_{kT}|k\in \N^+\}\)拿出来染色,那么问题转化成了:

\(n\)种颜色,每种颜色有权值\(v_i\),现在有\(\mathcal O(n\log n)\)次染色,如果用\(\mathcal C(i)\)表示点\(i\)被染过的颜色的集合,求\(\displaystyle \sum_{c=1}^n\sum_{i=1}^n\sum_{j=1}^n\mathrm{dis}(i,j)\varphi(a_i)\varphi(a_j)v_c[c\in \mathcal C(i)][c\in \mathcal C(j)]\)

显然可以用桶来维护同色贡献,然后就大力拆贡献点分吧(比虚树dp好写好想多了)。

\(\mathbf{continuously\ updating...}\)

posted @ 2020-11-22 19:38  Parsnip  阅读(439)  评论(0编辑  收藏  举报