# 小学生都能看懂的生成函数入门教程

## 生成函数

### 普通生成函数

f[0][0] = 1;
for(int i = 1; i <= 3; i++)
for(int j = 0; j <= 8; j++) //当前总共要选出j个
for(int k = 0; k <= j; k++) //已经选了k个
if(j - k <= v[i]) //此时要选j-k个
f[i][j] += f[i - 1][k];


\begin{aligned} G(x) &= (1+x+x^2+x^3)(1+x+x^2)(1+x+x^2+x^3) \\ &= (1+2x+3x^2+3x^3+2x^4+x^5)(1+x+x^2+x^3)\\ &=1+3x+6x^2+9x^3+10x^4+9x^5+6x^6+3x^7+x^8 \end{aligned}

$x_1x_3^3+x_2x_3^3+x_1^2x_3^2+x_1x_2x_3^2+x_2^2x_3^2+x_1^3x_3+x_1^2x_2x_3+x_1x_2^2x_3+x_1^3x_3+x_1^2x_2^2$

## 指数生成函数

$S = \{a_1, a_2 \dots a_n\}, N = \sum_{i=1}^n a_i$，其中第$a_i$表示第$i$个物品有$a_i$个。

$x_1x_3^3+x_2x_3^3+x_1^2x_3^2+x_1x_2x_3^2+x_2^2x_3^2+x_1^3x_3+x_1^2x_2x_3+x_1x_2^2x_3+x_1^3x_3+x_1^2x_2^2$

$G_2(x) = 1 + \frac{x^1}{1} + \frac{x^2}{2}$

$G_3(x) = 1 + \frac{x^1}{1} + \frac{x^2}{2!} + \frac{x^3}{3!}$

\begin{aligned} G_e(x) &= (1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!})(1+\frac{x}{1!} + \frac{x^2}{2!}) (1 + \frac{x}{1!} + \frac{x^2}{2!} + \frac{x^3}{3!})\\ &= (1+2x+2x^2+\frac{7}{6}x^3 + \frac{5}{12}x^4 + \frac{1}{12}x^5) (1+x+\frac{1}{2}x^2 + \frac{1}{6}x^3)\\ &=(1+3x + \frac{9}{2}x^2 + \frac{14}{3}x^3 + \frac{35}{12}x^4 + \frac{17}{12}x^5 + \frac{35}{72} x^6 + \frac{8}{72}x^7 + \frac{1}{71}x^8) \end{aligned}

## 普通生成函数的推广

### 简单介绍

$S = 1 + x + x^2 + \dots x^{\infty}$

$xS = x + x^2 + x^3 + \dots x^{\infty}$

$S - xS = 1$

$S = \frac{1}{1-x}$

• $x$替换为$-x$$\frac{1}{1+x} = {1, -1, 1, -1, \dots}$

• $x$替换为$2x$$\ \frac{1}{1-2x} = {1, 2, 4, 8, 16, \dots}$

• $x$替换为$x^2$, $\ \ \ \frac{1}{1-x^2} = {1, 0, 1, 0, 1 \dots}$

• 将分子乘$2$, $\ \ \ \ \quad\frac{2}{1-x} = {2, 2, 2, 2, 2, \dots}$

• 将分子乘$x^3$, $\ \ \quad \frac{x^3}{1-x} \ = {0, 0, 0, 1, 1, 1, 1 \dots}$

• 求个导, $\qquad \ \ \ \ \ \frac{1}{(1-x)^2} = {1, 2, 3, 4, 5} \dots$

• 再求一次， $\quad \ \ \ \frac{2}{(1-x)^3} ={2 + 6 + 12 + 20 \dots}$

$\frac{1}{(1-x)^n} = \sum_{k=0}^{\infty} C_{n+k-1}^{k-1} x^k$

### 求斐波那契数列通项公式

$f_i = f_{i-1} + f_{i - 2}$

$f_0 = f_1 = 1$

$A = 1 + 1x + 2x^2 + 3x^3 + 5x^4 + 8x^5 \dots$

\begin{aligned} A = \ 1 + 1x + &2x^2 + 3x^3 + 5x^4 + 8x^5 \dots \\ xA = \ \ \qquad x + &1x^2 + 2x^3 + 3x^4 + 5x^5\dots \\ x^2A =\qquad \qquad &1x^2 + 1x^3 + 2x^4 + 3x^5 \dots \end{aligned}

$\frac{1}{1-x-x^2}$这玩意儿下半部分是个一元二次方程，我们可以配方

$1-x-x^2 = (1-\phi_1x)(1-\phi_2x)$

$\phi_1 = \frac{1+\sqrt{5}}{2}, \phi_2 = \frac{1-\sqrt{5}}{2}$

(解的时候可以直接把后面的式子拆开，把这两个式子对应项联立组成方程组, $\phi_1 \phi_2$的取值是可以反过来的)

$(a+b - 1) - x(a\phi_2 + b\phi_1) = 0$

$\begin{cases} a+b-1 = 0\\ a\phi_2 + b\phi_1 = 0 \end{cases}$

$A = \frac{\phi_1}{\sqrt{5}} \frac{1}{1-\phi_1x} - \frac{\phi_2}{\sqrt{5}} \frac{1}{1-\phi_2x}$

$A_n = \frac{1}{\sqrt{5}} ((\frac{1+\sqrt{5}}{2})^{n+1} - (\frac{1-\sqrt{5}}{2})^{n+1})$

### 解决组合类问题

$k$的倍数就是$\frac{1}{1-x^k}$

## 指数生成函数的推广

$e^x = \sum_{i=0}^{\infty} \frac{x^i}{i!}$

• $e^{-x} = 1 - \frac{x}{1} + \frac{x}{2!} -\frac{x}{3!} + \frac{x}{4!}\dots$

• $\frac{e^x + e^{-x}}{2} = 1 + \frac{x^2}{2!} + \frac{x^4}{4!} + \frac{x^6}{6!}\dots$

• $\frac{e^x - e^{-x}}{2} = \frac{x}{1} + \frac{x^3}{3!} + \frac{x^5}{5!} + \frac{x^7}{7!}$

• $e^{kx} = 1 + \frac{kx}{1} + \frac{k^2x^2}{2!} + \frac{k^3x^3}{3!} + \frac{k^4x^4}{4!} \dots$

### 一道简单的题目

$n \leqslant 10^9$

## 参考资料

posted @ 2019-03-13 10:58  自为风月马前卒  阅读(...)  评论(...编辑  收藏

……