二项式反演
先证明一个等式:
\[\large \binom{n}{i}\binom{i}{j} = \binom{n}{j}\binom{n-j}{i-j}
\]
左边的组合意义是从 \(n\) 里选 \(i\),再从 \(i\) 里选 \(j\),右边是先选出 \(j\),再选出 \(i\)。
形式一
\[\large f(n)=\sum_{i=0}^n(-1)^i \binom{n}{i} g(i) \Leftrightarrow g(n)=\sum_{i=0}^n(-1)^i \binom{n}{i} f(i) \\
\large\begin{aligned}
&\sum_{i=0}^n(-1)^i \binom{n}{i} f(i) \\
=&\sum_{i=0}^n(-1)^i \binom{n}{i}\sum_{j=0}^i(-1)^j\binom{i}{j}g(j) \\
=&\sum_{j=0}^n g(j)\sum_{i=j}^n (-1)^{i+j} \binom{n}{i}\binom{i}{j} \\
=&\sum_{j=0}^n g(j)\sum_{i=j}^n (-1)^{i-j} \binom{n}{j}\binom{n-j}{i-j} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}\sum_{i=0}^{n-j} (-1)^i \binom{n-j}{i} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}(1-1)^{n-j} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}[n=j] \\
=&g(n)
\end{aligned}
\]
形式二
\[\large f(n)=\sum_{i=0}^n\binom{n}{i} g(i) \Leftrightarrow g(n)=\sum_{i=0}^n(-1)^{n-i} \binom{n}{i} f(i) \\
\large\begin{aligned}
&\sum_{i=0}^n(-1)^{n-i} \binom{n}{i} f(i) \\
=&\sum_{i=0}^n(-1)^{n-i} \binom{n}{i}\sum_{j=0}^i\binom{i}{j}g(j) \\
=&\sum_{j=0}^n g(j)\sum_{i=j}^n (-1)^{n-i} \binom{n}{i}\binom{i}{j} \\
=&\sum_{j=0}^n g(j)\sum_{i=j}^n (-1)^{n-i}\binom{n}{j}\binom{n-j}{n-i} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}\sum_{i=0}^{n-j} (-1)^{i} \binom{n-j}{i} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}(1-1)^{n-j} \\
=&\sum_{j=0}^n g(j)\binom{n}{j}[n=j] \\
=&g(n) \\
\end{aligned}
\]
其他
之前两个形式都是下三角,同样也有上三角的形式:
\[\large\begin{aligned}
f(m)=\sum_{i=m}^n(-1)^i \binom{i}{m} g(i) &\Leftrightarrow g(m)=\sum_{i=m}^n(-1)^i \binom{i}{m} f(i) \\
f(m)=\sum_{i=m}^n\binom{i}{m} g(i) &\Leftrightarrow g(m)=\sum_{i=m}^n(-1)^{i-m} \binom{i}{m} f(i)
\end{aligned}
\]
应用
错位排列
设 \(f(i)\) 为恰好有 \(i\) 位错排的方案数,得:
\[\large n!=\sum_{i=0}^n \binom{n}{i}f(n-i)=\sum_{i=0}^n \binom{n}{n-i}f(i)=\sum_{i=0}^n \binom{n}{i}f(i)
\]
二项式反演得:
\[\large\begin{aligned}
f(n)&=\sum_{i=0}^n(-1)^{n-i}\binom{n}{i}i! \\
&=n!\sum_{i=0}^n\frac{(-1)^{n-i}}{(n-i)!} \\
&=n!\sum_{i=0}^n\frac{(-1)^i}{i!}
\end{aligned}
\]
至多好算用 \(f(n)=\sum\limits_{i=0}^n\binom{n}{i} g(i) \Leftrightarrow g(n)=\sum\limits_{i=0}^n(-1)^{n-i} \binom{n}{i} f(i)\)。
至少好算用 \(f(m)=\sum\limits_{i=m}^n\binom{i}{m} g(i) \Leftrightarrow g(m)=\sum\limits_{i=m}^n(-1)^{i-m} \binom{i}{m} f(i)\)。