生成函数

Author

李 辉

Published

April 2, 2026

递推关系

We have deposited 1000 yuan into a bank account that pays 5% interest at the end of each year.

At the beginning of each year, we deposit another 500 yuan into this account.

How much money will be in this account after 20 years?

a_0=1000, a_{n+1}=1.05\cdot a_n+500

a_{20}=?


Exercise 1 a_0=1000, a_{n+1}=1.05\cdot a_n+500

a_{20}=?


Derangement

Suppose that n persons are numbered 1,2,\cdots,n. Let there be n hats also numbered 1,2,\cdots,n. We have to find the number of ways in which no one gets the hat having same number as his/her number.

Let A_i be the set of all permutations of [n] in which the element i is in the ith position, in other words, in which the element i is fixed.

For example, 23541\in A_4.

The answer is connected with \#(\bigcup _{i=1}^{n}A_{i}).

\#A_i=(n-1)!


The set A_i\cap A_j consists of permutations in which elements i and j are fixed, and the remaining n-2 entries can be permuted freely, in (n-2)! ways. \#(A_i\cap A_j)=(n-2)!

So, the solution is n!-\left[\binom n1 (n-1)!-\binom n2 (n-2)!+\binom n3 (n-3)!\cdots\right]

or

\sum_{k=0}^{n}(-1)^k\binom nk (n-k)!\,\,\text{or}\,\,d_n=n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}.


Recursion Suppose we want to determine the number of derangements of the n integers 1,2,\cdots,n.

Let us focus on k and move it into the first position.

We thus have started a derangement, for 1 is not in its natural position. Where could 1 be placed?

There are two cases we could consider: either 1 is in position k or 1 is not in position k.


If 1 is in position k,

\begin{aligned} 1&2&3&\cdots&k-1&k&k+1&\cdots&n\\ k&?&?&\cdots&?&1&?&\cdots&?\\ \end{aligned}

There are n-2 integers yet to derange.

This can be done in d_{n-2} ways.

If 1 is not in position k,

\begin{aligned} 1&2&3&\cdots&k-1&k&k+1&\cdots&n\\ k&?&?&\cdots&?&?&?&\cdots&?\\ \end{aligned}

There are now n-1 integers to derange.

This can be done in d_{n-1} ways.


Putting this together, we have d_{n-1} + d_{n-2} possible derangements when k is in the first position.

There are (n-1) different ways to select the k element.

So, d_n = (n-1)\left[d_{n-1} + d_{n-2}\right]

d_1 = 0 and d_2 = 1

普通生成函数

A generating function is a formal power series in one indeterminate, whose coefficients encode information about a sequence of numbers a_n that is indexed by the natural numbers.

The ordinary generating function of a sequence a_n is

G(a_n,x)=\sum_{n=0}^\infty a_nx^n.


Example 1 a_0=50, a_{n+1}=4a_n-100.

Let G(x)=\sum_{n=0}^\infty a_nx^n.

\sum_{n=0}^\infty a_{n+1}x^{n+1}=\sum_{n=0}^\infty 4a_{n}x^{n+1}-\sum_{n=0}^\infty 100x^{n+1} G(x)-a_0=4xG(x)-\frac{100x}{1-x}.

G(x)=\frac{a_0}{1-4x}-\frac{100x}{(1-x)(1-4x)}


\frac{a_0}{1-4x}=50\sum_{n=0}^\infty (4x)^n=50\sum_{n=0}^\infty 4^nx^n

\frac{100x}{(1-x)(1-4x)}=-\frac{100/3}{1-x}+\frac{100/3}{1-4x} =\frac{100}{3}\left(\sum_{n=0}^\infty 4^nx^n-\sum_{n=0}^\infty x^n \right)

G(x)=50\sum_{n=0}^\infty 4^nx^n-\frac{100}{3}\left(\sum_{n=0}^\infty 4^nx^n-\sum_{n=0}^\infty x^n \right)

=\sum_{n=0}^\infty \left(50\cdot 4^n-100\cdot \frac{4^n-1}{3}\right)x^n


