跳到正文

算法·理论

浅谈PN筛

tmlrock约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年1月17日
更新
2026年7月10日
篇幅
约 6 分钟
获赞
1

版本

  1. v2当前
  2. v1

0x00 本文约定

  1. P\mathbb{P} 表示素数集,变量 p∈Pp\in \mathbb{P} 。
  2. x=Πi=1p(x)picix=\Pi_{i=1}^{p(x)} p_i^{c_i} 表示 xx 完全质因数分解的结果,其中 p(x)p(x) 表示x质因子个数。
  3. 本文不区分素数、质数。
  4. a∣ba|b 表示 aa 是 bb 的因子。
  5. [x][x] 表示艾弗森括号,即命题 xx 为真时值为 11 , 反之为 00 。
  6. f∗gf*g 表示 迪利克雷(Dirichlet)卷积。
  7. ω(x)\omega(x) 表示 xx 的不同质因数个数

0X01 PN筛简介

PN (Powerful-Number) 筛,是解决一些积性函数 ff 的求和在 nln⁡n\sqrt{n}\ln n 左右的复杂度。
特征:

  • 容易构造积性函数 gg 使得对于 p∈Pp\in\mathbb{P} 都有 f(p)=g(p)f(p)=g(p) 。
  • G(n)G(n) 容易求出
  • nn 大约为 10910^9 到 101410^{14} 级别

0x02 PN

PN (Powerful-Number),强数是指对于所有 cic_i 都不小于1

xx 可以表示为 a2b3,a,b∈N+a^2b^3 , a,b\in \mathbb{N}^+ 是 x is PNx\ is\ \text{PN} 的充要条件。

证明

当 2∣ci2 | c_i 时,a×pici2→aa\times p_i^{\frac{c_i}{2}}\to a 。
否则,b×pi3→bb\times p_i^3\to b , a×pipi−32a\times p_i^{\frac{p_i-3}{2}} 。
因为 ci>1c_i>1 , 所以此时 ci≥3c_i \ge 3 。

□\square

小于 nn 的 PN 有 O(n)O\left(\sqrt{n}\right) 个。

证明
O(∫0nna23da)=O(n13∫0na−23da)=O(n13n13)=O(n)O\left(\int_0^{\sqrt{n}} \sqrt[3]{\dfrac{n}{a^2}}da\right)=O\left(n^{\frac{1}{3}}\int_0^{\sqrt{n}}a^{-\frac{2}{3}}da\right)=O\left(n^{\frac{1}{3}}\sqrt{n}^{\frac{1}{3}}\right)=O\left(\sqrt{n}\right)□\square

所以,我们可以暴力一下 aa 即可预处理复杂度: O(n)O(\sqrt{n}) 。 同理,nn 是 PNPN 的概率为 O(n−12)O(n^{-\frac{1}{2}})

0x03 化简原式

令 g∗h=fg*h=f ,其中 ∀p∈P,f(p)=g(p)\forall p\in\mathbb{P},f(p)=g(p) 。

∀p∈P,h(p)=0\forall p\in \mathbb{P} , h(p)=0

证明

由 Dirichlet 卷积定义得 ,f(p)=g(p)h(1)+h(p)g(1)f(p)=g(p)h(1)+h(p)g(1) .
其中 gg 、 pp 为积性函数,所以g(1)=h(1)=1g(1)=h(1)=1 。
联立上式得 h(p)=f(p)−g(p)=0h(p)=f(p)-g(p)=0 。

□\square

∀x isn′t PN , h(x)=0\forall x\ isn't\ \text{PN}\ ,\ h(x)=0

证明

因为 xx 不是 PN,所以必定有一个 pip_i 使得 ci=1c_i=1 。
因为 hh 为积性函数, h(x)=h(npi)h(pi)=0h(x)=h(\dfrac{n}{p_i})h(p_i)=0。

