【组合数学:四】生成函数 | 指数生成函数 | 线性齐次递推关系(上)
斐波那契数
- 之前博客整理过了 【小组专题三:斐波那契专题】
生成函数
- 一方面,我们可以把生成函数看成是代数对象,其形式上的处理使得人们可以通过代数手段计算一个问题的可能性的数目;另一方面,生成函数是无限可微分函数的泰勒级数。
通常我们默认级数是收敛的且只在其形式上操作幂级数。 - 设:
h 0 , h 1 , h 2 , ⋯ , h n , ⋯ h_0,h_1,h_2,\cdots,h_n,\cdots h0,h1,h2,⋯,hn,⋯
是无穷数列。它的生成函数定义为无穷级数
g ( x ) = ∑ i = 0 ∞ h i x i g(x)=\sum_{i=0}^\infin h_i x^i g(x)=i=0∑∞hixi
每一个有限数列都有一个生成函数(后面的所有项都为 0 0 0),它是一个多项式。 - 例子:每一项都等于
1
1
1 的无穷数列
1 , 1 , 1 , ⋯ , 1 , ⋯ 1,1,1,\cdots,1,\cdots 1,1,1,⋯,1,⋯
的生成函数是
g ( x ) = ∑ i = 0 ∞ x i = 1 1 − x g(x)=\sum_{i=0}^\infin x^i=\frac{1}{1-x} g(x)=i=0∑∞xi=1−x1 - 例子:设
m
m
m 是正整数。二项式系数
C m , 0 , C m , 1 , C m , 2 , ⋯ , C m , m C_{m,0}, C_{m,1}, C_{m,2} ,\cdots ,C_{m,m} Cm,0,Cm,1,Cm,2,⋯,Cm,m
的生成函数是
g m ( x ) = ∑ i = 0 m C m i x i = ( 1 + x ) m g_m(x)=\sum_{i=0}^m C_m^i x^i = (1+x)^m gm(x)=i=0∑mCmixi=(1+x)m - 例子:设
k
k
k 是整数,并设数列
h
n
h_n
hn 等于
e 1 + e 2 + ⋯ + e k = n e_1+e_2+\cdots+e_k=n e1+e2+⋯+ek=n
的非负整数解的个数。
它的生成函数是
g ( x ) = ( ∑ e 1 = 0 ∞ x e 1 ) ( ∑ e 2 = 0 ∞ x e 2 ) ⋯ ( ∑ e k = 0 ∞ x e 1 ) = 1 ( 1 − x ) × 1 ( 1 − x ) × ⋯ × 1 ( 1 − x ) = 1 ( 1 − x ) k \begin{aligned} g(x)&=(\sum_{e_1=0}^\infin x^{e_1})(\sum_{e_2=0}^\infin x^{e_2})\cdots(\sum_{e_k=0}^\infin x^{e_1})\\ &=\frac{1}{(1-x)}\times\frac{1}{(1-x)}\times\cdots\times\frac{1}{(1-x)}\\ &=\frac{1}{(1-x)^k} \end{aligned} g(x)=(e1=0∑∞xe1)(e2=0∑∞xe2)⋯(ek=0∑∞xe1)=(1−x)1×(1−x)1×⋯×(1−x)1=(1−x)k1
根据之前我们可知
g ( x ) = ∑ n = 0 ∞ C n + k − 1 n x n g(x)=\sum_{n=0}^\infin C_{n+k-1}^{n} x^n g(x)=n=0∑∞Cn+k−1nxn - 例子:求装有苹果、香蕉、橘子和梨子的果篮的数量
h
n
h_n
hn,其中在每个果篮中苹果的个数是偶数,香蕉数是
5
5
5 的倍数,橘子数最多是
4
4
4 个,梨子要么一个要么没有
解:直接写出生成函数
g ( x ) = ( 1 + x 2 + x 4 + ⋯ ) ( 1 + x 5 + x 10 + ⋯ ) ( 1 + x + x 2 + x 3 + x 4 ) ( 1 + x ) = 1 1 − x 2 1 1 − x 5 1 − x 5 1 − x ( 1 + x ) = 1 ( 1 − x ) 2 = ∑ n = 0 ∞ C n + 1 n x n \begin{aligned} g(x)&=(1+x^2+x^4+\cdots)(1+x^5+x^{10}+\cdots)(1+x+x^2+x^3+x^4)(1+x)\\ &=\frac{1}{1-x^2}\frac{1}{1-x^5}\frac{1-x^5}{1-x}(1+x)\\ &=\frac{1}{(1-x)^2}\\ &=\sum_{n=0}^\infin C_{n+1}^n x^n \end{aligned} g(x)=(1+x2+x4+⋯)(1+x5+x10+⋯)(1+x+x2+x3+x4)(1+x)=1−x211−x511−x1−x5(1+x)=(1−x)21=n=0∑∞Cn+1nxn
于是我们可以看到 h n = n + 1 h_n=n+1 hn=n+1
指数生成函数
- 之前的生成函数特别适合涉及二项式系数的数列;然而对于某些计数排序的项数列,更有效的方法是考虑关于下面单项式集合的生成函数
{ 1 , x , x 2 2 ! , ⋯ , x n n ! , ⋯ } \{1,x,\frac{x^2}{2!},\cdots,\frac{x^n}{n!},\cdots\} {1,x,2!x2,⋯,n!xn,⋯}
这些单项式出现在泰勒级数
e x = ∑ n = 0 ∞ x n n ! e^x=\sum_{n=0}^\infin \frac{x^n}{n!} ex=n=0∑∞n!xn
中。数列 h 0 , h 1 , ⋯ , h n , ⋯ h_0,h_1,\cdots,h_n,\cdots h0,h1,⋯,hn,⋯ 的指数生成函数定义为
g ( e ) ( x ) = ∑ n = 0 h n x n n ! g^{(e)}(x)=\sum_{n=0}h_n\frac{x^n}{n!} g(e)(x)=n=0∑hnn!xn
后文简记为 g ( x ) g(x) g(x) - 例子:设
n
n
n 是正整数。确定下面数列的指数生成函数
P ( n , 0 ) , P ( n , 1 ) , ⋯ , P ( n , n ) P(n,0),P(n,1),\cdots,P(n,n) P(n,0),P(n,1),⋯,P(n,n)
解:直接带入
g ( x ) = ∑ i = 0 n P ( n , i ) x i i ! = ∑ i = 0 n C n i x i = ( 1 + x ) n g(x)=\sum_{i=0}^n P(n,i) \frac{x^i}{i!}=\sum_{i=0}^n C_n^i x^i =(1+x)^n g(x)=i=0∑nP(n,i)i!xi=i=0∑nCnixi=(1+x)n - 例子:数列
{
1
,
1
,
1
,
⋯
,
1
,
⋯
}
\{1,1,1,\cdots,1,\cdots\}
{1,1,1,⋯,1,⋯} 的指数生成函数是
g
(
x
)
=
e
x
g(x)=e^x
g(x)=ex
更一般的,数列 { a 0 , a 1 , a 2 , ⋯ , a n , ⋯ } \{a^0,a^1,a^2,\cdots,a^n,\cdots\} {a0,a1,a2,⋯,an,⋯} 的指数生成函数是 g ( x ) = e a x g(x)=e^{ax} g(x)=eax - 定理:设
S
S
S 是多重集合
{
n
1
⋅
a
1
,
n
2
⋅
a
2
,
⋯
,
n
k
⋅
a
k
}
\{n_1\cdot a_1,n_2\cdot a_2,\cdots,n_k\cdot a_k\}
{n1⋅a1,n2⋅a2,⋯,nk⋅ak} ,其中
n
i
n_i
ni 是非负整数。设
h
n
h_n
hn 是
S
S
S 的
n
n
n 排列数。那么数列
h
0
,
h
1
,
⋯
,
h
n
,
⋯
h_0,h_1,\cdots,h_n,\cdots
h0,h1,⋯,hn,⋯ 的指数生成函数
g
(
x
)
g(x)
g(x) 为:
g ( x ) = ∏ f n i ( x ) g(x)=\prod f_{n_i}(x) g(x)=∏fni(x)
其中, ∀ i ∈ [ 1 , k ] \forall i\in[1,k] ∀i∈[1,k],有:
f n i ( x ) = 1 + x + x 2 2 ! + ⋯ + x n i n i ! f_{n_i}(x)=1+x+\frac{x^2}{2!}+\cdots+\frac{x^{n_i}}{n_i!} fni(x)=1+x+2!x2+⋯+ni!xni
证明略。
注意到当 n i = ∞ n_i=\infin ni=∞ 时,上面的定理仍然成立。 - 例子:用红、白、蓝三种颜色给
1
×
n
1\times n
1×n 的棋盘着色,要求红色格子是偶数个的方案数。
解:直接写出相应的指数生成函数:
g ( x ) = ( 1 + x 2 2 ! + x 4 4 ! + ⋯ ) ( 1 + x 1 1 ! + x 2 2 ! + ⋯ ) ( 1 + x 1 1 ! + x 2 2 ! + ⋯ ) = e x + e − x 2 e x e x = e 3 x + e x 2 = 1 2 ( ∑ 3 i x i i ! ) ( ∑ x i i ! ) = 1 2 ∑ ( 3 i + 1 ) x i i ! \begin{aligned} g(x)&=(1+\frac{x^2}{2!}+\frac{x^4}{4!}+\cdots)(1+\frac{x^1}{1!}+\frac{x^2}{2!}+\cdots)(1+\frac{x^1}{1!}+\frac{x^2}{2!}+\cdots)\\ &=\frac{e^x+e^{-x}}{2} e^x e^x\\ &=\frac{e^{3x}+e^x}{2}\\ &=\frac{1}{2}(\sum 3^i\frac{x^i}{i!})(\sum \frac{x^i}{i!})\\ &=\frac{1}{2}\sum (3^i+1)\frac{x^i}{i!} \end{aligned} g(x)=(1+2!x2+4!x4+⋯)(1+1!x1+2!x2+⋯)(1+1!x1+2!x2+⋯)=2ex+e−xexex=2e3x+ex=21(∑3ii!xi)(∑i!xi)=21∑(3i+1)i!xi
因此,我们能得到 h n = 1 2 ( 3 n + 1 ) h_n=\frac{1}{2}(3^n+1) hn=21(3n+1) - 例子:一个
n
n
n 位数,每个数字都是奇数且数字
1
1
1 和数字
3
3
3 出现偶数次的个数
h
n
h_n
hn
解:直接写出相应的指数生成函数:
g ( x ) = ( 1 + x 2 2 ! + x 4 4 ! + ⋯ ) 2 ( 1 + x + x 2 2 ! + ⋯ ) 3 = ( e x + e − x 2 ) 2 ⋅ e 3 x = 1 4 ( e 5 x + 2 e 3 x + e x ) = 1 4 ( ∑ ( 5 i + 2 ⋅ 3 i + 1 ) x i i ! ) \begin{aligned} g(x)&=(1+\frac{x^2}{2!}+\frac{x^4}{4!}+\cdots)^2(1+x+\frac{x^2}{2!}+\cdots)^3\\ &=(\frac{e^x+e^{-x}}{2})^2\cdot e^{3x}\\ &=\frac{1}{4}(e^{5x}+2e^{3x}+e^x)\\ &=\frac{1}{4}(\sum (5^i+2\cdot3^i+1)\frac{x^i}{i!}) \end{aligned} g(x)=(1+2!x2+4!x4+⋯)2(1+x+2!x2+⋯)3=(2ex+e−x)2⋅e3x=41(e5x+2e3x+ex)=41(∑(5i+2⋅3i+1)i!xi)
因此,我们得到 h n = 1 4 ( 5 n + 2 ⋅ 3 n + 1 ) h_n=\frac{1}{4}(5^n+2\cdot3^n+1) hn=41(5n+2⋅3n+1) - 例子:用红、白、蓝三色给
1
×
n
1\times n
1×n 的棋盘着色,其中红色格子有偶数个,蓝色格子至少一个的方案数
解:直接写指数生成函数:
g ( x ) = ( 1 + x 2 2 ! + x 4 4 ! + ⋯ ) ( 1 + x + x 2 2 ! + ⋯ ) ( x + x 2 2 ! + ⋯ ) = e x + e − x 2 e x × ( e x − 1 ) = e 3 x − e 2 x + e x − 1 2 = − 1 2 + ∑ 3 i − 2 i + 1 2 x i i ! \begin{aligned} g(x)&=(1+\frac{x^2}{2!}+\frac{x^4}{4!}+\cdots)(1+x+\frac{x^2}{2!}+\cdots)(x+\frac{x^2}{2!}+\cdots)\\ &=\frac{e^x+e^{-x}}{2} e^x \times(e^x-1)\\ &= \frac{e^{3x}-e^{2x}+e^x-1}{2}\\ &=-\frac{1}{2}+\sum \frac{3^i-2^i+1}{2} \frac{x^i}{i!} \end{aligned} g(x)=(1+2!x2+4!x4+⋯)(1+x+2!x2+⋯)(x+2!x2+⋯)=2ex+e−xex×(ex−1)=2e3x−e2x+ex−1=−21+∑23i−2i+1i!xi
注意到, h 0 = − 1 2 + 1 2 = 0 h_0=-\frac{1}{2}+\frac{1}{2}=0 h0=−21+21=0
h n = 3 n − 2 n + 1 2 , n > 0 h_n=\frac{3^n-2^n+1}{2},n>0 hn=23n−2n+1,n>0
求解线性齐次递推关系 (上)
- 设
h
0
,
h
1
,
⋯
,
h
n
,
⋯
h_0,h_1,\cdots,h_n,\cdots
h0,h1,⋯,hn,⋯ 是一个数列。称这个数列满足
k
k
k 阶线性递推关系是指存在量
a
1
,
a
2
,
⋯
,
a
k
(
a
k
≠
0
)
a_1,a_2,\cdots,a_k(a_k\ne 0)
a1,a2,⋯,ak(ak=0) 和量
b
n
b_n
bn ,使得
h n = a 1 h n − 1 + a 2 h n − 2 + ⋯ + a k h n − k + b n ( n ≥ k ) h_n=a_1h_{n-1}+a_2h_{n-2}+\cdots+a_kh_{n-k}+b_n (n\ge k) hn=a1hn−1+a2hn−2+⋯+akhn−k+bn(n≥k)
其中, a i , b i a_i,b_i ai,bi 或是常数,或是依赖于 n n n 的量。 - 我们称线性递推关系是 齐次的 (
h
o
m
o
g
e
n
e
o
u
s
homogeneous
homogeneous) ,如果
b
n
b_n
bn 是常数
0
0
0。
称它是 常系数 ( c o n s t a n t c o e f f i c i e n t constant\ coefficient constant coefficient) 的,如果量 a 1 , a 2 , ⋯ , a k a_1,a_2,\cdots,a_k a1,a2,⋯,ak 是常数。
我们后文讨论的是常系数线性齐次递推关系,即形如
h n − a 1 h n − 1 − a 2 h n − 2 − ⋯ − a k h n − k = 0 ( n ≥ k ) h_n-a_1h_{n-1}-a_2h_{n-2}-\cdots-a_kh_{n-k}=0\quad(n\ge k) hn−a1hn−1−a2hn−2−⋯−akhn−k=0(n≥k)
的多项式。 - 定理:如果多项式方程有
k
k
k 个不同的根
q
1
,
q
2
,
⋯
,
q
k
q_1,q_2,\cdots,q_k
q1,q2,⋯,qk,则
h n = ∑ i = 1 k c i q i n h_n=\sum_{i=1}^k c_iq_i^n hn=i=1∑kciqin
无论给定什么样的初始值 h 0 , h 1 , ⋯ , h k − 1 h_0,h_1,\cdots,h_{k-1} h0,h1,⋯,hk−1,都存在常数 c 1 , c 2 , ⋯ , c k c_1,c_2,\cdots,c_k c1,c2,⋯,ck,使得上述式子满足递推关系和初始条件的唯一数列。 - 例子:初始值
h
0
=
1
,
h
1
=
2
,
h
2
=
0
h_0=1,h_1=2,h_2=0
h0=1,h1=2,h2=0,求解下面的递推关系:
h n = 2 h n − 1 + h n − 2 − 2 h n − 3 ( n ≥ 3 ) h_n=2h_{n-1}+h_{n-2}-2h_{n-3}\quad(n\ge 3) hn=2hn−1+hn−2−2hn−3(n≥3)
解:这个递推关系的特征方程是:
x 3 − 2 x 2 − x + 2 = 0 x^3-2x^2-x+2=0 x3−2x2−x+2=0
它的三个根是 1 , − 1 , 2 1,-1,2 1,−1,2。根据定理,
h n = c 1 1 n + c 2 ( − 1 ) n + c 3 2 n h_n=c_11^n+c_2(-1)^n+c_32^n hn=c11n+c2(−1)n+c32n
是通解。现在,我们要求常数 c 1 , c 2 , c 3 c_1,c_2,c_3 c1,c2,c3,使得他们满足 h 0 , h 1 , h 2 h_0,h_1,h_2 h0,h1,h2 是成立的
利用消元法可以解出解是: c 1 = 2 , c 2 = − 2 3 , c 3 = − 1 3 c_1=2,c_2=-\frac{2}{3},c_3=-\frac{1}{3} c1=2,c2=−32,c3=−31。因此,
h n = 2 − 2 3 ( − 1 ) n − 1 3 2 n h_n=2-\frac{2}{3}(-1)^n-\frac{1}{3}2^n hn=2−32(−1)n−312n - 补充的内容:
1 ( 1 − r x ) n = ∑ k = 0 ∞ C n + k − 1 k r k x k \frac{1}{(1-rx)^n}=\sum_{k=0}^\infin C_{n+k-1}^k r^kx^k (1−rx)n1=k=0∑∞Cn+k−1krkxk
例子:确定下面数列的生成函数
0 , 1 , 4 , ⋯ , n 2 , ⋯ 0,1,4,\cdots,n^2,\cdots 0,1,4,⋯,n2,⋯
解:带入上述例子, r = 1 , n = 2 r=1,n=2 r=1,n=2,得到:
1 ( 1 − x ) 2 = 1 + 2 x + 3 x 2 + ⋯ + n x n − 1 + ⋯ x ( 1 − x ) 2 = x + 2 x 2 + 3 x 3 + ⋯ + n x n + ⋯ 1 + x ( 1 − x ) 3 = 1 + 2 2 x + 3 2 x 2 + ⋯ + n 2 x n − 1 + ⋯ x ( 1 + x ) ( 1 − x ) 3 = x + 2 2 x 2 + 3 2 x 3 + ⋯ + n 2 x n + ⋯ \begin{aligned} \frac{1}{(1-x)^2}&=1+2x+3x^2+\cdots+nx^{n-1}+\cdots\\ \frac{x}{(1-x)^2}&=x+2x^2+3x^3+\cdots+nx^n+\cdots\\ \frac{1+x}{(1-x)^3}&=1+2^2x+3^2x^2+\cdots+n^2x^{n-1}+\cdots\\ \frac{x(1+x)}{(1-x)^3}&=x+2^2x^2+3^2x^3+\cdots+n^2x^n+\cdots\\ \end{aligned} (1−x)21(1−x)2x(1−x)31+x(1−x)3x(1+x)=1+2x+3x2+⋯+nxn−1+⋯=x+2x2+3x3+⋯+nxn+⋯=1+22x+32x2+⋯+n2xn−1+⋯=x+22x2+32x3+⋯+n2xn+⋯ - 例子:求解下面的递推关系
h n = 5 h n − 1 − 6 h n − 2 h_n=5h_{n-1}-6h_{n-2} hn=5hn−1−6hn−2
初始值 h 0 = 1 , h 1 = 2 h_0=1,h_1=2 h0=1,h1=2
我们把递推关系写成下面这样的形式:
h n − 5 h n − 1 + 6 h n − 2 = 0 ( n ≥ 2 ) h_n-5h_{n-1}+6h_{n-2}=0\quad(n\ge 2) hn−5hn−1+6hn−2=0(n≥2)
设 g ( x ) = h 0 + h 1 x + h 2 x 2 + ⋯ + h n x n + ⋯ g(x)=h_0+h_1x+h_2x^2+\cdots+h_nx^n+\cdots g(x)=h0+h1x+h2x2+⋯+hnxn+⋯ 是数列 h 0 , h 1 , h 2 , ⋯ , h n , ⋯ h_0,h_1,h_2,\cdots,h_n,\cdots h0,h1,h2,⋯,hn,⋯ 的生成函数。
则有下面的一些方程,参照原来的递推关系,分别用 − 5 x , 6 x 2 -5x,6x^2 −5x,6x2 作为乘数乘以 g ( x ) g(x) g(x) 得到:
g ( x ) = h 0 + h 1 x + h 2 x 2 + ⋯ + h n x n + ⋯ − 5 x g ( x ) = − 5 h 0 x − 5 g 1 x − ⋯ − 5 h n − 1 x n − ⋯ 6 x 2 g ( x ) = 6 h 0 x 2 + ⋯ + 6 h n − 2 x n + ⋯ \begin{aligned} g(x)&=h_0+h_1x+h_2x^2+\cdots+h_nx^n+\cdots\\ -5xg(x)&=-5h_0x-5g_1x-\cdots-5h_{n-1}x^n-\cdots\\ 6x^2g(x)&=6h_0x^2+\cdots+6h_{n-2}x^n+\cdots\\ \end{aligned} g(x)−5xg(x)6x2g(x)=h0+h1x+h2x2+⋯+hnxn+⋯=−5h0x−5g1x−⋯−5hn−1xn−⋯=6h0x2+⋯+6hn−2xn+⋯
把上面三个方程加起来,我们得到
( 1 − 5 x + 6 x 2 ) g ( x ) = h 0 + ( h 1 − 5 h 0 ) x + ( h 2 − 5 h 1 + 6 h 0 ) x 2 + ⋯ + ( h n − 5 h n − 1 + 6 h n − 2 ) x n + ⋯ (1-5x+6x^2)g(x)=h_0+(h_1-5h_0)x+(h_2-5h_1+6h_0)x^2+\cdots+(h_n-5h_{n-1}+6h_{n-2})x^n+\cdots (1−5x+6x2)g(x)=h0+(h1−5h0)x+(h2−5h1+6h0)x2+⋯+(hn−5hn−1+6hn−2)xn+⋯
因为 h n − 5 h n − 1 + 6 h n − 2 = 0 ( n ≥ 2 ) h_n-5h_{n-1}+6h_{n-2}=0\quad(n\ge 2) hn−5hn−1+6hn−2=0(n≥2)
又因为 h 0 = 1 , h 1 = − 2 h_0=1,h_1=-2 h0=1,h1=−2,所以有:
( 1 − 5 x + 6 x 2 ) g ( x ) = h 0 + ( h 1 − 5 h 0 ) x = 1 − 7 x (1-5x+6x^2)g(x)=h_0+(h_1-5h_0)x=1-7x (1−5x+6x2)g(x)=h0+(h1−5h0)x=1−7x
因此:
g ( x ) = 1 − 7 x 1 − 5 x + 6 x 2 = c 1 1 − 2 x + c 2 1 − 3 x = 5 1 − 2 x − 4 1 − 3 x g(x)=\frac{1-7x}{1-5x+6x^2}=\frac{c_1}{1-2x}+\frac{c_2}{1-3x}=\frac{5}{1-2x}-\frac{4}{1-3x} g(x)=1−5x+6x21−7x=1−2xc1+1−3xc2=1−2x5−1−3x4
因此我们又可以得到:
g ( x ) = ∑ i = 0 ∞ ( 5 ⋅ 2 i − 4 ⋅ 3 i ) x i g(x)=\sum_{i=0}^\infin (5\cdot 2^i-4\cdot3^i)x^i g(x)=i=0∑∞(5⋅2i−4⋅3i)xi
所以得到 h n = 5 ⋅ 2 n − 4 ⋅ 3 n h_n=5\cdot 2^n -4\cdot 3^n hn=5⋅2n−4⋅3n
不过前提是特征方程的根是互不相同的,定理才成立。