Since G(x)=\sum_{n=0}^\infty a_nx^n,

a_n=50\cdot 4^n-100\cdot \frac{4^n-1}{3}.

\sum_{n=0}^\infty x^n=\frac{1}{1-x}

\frac{A}{1-x}+\frac{B}{1-4x}=\frac{100x}{(1-x)(1-4x)}

A(1-4x)+B(1-x)=100x

(-B-4A)x+A+B=100x

\begin{cases} -B-4A=100\\ A+B=0\\ \end{cases}


Exercise 2 Please compute a_{20} using generating function.

a_0=1000

a_{n+1}=1.05\cdot a_n+500

斐波那契数列

Figure 1: A skew Yanghui’s Triangle

The sequence Fn of Fibonacci numbers is defined by the recurrence relation

F_n = F_{n-1} + F_{n-2},

with initial values:

F_1 = 1,\; F_2 = 1.


The generating function of the Fibonacci sequence is the power series

G(x)=\sum_{k=0}^{\infty} F_k x^k.

Next \begin{gather} = F_0 + F_1x + \sum_{k=2}^{\infty} \left( F_{k-1} + F_{k-2} \right) x^k \\ = x + \sum_{k=2}^{\infty} F_{k-1} x^k + \sum_{k=2}^{\infty} F_{k-2} x^k \end{gather}


\begin{gather} = x + x\sum_{k=0}^{\infty} F_k x^k + x^2\sum_{k=0}^{\infty} F_k x^k \\ = x + x G(x) + x^2 G(x). \end{gather}

Solving the equation

G(x)=x+xG(x)+x^2G(x)

Then

G(x)=\frac{x}{1-x-x^2}


Exercise 3 Try to obtain the explicit formula for the Fibonacci number F_n.


Theorem 1 The sum of the first n Fibonacci numbers can be expressed as F_{1}+F_{2}+\cdots+F_{n-1}+F_{n}=F_{n+2}-1.

Proof. \begin{aligned} F_1=F_3-F_2\\ F_2=F_4-F_3\\ F_3=F_5-F_4\\ \cdots\\ F_n=F_{n+2}-F_{n+1}\\ \end{aligned}  

F_1+F_2+\cdots+F_{n}=F_{n+2}-F_2=F_{n+2}-1\square


Theorem 2 The sum of the odd terms of the Fibonacci sequence

F_{1}+F_{3}+F_{5}+\cdots+F_{2n-1}=F_{2n}.

Exercise 4 Can you prove it?


Theorem 3 The sum of the even terms of the Fibonacci sequence F_{2}+F_{4}+F_{6}+\cdots+F_{2n}=F_{2n+1}-1.

Proof. F_1+F_2+\cdots+F_{2n}=F_{2n+2}-1 F_1+F_3+\cdots+F_{2n-1}=F_{2n}

F_2+F_4+\cdots+F_{2n}=F_{2n+2}-1-F_{2n}=F_{2n+1}-1\square


Theorem 4 The sum of the Fibonacci numbers with alternating signs

F_1-F_2+F_3-F_4+\cdots+(-1)^{n+1}F_n=(-1)^{n+1}F_{n-1}+1.

Exercise 5 Can you prove it?


Theorem 5 The sum of the squares of the first n Fibonacci numbers

F_1^2+F_2^2+\cdots+F_{n-1}^2+F_n^2=F_n F_{n+1}

Proof. F_k^2=F_k(F_{k+1}-F_{k-1})=F_k F_{k+1}-F_k F_{k-1}

\begin{aligned} F_1^2=F_1 F_2\\ F_2^2=F_2 F_3-F_1 F_2\\ F_3^2=F_3 F_4-F_2 F_3\\ \cdots\\ F_n^2=F_n F_{n+1}-F_{n-1}F_n\square\\ \end{aligned}


Johannes Kepler observed that the ratio of consecutive Fibonacci numbers converges.

The limit approaches the golden ratio \varphi.

\lim_{n\to\infty}\frac{F_{n+1}}{F_n}=\varphi.

Proof. Using the Fibonacci rule:

