洛谷 P4213 【模板】杜教筛(Sum)
\[\sum_{i=1}^{n} \mu(i)\\
\sum_{i=1}^{n} \varphi(i)\\
\]
杜教筛模板:
\[S(n) = \sum_{i=1}^{n} f(i)\\
\sum_{i=1}^{n} \sum_{d|i} f(d) * g(\frac{i}{d})\\
=\sum_{i=1}^{n} g(i) \sum_{d=1}^{\lfloor \frac{n}{i}\rfloor} f(d)\\
=\sum_{i=1}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor)\\
g(1)S(n) =\sum_{i=1}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor) - \sum_{i=2}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor)
\]
具体分析:
\[S1(n) = \sum_{i=1}^{n}\mu(i)\\
g = 1\\
g(1)S(n) =\sum_{i=1}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor) - \sum_{i=2}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor)\\
S1(n) =\sum_{i=1}^{n} \sum_{d|i}\mu(d) - \sum_{i=2}^{n} S1(\lfloor \frac{n}{i}\rfloor)\\
S1(n) =\sum_{i=1}^{n} [i =1] - \sum_{i=2}^{n} S1(\lfloor \frac{n}{i}\rfloor)\\
S1(n) =1 - \sum_{i=2}^{n} S1(\lfloor \frac{n}{i}\rfloor)\\
\]
\[S2(n) = \sum_{i=1}^{n}\phi(i)\\
g = 1\\
g(1)S(n) =\sum_{i=1}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor) - \sum_{i=2}^{n} g(i) S(\lfloor \frac{n}{i}\rfloor)\\
S2(n) =\sum_{i=1}^{n} \sum_{d|i}\phi(d) - \sum_{i=2}^{n} S2(\lfloor \frac{n}{i}\rfloor)\\
S2(n) =\sum_{i=1}^{n} i- \sum_{i=2}^{n} S2(\lfloor \frac{n}{i}\rfloor)\\
S2(n) =\frac{n*(n + 1)}{2} - \sum_{i=2}^{n} S2(\lfloor \frac{n}{i}\rfloor)\\
\]
其中:
\[\sum_{d|n}^{n} \mu(d) = [n = 1]\\
\sum_{d|n}^{n} \phi(d) = n\\
\]
上面的式子前半部分可以直接得出,后面的用数论分块+递归解决。