Generating Funx Maths Ae - A11
Generating Funx Maths Ae - A11
Generating Funx Maths Ae - A11
Page 1 of 17
GENERATING FUNCTIONS
1. Introduction
The concept of generating functions is a powerful tool for solving counting problems.
Intuitively put, its general idea is as follows. In counting problems, we are often interested in
counting the number of objects of size n, which we denote by
n
a . By varying n, we get different
values of
n
a . In this way we get a sequence of real numbers
0
a ,
1
a ,
2
a ,
from which we can define a power series (which in some sense can be regarded as an infinite-
degree polynomial)
2
0 1 2
( ) G x a a x a x = + + +.
The above ( ) G x is the generating function for the sequence
0
a ,
1
a ,
2
a , .
In this set of notes we will look at some elementary applications of generating functions.
Before formally introducing the tool, let us look at the following example.
Example 1.1.
(IMO 2001 HK Preliminary Selection Contest) Find the coefficient of
17
x in the expansion of
5 7 20
(1 ) x x + + .
Solution.
The only way to form an
17
x term is to gather two
5
x and one
7
x . Since there are
20
2
190 C = ways
to choose two
5
x from the 20 multiplicands and 18 ways to choose one
7
x from the remaining 18
multiplicands, the answer is 190 18 3420 = .
To gain a preliminary insight into how generating functions is related to counting, let us
describe the above problem in another way. Suppose there are 20 bags, each containing a $5 coin
and a $7 coin. If we can use at most one coin from each bag, in how many different ways can we
pay $17, assuming that all coins are distinguishable (i.e. the $5 coin from the first bag is considered
to be different from that in the second bag, and so on)? It should be quite clear that the answer is
again 3420 to pay $17, one must use two $5 coins and one $7 coin. There are
20
2
190 C = ways to
choose two $5 coins from the 20 bags, and 18 ways to choose a $7 coin from the remaining 18 bags.
Using the notations we introduced at the very beginning, we could say that
17
3420 a = .
Mathematical Database
Page 2 of 17
2. Techniques of Computation
Let us once again give the definition of a generating function before we proceed.
Definition. Given a sequence
0
a ,
1
a ,
2
a , , we define the generating function of the
sequence { }
n
a to be the power series
2
0 1 2
( ) G x a a x a x = + + +.
Let us look at a few examples.
Example 2.1.
Find the generating functions for the following sequences. In each case, try to simplify the answer.
(a) 1, 1, 1, 1, 1, 1, 0, 0, 0, 0,
(b) 1, 1, 1, 1, 1,
(c) 1, 3, 3, 1, 0, 0, 0, 0,
(d)
2005
0
C ,
2005
1
C ,
2005
2
C , ,
2005
2005
C , 0, 0, 0, 0,
Solution.
(a) The generating function is
2 3 4 5 6 7
2 3 4 5
( ) 1 1 1 1 1 1 0 0
1
G x x x x x x x x
x x x x x
= + + + + + + + +
= + + + + +
We can apply the formula for the sum of a geometric series to rewrite ( ) G x as
6
1
( )
1
x
G x
x
.
(b) The generating function is
2 3 4
( ) 1 G x x x x x = + + + + +.
When | | 1 x < , we can apply the formula for the sum to infinity of a geometric series to rewrite
( ) G x as
1
( )
1
G x
x
=
.
(c) The generating function is
2
( ) 1 3 3 1 G x x x = + + + , and of course, the binomial theorem enables
us to simplify the answer as
3
( ) (1 ) G x x = + .
Mathematical Database
Page 3 of 17
(d) The generating function is
2005 2005 2005 2 2005 2004 2005 2005
0 1 2 2004 2005
( ) G x C C x C x C x C x = + + + + + ,
and the binomial theorem once again enables us to simplify the answer as
2005
( ) (1 ) G x x = + .
When dealing with computations of generating functions, we are particularly interested with
two things, namely, whether the generating function can be written in closed form and whether we
can find the coefficient of a certain power of x easily.
To write a generating function in closed form means, in general, writing it in a direct form
without summation sign nor . For instance, in Example 2.1 (b),
2 3 4
( ) 1 G x x x x x = + + + + +
is not in closed form while
1
( )
1
G x
x
=
=
+
,
then finding a specific coefficient will be easy. However, if a generating function is given in closed
form, ingenious tricks are sometimes required in determining certain coefficients. The following
example illustrates some common tricks.
Example 2.2.
In each of the following, find the coefficient of
2005
x in the generating function ( ) G x .
(a)
5000
( ) (1 2 ) G x x =
(b)
1
( )
1 3
G x
x
=
+
(c)
2
1
( )
(1 5 )
G x
x
=
+
Solution.
Mathematical Database
Page 4 of 17
(a) By the binomial theorem, we have
5000 5000 2 5000 4999 5000
1 2 4999
( ) 1 (2 ) (2 ) (2 ) (2 ) G x C x C x C x x = + + .
From this, we see that the coefficient of
2005
x is
2005 5000
2005
2 C .
(b) Recalling the formula for the sum to infinity of a geometric series, we have (noting once again
that everything is dealt with formally, ignoring questions of convergence)
2 3 2 2 3 3
1
( ) 1 ( 3 ) ( 3 ) ( 3 ) 1 3 3 3
1 ( 3 )
G x x x x x x x
x
= = + + + + = + +
.
From this, we see that the coefficient of
2005
x is
2005
3 .
(c) As in (b), we know that
2 2 3 3
1
1 5 5 5
1 5
x x x
x
= + +
+
.
Hence
2 2 3 3 2 2 3 3
( ) (1 5 5 5 )(1 5 5 5 ) G x x x x x x x = + + + + .
To form an
2005
x term, we can multiply 1 with
2005 2005
5 x , 5x with
2004 2004
5 x ,
2 2
5 x with
2003 2003
5 x and so on, and finally
2005 2005
5 x with 1. There are altogether 2006 products, each
equal to
2005 2005
5 x . It follows that the coefficient of
2005
x is
2005
2006 5 .
The technique used in Example 2.2 (c) is rather ad-hoc in nature. It will not work if the
power 2 is increased to higher powers. (At least it will involve much more analysis.) To deal with
higher powers, we shall need an extended version of the binomial theorem. In fact, the generating
function in Example 2.2 (b) and (c) can be written as
1
( ) (1 3 ) G x x
= + and
2
( ) (1 5 ) G x x
= +
respectively. If we can expand them using more or less the same method as in Example 2.2 (a), then
the computation will be much simpler. For this purpose, we attempt to generalize the binomial
theorem for positive integral indices. We begin by extending the usual notion of binomial
coefficients to non-integer values.
Definition. For any real number u and positive integer k, we define the extended binomial
coefficient
u
k
C by
( 1)( 2) ( 1)
!
u
k
u u u u k
C
k
+
=
.
We also define
0
1
u
C = for any real number u.
Mathematical Database
Page 5 of 17
Clearly, if u is a positive integer with u k , then the above extended binomial coefficient
agrees with the usual binomial coefficient. Also, if u and k are positive integers with u k < , then we
have 0
u
k
C = . This is natural from a combinatorial point of view: if u k < , there is no way to choose
k different objects from a collection of u objects. With the notion of the extended binomial
coefficients, we can state the extended binomial theorem as follows.
Theorem (Extended Binomial Theorem). For any real number u, we have
2
0 1 2
(1 )
u u u u
x C C x C x + = + + +.
Again, if u is a positive integer, we see that the extended binomial theorem agrees with the
usual binomial theorem by noting that 0
u
k
C = when u k < .
Now if we go back to Example 2.2 (c), we see at once, with the extended binomial theorem,
that the answer is
2005 2
2005
5 C
= = =
,
so that the answer agrees with what we obtained in Example 2.2 (c).
Since we often have to compute extended binomial coefficients of the form
u
k
C where u is a
negative integer, it is useful to relate the extended binomial coefficients with the usual binomial
coefficients. We have the following.
Theorem. For positive integers n and r, we have
1
( 1)
n r n r
r r
C C
+
= .
Hence we have
2 2005 2 2005 1 2006
2005 2005 2005
( 1) 2006 C C C
+
= = = , as we have previously worked out. It
should be noted that the right hand side of the above formula is in fact ( 1)
r n
r
H .
Mathematical Database
Page 6 of 17
With the extended binomial theorem, computation of coefficients for generating functions like
5
1
(1 ) x +
and 1 x + becomes easy. Sometimes we also need the technique of partial fraction
decomposition in the computation, as the following example shows.
Example 2.3.
What is the coefficient of
2005
x in the generating function
2 2
1
( )
(1 ) (1 )
G x
x x
=
+
?
Solution.
Let
2 2 2 2
1
(1 ) (1 ) 1 (1 ) 1 (1 )
A B C D
x x x x x x
= + + +
+ + +
.
Upon simplification, the right hand side becomes
3 2
2 2
( ) ( ) ( 2 2 ) ( )
(1 ) (1 )
C A x B D A C x A B C D x A B C D
x x
+ + + + + + + +
+
.
Comparing coefficients, we have
0
0
2 2 0
1
C A
B D A C
A B C D
A B C D
=
+ =
+ =
+ + + =
.
Solving, we get
1
4
A B C D = = = = . It follows that
1 2 1 2
1
( ) (1 ) (1 ) (1 ) (1 )
4
G x x x x x
( = + + + + +
.
Thus the coefficient of
2005
x is
1 2 1 2
2005 2005 2005 2005
1
( ) 0
4
C C C C
+ + = .
Finally, for those who know calculus, the following two examples illustrate some further
computation techniques in dealing with generating functions.
Example 2.4.
Find the generating functions of the following sequences in closed form.
(a) 1, 2, 3, 4, 5, 6, 7
(b) 0, 1,
1
2
,
1
3
,
1
4
,
Mathematical Database
Page 7 of 17
Solution.
Formally, we can differentiate and integrate a power series term by term. In other words, if
2 3
0 1 2 3
( ) G x a a x a x a x = + + + +,
then
2
1 2 3
'( ) 2 3 G x a a x a x = + + +.
The same is true for integration in place of differentiation.
(a) The generating function is
2 3
2 3 4
2
( ) 1 2 3 4
( )
1
1
(1 )
G x x x x
d
x x x x
dx
d x
dx x
x
= + + + +
= + + + +
| |
=
|
\ .
=
We can verify the answer by expanding
2
1
(1 ) x
using the extended binomial theorem.
(b) The generating function is
2 3 4
2 3
1 1 1
( )
2 3 4
(1 )
1
ln(1 )
G x x x x x
x x x dx
dx
x
x C
= + +
= + +
=
+
= + +
To find the constant C, we put in 0 x = to get (0) C G = . If we write
2
0 1 2
( ) G x a a x a x = + + +,
then (0) G is simply equal to
0
a , which is 0 in this case. Hence the answer is
( ) ln(1 ) G x x = + .
Example 2.5.
Find the coefficient of
2005
x for each of the following generating functions.
(a) ( ) ln(1 ) G x x =
(b) ( ) sin G x x =
Mathematical Database
Page 8 of 17
Solution.
(a) We have
2 3
2 3 4
1
( )
1
( 1 )
1 1 1
2 3 4
G x dx
x
x x x dx
C x x x x
| |
=
|
\ .
=
=
where (0) 0 C G = = . From the above expression, we see that the coefficient of
2005
x is
1
2005
.
(b) Let
2
0 1 2
sin ( ) x G x a a x a x = = + + +. Setting 0 x = , we have
0
0 a = .
Differentiating both sides of ( ) G x with respect to x, we have
2
1 2 3
cos 2 3 x a a x a x = + + +.
Setting 0 x = again, we have
1
1 a = . Differentiating the above equality with respect to x, we
have
2
2 3 4
sin 2 1 3 2 4 3 x a a x a x = + + +.
Again, setting 0 x = gives
2
0 2a = . Continuing this process, we see that
3
4
5
6
7
8
1 3!
0 4!
1 5!
0 6!
1 7!
0 8!
a
a
a
a
a
a
=
=
=
=
=
=
Since 2005 1 (mod 4), the coefficient of
2005
x is
1
2005!
.
For those who know about Taylor series, it should be noted that exactly the same idea is
employed in part (b) above. In fact, given a generating function ( ) G x in closed form, the coefficient
of
n
x is given by
( )
1
(0)
!
n
n
a G
n
.
Mathematical Database
Page 9 of 17
3. Applications of Generating Functions
A. Basic Counting Problems
Some well-known results in combinatorics can be reproduced by means of generating
functions, as the following examples show.
Example 3.1.
There are 50 students in the International Mathematical Olympiad (IMO) training programme. 6 of
them are to be selected to represent Hong Kong in the IMO. How many ways are there to select 6
students?
Solution.
Each student is either selected or not selected. Hence each student contributes a factor of 1 x + to
the generating function, where the term 1 (i.e.
0
x ) refers to the case when the student is not selected
(i.e. the student occupies 0 place) while the term x (i.e.
1
x ) refers to the case when the student is
selected (i.e. the student occupies 1 place). Since there are 50 students, the generating function is
50
( ) (1 ) G x x = + .
Since 6 students are to be selected, the answer is the coefficient of
6
x in ( ) G x , which, according to
the binomial theorem, is
50
6
C . This, of course, agreed with what we would have obtained without
using generating functions.
It is worth noting how the generating function is formed. Basically, it is formed by a sequence
of +s and s, corresponding to a sequence of ORs and ANDs, very much like how counting
problems are typically formulated. For each student, he is either selected OR not selected, so each
student contributes a factor of 1 x + . Now we need to do the same for the 1st student AND the 2nd
student AND the 3rd student AND so on. Thats why we multiply 50 copies of 1 x + together to
form the generating function.
Example 3.2.
There are 30 identical souvenirs, to be distributed among the 50 IMO trainees, and each trainee may
get more than one souvenir. How many ways are there to distribute the 30 souvenirs among the 50
trainees?
Solution.
Mathematical Database
Page 10 of 17
Each student may get 0 OR 1 OR 2 OR souvenirs, thus contributing a factor of
2
1 x x + + +.
Since there are 50 students, the generating function is
50
2 50 50
1
( ) (1 ) (1 )
1
G x x x x
x
| |
= + + + = =
|
\ .
.
As there are 30 souvenirs, the answer is the coefficient of
30
x in ( ) G x , which, according to the
extended binomial theorem, is equal to
50 79
30 30
C C
= + + + + + +
= + + +
| |
=
|
\ .
= +
= +
The answer is the coefficient of
6
x in ( ) G x . To get an
6
x term, we can multiply x with the
5
x term
in
3
(1 ) x
, as well as multiply
5
3x with the x term in
3
(1 ) x
. By the extended binomial theorem,
the coefficient of
5
x and x in
3
(1 ) x
are
3
5
C
and
3
1
C
so that the
generating function becomes
2 3 4
5 2 10 25 50 100
1
( ) (1 )
(1 ) (1 )(1 )(1 )(1 )
G x x x x x
x x x x x
(
= + + + +
(
.
Now all powers of x in the second factor are divisible by 5, so the only way to form an
100
x term is
to multiply the term 1 in the first factor with an
100
x term in the second factor. Setting
5
y x = , we
therefore have to compute the coefficient of
20
y in the expansion of
Mathematical Database
Page 12 of 17
2 2 5 10 20
1
(1 ) (1 )(1 )(1 )(1 ) y y y y y
.
In the denominator, all powers of y are divisors of 20. Using the same technique as we replaced
1
1 x
by
2 3 4
5
1
1
x x x x
x
+ + + +
.
Finally, to extract the coefficient of
20
y , we note that
20 6 20
(1 ) 1 6 y y
= + + (here
6
1
6 C
= ) and
so we need only find the coefficient of
20
y and the constant term in the numerator. The constant
term is clearly 1. The coefficient of
20
y is, unfortunately, complicated to compute. Nevertheless one
can consider the last two factors first and deal with the terms case by case. For instance, if the term
5
y is taken in the second last factor while the term
10
y is taken in the last factor, there are 12 ways
to get
5
y from the first 3 factors ( 0 5 0 + + , 1 4 0 + + , 2 3 0 + + , 3 2 0 + + , 4 1 0 + + , 5 0 0 + + ,
0 3 2 + + , 1 2 2 + + , 2 1 2 + + , 3 0 2 + + , 0 1 4 + + and 1 0 4 + + ). We shall omit the remaining details,
but anyway it can be found that the coefficient of
20
y in the numerator is 287. It follows that the
answer is 287 6 293 + = .
C. Solving Recurrence Relations
Given a recurrence relation, one of the common ways to solve for the general term of a
sequence is to use the method of characteristic equations. Here we will see how generating
functions may be employed as an alternative way in solving recurrence relations.
For each sequence, we can form a generating function which may be regarded as an infinite
polynomial. If a recurrence relation is given, we can possibly reduce this infinite polynomial to a
finite one, so that we can get the generating function in closed form. Consequently the coefficient of
a general term (i.e. the general term of the sequence) can be found. The idea is more or less the
same as that employed in deriving the sum of a geometric series.
Example 3.6.
Using generating functions, find
n
a in terms of n in each of the following cases.
(a)
0
2 a = and
1
3
n n
a a
+
= for 0 n
(b)
0
1 a = ,
1
2 a = and
2 1
5 4
n n n
a a a
+ +
= for 0 n
Mathematical Database
Page 13 of 17
Solution.
In each case, we let ( ) G x be the generating function for the given sequence { }
n
a .
(a) We have
2
0 1 2
2
0 1
2
0 1 0 2 1
( ) (1)
(1) 3 : 3 ( ) 3 3 (2)
(1) (2) : (1 3 ) ( ) ( 3 ) ( 3 )
G x a a x a x
x xG x a x a x
x G x a a a x a a x
= + + +
= + +
= + + +
Since
0
2 a = and
1
3
n n
a a
+
= for 0 n , we have (1 3 ) ( ) 2 x G x = , i.e.
2 3
2
( )
1 3
2 1 (3 ) (3 ) (3 )
G x
x
x x x
=
( = + + + +
In this way, we see that the coefficient of
n
x in ( ) G x is 2 3
n
, so that 2 3
n
n
a = for all n, as
we would expect since { }
n
a is in fact a geometric sequence with first term 2 and common ratio
3.
(b) Similar to (a), we have
2 3
0 1 2 3
2 3
0 1 2
2 2 2
0 1
( )
5 ( ) 5 5 5
4 ( ) 4 4
G x a a x a x a x
xG x a x a x a x
x G x a x a x
= + + + +
=
= + + +
Adding these three equations, and using the initial conditions as well as the given recurrence
relation, we get
2
(1 5 4 ) ( ) 1 3 x x G x x + = .
Applying partial fraction decomposition, we have
2
2 3 2 3
1 3
( )
1 5 4
2 1 1 1
3 1 3 1 4
2 1
1 1 (4 ) (4 ) (4 )
3 3
x
G x
x x
x x
x x x x x x
=
+
| | | |
= +
| |
\ . \ .
( ( = + + + + + + + + +
Thus the coefficient of
n
x in ( ) G x is
2 1
4
3 3
n
+ , so that
4 2
3
n
n
a
+
= .
Mathematical Database
Page 14 of 17
4. Further Examples
In this section we shall look at a couple of problems from mathematical olympiads in which
generating functions serve as a powerful tool.
Example 4.1.
(CGMO 2004) A set of poker has 32 cards. 30 of them are in red, yellow and blue, with 10 cards in
each colour, given the numbers 1, 2, , 10 respectively. 2 of them are different jokers, both with
the number 0. A card with the number k counts for 2
k
points. Call a group of cards a good group
if the sum of their points is 2004. Calculate the number of good groups.
Solution.
The generating function is given by
2 2 3 4 3 1024 3
3
2 2 2 4 1024
2 3
2 2048 3
2 3
2 2048 4096 6144 2 3
2 2048 2049 2 3
( ) (1 ) (1 ) (1 ) (1 )
(1 ) (1 )(1 )(1 ) (1 )
(1 )
(1 ) (1 )
(1 )
(1 2 )(1 3 3 )(1 )
(1 2 3 6 )(1 )
G x x x x x
x x x x x
x
x x
x
x x x x x x
x x x x x
= + + + +
( + + + +
=
+
=
= + + +
= + +
The answer is the coefficient of
2004
x in ( ) G x . Note that in the expansion of
2 3
(1 ) x
, there are
only even powers of x. To form an
2004
x term, one can multiply the 1 in the first factor with the
2004
x term in
2 3
(1 ) x
, as well as multiply the
2
x term in the first factor with the
2002
x term in
2 3
(1 ) x
. By the extended binomial theorem, the coefficients of
2004
x and
2002
x in
2 3
(1 ) x
are
3
1002
C
and
3
1001
C
by two different methods. We shall give the outline below and the details are left as an exercise.
Method 1
(1) Establish the fact that
if | or 1
0 otherwise
n
z E
p p n z
z
=
=
.
(2) Replace ( , ) G t x by
,
( , )
k m
k m
a k m t x
in S.
(3) Show that
2
|
|
( , )
p m
p k
S p a k m =
by first summing x and then t.
Method 2
(1) Replace ( , ) G t x by
2 2
( )( ) ( )
p
t x t x t x + + + in S.
(2) First fix t and sum x, treating the cases 1 x = and 1 x separately.
(3) Then sum t to show that
( )
2
1
4 2
p
p
S C p
p
= + .
Now we can equate the results of the two methods to get
( )
2
|
|
1
( , ) 4 2
p
p
p m
p k
a k m C p
p
= +
.
Bearing in mind that (0, 0) 1 a = and
2
(2 , 2 ) 1 a p p p = (i.e. coefficients corresponding to the empty
set and the whole set of 2p integers respectively) are included in the left-hand summation but are
not wanted in our final answer, we subtract 2 from the right hand side to get the answer
( )
2
1
2 2
p
p
C p
p
+ .
Mathematical Database
Page 16 of 17
5. Exercises
1. Find the generating functions of the following sequences in closed form.
(a) 1, 0, 1, 0, 1, 0,
(b) 2, 4, 6, 8, 10, 12,
(c)
0
2
C ,
1
2
C ,
2
2
C ,
3
2
C ,
4
2
C ,
5
2
C ,
2. Find the coefficient of
2005
x in each of the following generating functions.
(a)
2006
(1 2 ) x
(b)
4
1
1 3x
| |
|
+
\ .
(c)
2
1
2 5 2 x x + +
(d) | | ln 2(1 ) x +
3. Using generating functions, find
n
a in terms of n in each of the following cases.
(a)
0
2 a = and
1
3 1
n n
a a
+
= + for 0 n
(a)
0
1 a = ,
1
2 a = and
2 1
4 3
n n n
a a a
+ +
= for 0 n
(a)
0 1
1 a a = = and
2 1 n n n
a a a
+ +
= + for 0 n
4. Find the number of solutions to the equation 50 a b c d + + + = if each variable is
(a) a non-negative integer
(b) a positive integer
(c) an odd positive integer
(d) an integer between 4 and 10 (inclusive)
5. Prove the formula
1
( 1)
n r n r
r r
C C
+
= for positive integers n and r.
6. Solve Example 3.3 without using generating functions and compare the two methods.
Mathematical Database
Page 17 of 17
7. Complete Example 3.5 by filling in the missing details in the last paragraph of the solution.
8. Complete the solution to Example 4.2 by following the steps outlined.
9. There are 10000 identical red balls, 10000 identical yellow balls and 10000 identical green
balls. In how many different ways can we select 2005 balls so that the number of red balls is
even or the number of yellow balls is odd?
10. Amy and Betty each picks n different positive integers, and Amys numbers are not all the
same as Bettys numbers. For each set of n numbers, there are
2
n
H ways to add up two of them
(adding a number with itself is allowed) and hence
2
n
H sums can be obtained (some of them
may be repeated). It turns out that the
2
n
H sums obtained by each of Amy and Betty are
exactly the same (same sums to the same multiplicities). Show that n is a power of 2.
11. For positive integer 1 n > , let
n
C be the number of ways of dissecting a regular ( 2 n + )-sided
polygon into triangles by 1 n diagonals which do not intersect inside the polygon. For
convenience we also define
0 1
1 C C = = .
(a) Compute
2
C ,
3
C and
4
C .
(b) Show that for 1 n > ,
n
C satisfies the recurrence relation
0 1 1 2 2 1 1 0 n n n n n
C C C C C C C C C
= + + + + .
(c) Show that the generating function for the sequence { }
n
C is given by
1 1 4
( )
2
x
G x
x
= .
(d) Show that
2
1
1
n
n n
C C
n
=
+
.
Remark. The numbers
n
C are known as Catalan numbers. In Richard Stanleys book
Enumerative Combinatorics, he listed 66 different combinatorial interpretations of the Catalan
numbers!