F_n = F_{n-1} + F_{n-2}.

\varphi^2 = \varphi + 1,

\varphi = \frac{1\pm\sqrt{5}}{2}\approx1.618\,or\,-0.618.\square

指数生成函数***

The exponential generating function of a sequence a_n is

\operatorname{E}(a_n,x)=\sum _{n=0}^{\infty} a_n \frac{x^n}{n!}.

Example 2 Let a_0 = 1, and let a_{n+1} = (n + 1)(a_n-n + 1), if n > 0. Let is try to find a closed formula for a_n.


The exponential generating function is E(x)=\sum_{n=0}^\infty a_n\frac{x^n}{n!}.

\sum_{n=0}^\infty a_{n+1}\frac{x^{n+1}}{{(n+1)}!}=\sum_{n=0}^\infty a_n\frac{x^{n+1}}{n!}-\sum_{n=0}^\infty (n-1)\frac{x^{n+1}}{n!}

E(x)-1=xE(x)-x^2 e^x+xe^x

E(x)=\frac{1}{1-x}+x e^x=\sum_{n=0}^\infty x^n+\sum_{n=0}^\infty \frac{x^{n+1}}{n!}.


E(x)=\sum_{n=0}^\infty n!\frac{x^n}{n!}+\sum_{n=0}^\infty (n+1)\frac{x^{n+1}}{(n+1)!}.

E(x)=\sum_{n=0}^\infty n!\frac{x^n}{n!}+\sum_{n=0}^\infty n\frac{x^{n}}{n!}.

Since E(x)=\sum_{n=0}^\infty a_n\frac{x^n}{n!},

a_n=n!+n.


Exercise 6 Let a_0 = 0. If n > 0, then a_{n+1} = 2(n + 1)a_n+(n+1)!

Try to find a closed formula for a_n.


Derangement

Start with the recurrence d_{n+1}=nd_n+nd_{n-1}. Define d_{-1}=0 which is consistent with the recurrence.

Multiply by \frac{x^n}{n!} and sum over n\ge 0,

\sum_{n\ge 0}d_{n+1}\frac{x^n}{n!}=\sum_{n\ge 0}nd_n\frac{x^n}{n!}+\sum_{n\ge 0}nd_{n-1}\frac{x^n}{n!}.


Let E(x)=\sum_{n\ge 0}d_n\frac{x^n}{n!} be the exponential generating function.

E\,'(x)=\sum_{n\ge 0}nd_n\frac{x^{n-1}}{n!}=\sum_{n\ge 1}d_n\frac{x^{n-1}}{(n-1)!}=\sum_{n\ge 0}d_{n+1}\frac{x^n}{n!},\qquad

xE\,'(x)=x\sum_{n\ge 0}nd_n\frac{x^{n-1}}{n!}=\sum_{n\ge 0}nd_n\frac{x^n}{n!},

xE(x)=\sum_{n\ge 0}d_n\frac{x^{n+1}}{n!}\qquad


=\sum_{n\ge 0}(n+1)d_n\frac{x^{n+1}}{(n+1)!}=\sum_{n\ge 1}nd_{n-1}\frac{x^n}{n!}=\sum_{n\ge 0}nd_{n-1}\frac{x^n}{n!}\qquad

So, E'(x)=xE'(x)+xE(x),\,\,\,\frac{E'(x)}{E(x)}=\frac{x}{1-x}=-1+\frac1{1-x},\,\,\,E(x)=\frac{e^{-x}}{1-x}.

E(x)=\left(1+x+x^2+\cdots+x^n+\cdots\right)\cdot

\left(1-\frac{x}{1!}+\frac{x^2}{2!}-\cdots+(-1)^n\frac{x^n}{n!}+\cdots\right)=\sum_{n=0}^\infty \left[1-\frac{1}{1!}+\frac{1}{2!}-\cdots+\frac{(-1)^n}{n!}\right]x^n

d_n=\left[1-\frac{1}{1!}+\frac{1}{2!}-\cdots+\frac{(-1)^n}{n!}\right]\cdot n!