杜教筛求积性函数的前缀和 O ( n 3 4 ) O(n^{\frac{3}{4}}) O(n43),预处理后的时间复杂度 O ( n 2 3 ) O(n^{\frac{2}{3}}) O(n32)
一般的推导过程
(
f
∗
g
)
(
n
)
=
g
(
d
)
f
(
n
d
)
(f*g)(n)=g(d)f(\frac{n}{d})
(f∗g)(n)=g(d)f(dn)是两个数论函数的卷积
∑
i
=
1
n
(
f
∗
g
)
(
i
)
=
∑
i
=
1
n
∑
d
∣
i
g
(
d
)
f
(
i
d
)
=
∑
d
=
1
n
∑
d
∣
i
g
(
d
)
f
(
i
d
)
=
∑
d
=
1
n
g
(
d
)
∑
d
∣
i
f
(
i
d
)
=
∑
d
=
1
n
g
(
d
)
∑
i
=
1
⌊
n
d
⌋
f
(
i
)
\sum_{i=1}^{n}(f*g)(i)=\sum_{i=1}^{n}\sum_{d|i}g(d)f(\frac{i}{d}) \\ =\sum_{d=1}^{n}\sum_{d|i}g(d)f(\frac{i}{d}) \\ =\sum_{d=1}^{n}g(d)\sum_{d|i}f(\frac{i}{d}) \\ =\sum_{d=1}^{n}g(d)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}f(i)
i=1∑n(f∗g)(i)=i=1∑nd∣i∑g(d)f(di)=d=1∑nd∣i∑g(d)f(di)=d=1∑ng(d)d∣i∑f(di)=d=1∑ng(d)i=1∑⌊dn⌋f(i)
记
S
(
n
)
=
∑
i
=
1
n
f
(
i
)
S(n)=\sum_{i=1}^{n}f(i)
S(n)=∑i=1nf(i)
∑
d
=
1
n
g
(
d
)
∑
i
=
1
⌊
n
d
⌋
f
(
i
)
=
∑
d
=
1
n
g
(
d
)
S
(
⌊
n
d
⌋
)
\sum_{d=1}^{n}g(d)\sum_{i=1}^{\lfloor\frac{n}{d}\rfloor}f(i) \\ =\sum_{d=1}^{n}g(d)S(\lfloor\frac{n}{d}\rfloor)
d=1∑ng(d)i=1∑⌊dn⌋f(i)=d=1∑ng(d)S(⌊dn⌋)
由数论函数的性质可知,
g
(
1
)
=
1
g(1)=1
g(1)=1,那么
∑
i
=
1
n
(
f
∗
g
)
(
i
)
=
∑
d
=
1
n
g
(
d
)
S
(
⌊
n
d
⌋
)
=
∑
d
=
2
n
g
(
d
)
S
(
⌊
n
d
⌋
)
+
g
(
1
)
∗
S
(
n
)
=
∑
d
=
2
n
g
(
d
)
S
(
⌊
n
d
⌋
)
+
S
(
n
)
\sum_{i=1}^{n}(f*g)(i)=\sum_{d=1}^{n}g(d)S(\lfloor\frac{n}{d}\rfloor) \\ =\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor)+g(1)*S(n) \\ =\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor)+S(n)
i=1∑n(f∗g)(i)=d=1∑ng(d)S(⌊dn⌋)=d=2∑ng(d)S(⌊dn⌋)+g(1)∗S(n)=d=2∑ng(d)S(⌊dn⌋)+S(n)
那么
S
(
n
)
=
∑
i
=
1
n
(
f
∗
g
)
(
i
)
−
∑
d
=
2
n
g
(
d
)
S
(
⌊
n
d
⌋
)
S(n)=\sum_{i=1}^{n}(f*g)(i)-\sum_{d=2}^ng(d)S(\lfloor\frac{n}{d}\rfloor)
S(n)=i=1∑n(f∗g)(i)−d=2∑ng(d)S(⌊dn⌋)
常见的卷积公式
∑
d
∣
n
d
=
1
∗
i
d
\sum_{d|n}d=1*id
∑d∣nd=1∗id
1
∗
μ
=
e
1*\mu=e
1∗μ=e
φ
∗
1
=
i
d
\varphi*1=id
φ∗1=id
φ
=
i
d
∗
μ
\varphi=id*\mu
φ=id∗μ
一些式子的总结
令
f
(
i
)
=
i
φ
(
i
)
f(i)=i\varphi(i)
f(i)=iφ(i)
(
f
∗
i
d
)
(
n
)
=
∑
d
∣
n
d
φ
(
d
)
n
d
=
n
∑
d
∣
n
φ
(
d
)
=
n
2
(f*id)(n)=\sum_{d|n}d\varphi(d)\frac{n}{d}=n\sum_{d|n}\varphi(d)=n^2
(f∗id)(n)=∑d∣ndφ(d)dn=n∑d∣nφ(d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phi(i)=\sum_{i=1}^{n}(id)^2-\sum_{d=2}^ndS(\lfloor\frac{n}{d}\rfloor) \\.... \\ S(n)=\sum_{i=1}^ni^n\varphi(i)=\sum_{i=1}^n(id)^{n+1}-\sum_{d=2}^nd^nS(\lfloor\frac{n}{d}\rfloor)
S(n)=i=1∑nφ(i)=i=1∑nid−d=2∑nS(⌊dn⌋)S(n)=i=1∑niφ(i)=i=1∑n(id)2−d=2∑ndS(⌊dn⌋)....S(n)=i=1∑ninφ(i)=i=1∑n(id)n+1−d=2∑ndnS(⌊dn⌋)