伯努利数学习笔记
伯努利数\(B_i\)的生成函数定义如下
\[B(z) = \frac{ze^z}{e^z - 1} = \sum_{i = 0} ^ {\infty} B_i \times \frac{z^i}{i!}
\]
一次多项式求逆即可快速计算\(B_i\)
那么它有啥用呢
可以用来计算自然数幂和
我们记
\[S_p(n) = \sum_{i = 1} ^ {n} i^p
\]
那么
\[
\begin{aligned}
A(z,n) &= \sum_{p=0}^{\infty} S_p(n) \times \frac{z^p}{p!}\\
&= \sum_{p=0}^{\infty} \sum_{i=1}^{n} \frac{(iz)^p}{p!}\\
&= \sum_{i=1}^{n} e^{iz} = \frac{1-e^{nz}}{e^{-z}-1}\\
&= \frac{ze^z}{e^z - 1} \times \frac{e^{nz} - 1}{z}\\
&= B(z) \times \frac{e^{nz} - 1}{z}\\
&= \sum_{j=0}^{\infty} B_j \times \frac{z^{j-1}}{j!} \times (\sum_{k=1}^{\infty}\frac{(nz)^k}{k!})\\
&= \sum_{p=0}^{\infty} (\sum_{j=0}^{p} B_j \times \frac{z^{j-1}}{j!} \times \frac{(nz)^{p+1-j}}{(p+1-j)!})\\
&= \sum_{p=0}^{\infty} z^p \times (\sum_{j=0}^{p} \frac{B_j \times n^{p+1-j}}{j! \times {(p+1-j)!}})\\
&= \sum_{p=0}^{\infty} z^p \times \frac{1}{p+1} \times \frac{1}{p!} \times (\sum_{j=0}^{p} B_j \times \binom{p+1}{j} \times n^{p+1-j})\\
\end{aligned}
\]
对比系数不难发现
\[S_p(n) = \frac{1}{p+1} \times \sum_{j=0}^{p} \binom{p+1}{j} \times B_j \times n^{p+1-j}
\]
一道简单的应用
2018 Multi-University Training Contest 4 Delighful Formulas
根据题意列出式子:
\[Ans = \sum_{i = 1} ^ {N} {[\gcd(i, N) = 1] \sum_{j = 1} ^ {i} {j ^ K}}
\]
莫比乌斯反演:
\[
\begin{aligned}
Ans & = \sum_{d \mid N} {\mu(d) \sum_{i = 1} ^ {N} {[d \mid i] \sum_{j = 1} ^ {i} {j ^ K}}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{i = 1} ^ {\frac{N} {d}} \sum_{j = 1} ^ {id} {j ^ K}}\\
\end{aligned}
\]
定义 \(F\):
\[F_p(N) = \sum_{i = 1} ^ {N} {i ^ p}
\]
显然 \(F_p\) 是 \(p + 1\) 阶多项式:
\[F_p(N) = \sum_{i = 0} ^ {p + 1} {a_{p, i} N ^ i}
\]
利用 \(F\) 化简原式:
\[
\begin{aligned}
Ans & = \sum_{d \mid N} {\mu(d) \sum_{i = 1} ^ {\frac{N} {d}} {F_K(id)}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{i = 1} ^ {\frac{N} {d}} \sum_{j = 0} ^ {K + 1} {a_{K, j} (id) ^ j}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{j = 0} ^ {K + 1} {a_{K, j} d ^ j \sum_{i = 1} ^ {\frac{N} {d}} {i ^ j}}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{j = 0} ^ {K + 1} {a_{K, j} d ^ j F_j(\frac{N}{d})}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{j = 0} ^ {K + 1} {a_{K, j} d ^ j \sum_{k = 0} ^ {j + 1} {a_{j, k} (\frac{N} {d}) ^ k}}} \\
& = \sum_{d \mid N} {\mu(d) \sum_{i = -1} ^ {K + 1} {d ^ i \sum_{j = 0} ^ {K + 1} \sum_{k = 0} ^ {j + 1} {[j - k = i] a_{K, j} a_{j, k} N ^ k}}} \\
\end{aligned}
\]
定义 \(G\):
\[G_i = \sum_{j = 0} ^ {K + 1} \sum_{k = 0} ^ {j + 1} {[j - k = i] a_{K, j} a_{j, k} N ^ k}
\]
看题解发现这个是可以算的,只需要用伯努利数展开一下
\[
\begin{aligned}
G_i &= \sum_{j = 0} ^ {K + 1} \sum_{k = 0} ^ {j + 1} {[j - k = i] a_{K, j} a_{j, k} N ^ k}\\
&= \sum_{j = 0} ^ {K + 1} \sum_{k = 0} ^ {j + 1} [j - k = i] B_{K + 1 - j} \times \frac{K!}{j!\times (K + 1 - j)!} \times B_{j + 1 - k} \times \frac{j!}{k! \times (j + 1 - k)!} \times N ^ k\\
&= K! \times \frac{B_{i+1}}{(i+1)!} \times \sum_{j = 0} ^ {K + 1} \sum_{k = 0} ^ {j + 1} [j - k = i] \frac{B_{K + 1 - j}}{(K + 1 - j)!} \times \frac{N^k}{k!}\\
\end{aligned}
\]
容易发现这是一个卷积形式,NTT一次即可
那么答案就是
\[
\begin{aligned}
Ans & = \sum_{d \mid N} {\mu(d) \sum_{i = -1} ^ {K + 1} {d ^ i G_i}} \\
& = \sum_{i = -1} ^ {K + 1} {G_i \sum_{d \mid N} {\mu(d) d ^ i}} \\
& = \sum_{i = -1} ^ {K + 1} {G_i \prod_{p \mid N} {(1 - p ^ i)}} \\
\end{aligned}
\]
\(O(KM)\)计算即可
总复杂度是\(O(K(logK + M))\)