□\square
∑i=1nf(i)=∑i=1n∑d=1n[d∣i]h(d)g(id)=∑d=1nh(d)∑i=1n[d∣i]g(id)不妨令 i′=id 。∑d=1nh(d)∑i′=1⌊nd⌋g(i′)=∑d=1nh(d)G(⌊nd⌋)=∑d=1 d is PNnh(d)G(⌊nd⌋)\begin{aligned} \sum_{i=1}^n f(i)\\ =\sum_{i=1}^n\sum_{d=1}^n[d|i]h(d)g(\dfrac{i}{d})\\ =\sum_{d=1}^nh(d)\sum_{i=1}^n[d|i]g(\dfrac{i}{d})\\ \text{不妨令 $i'=\dfrac{i}{d}$ 。}\\ \sum_{d=1}^nh(d)\sum_{i'=1}^{\lfloor\dfrac{n}{d}\rfloor}g(i')\\ =\sum_{d=1}^nh(d)G(\lfloor\dfrac{n}{d}\rfloor)\\ =\sum_{d=1\ d\ is\ \text{PN}}^n h(d)G(\lfloor\dfrac{n}{d}\rfloor) \end{aligned}

任务变为如何求PN的 hh 值。

0x04 h(x)h(x) , 其中 xx 是PN

由于 hh 是积性函数,即求h(pc)h(p^c) 。 如法炮制:

f(pc)=∑i=0cg(pi)h(pc−i)=∑i=1cg(pi)h(pc−i)+h(pc)f(p^c)=\sum_{i=0}^cg(p^i)h(p^{c-i})\\ =\sum_{i=1}^cg(p^i)h(p^{c-i})+h(p^c)

复杂度 O(c2)O(c^2) 。 可以预处理出小于 n\sqrt{n} 的素数的次幂的 hh 值。
最终复杂度: O(∑i=1n[i∈P]log⁡i2n)O(\sum_{i=1}^{\sqrt{n}}[i\in \mathbb{P}]\log_i^2n) 。

0x05 复杂度证明

由 素数定理得 : [x∈P]=π(x)−π(x−1)≈1xln⁡x−1xln⁡x−ln⁡x≈1ln⁡x[x\in\mathbb{P}]= \pi(x)-\pi(x-1)\approx \dfrac{1}{x\ln x}-\dfrac{1}{x\ln x-\ln x}\approx \dfrac{1}{\ln x}
此处只是期望
原式为

O(∑i=1nlog⁡i2nln⁡i)=∑i=1nln⁡2nln⁡3i≈ln⁡2n∫2n1ln3i=O(ln⁡2nnln⁡n)=O(nln⁡n)O(\sum_{i=1}^{\sqrt{n}}\dfrac{\log_i^2n}{\ln i})\\= \sum_{i=1}^{\sqrt{n}}\dfrac{\ln^2n}{\ln^3 i}\\ \approx\ln^2n\int_2^{\sqrt{n}}\dfrac{1}{ln^3i}\\ = O\left(\ln^2 n\dfrac{\sqrt{n}}{\ln n}\right)\\ = O\left(\sqrt{n}\ln n\right)

总复杂度为 O(nln⁡n+T∫0nx−12f(x)dx)O\left(\sqrt{n}\ln n+T\int_0^nx^{-\frac{1}{2}}f(x)dx\right) 。
其中 O(f(x))O(f(x)) 为 计算 G(x)G(x) 的复杂度。

0x06 更多

当 gg 是完全积性函数时

有

h(pc)=f(pc)−∑i=1cg(pi)h(pc−i)=f(pc)−g(p)∑i=0c−1g(pi)h(p(c−1)−i)=f(pc)−g(p)f(pc−1)h(p^c)=f(p^c) - \sum_{i=1}^cg(p^i)h(p^{c-i}) \\=f(p^c) - g(p)\sum_{i=0}^{c-1}g(p^i)h(p^{(c-1)-i})\\ =f(p^c)-g(p)f(p^{c-1})

0x07 习题

【模板】Min_25筛

f(p)=p∗(p−1)=id(p)φ(p)f(p)=p*(p-1)=id(p)\varphi(p) ,那么不妨设 g(x)=id(x)φ(x)g(x)=id(x)\varphi(x) ,易得 gg 是积性函数。
用杜教筛解决 G(n)G(n) 即可。

例题2

注意原式为 ∑i=1n((μ2φ)∗1)(i)\sum_{i=1}^n((\mu^2\varphi)*1)(i)。

例题3

注意原式为 ∑i=1n∑d∣iμ2(d)\sum_{i=1}^n\sum_{d|i}\mu^2(d)。

原文