The ordinary generating function (OGF) of a sequence a is defined as the formal power series:
F(x)=n∑anxn
a may be either a finite sequence or an infinite sequence. Common examples (assuming that a starts at 0):
The ordinary generating function of the sequence a=⟨1,2,3⟩ is 1+2x+3x2.
The ordinary generating function of the sequence a=⟨1,1,1,⋯⟩ is ∑n≥0xn.
The generating function of the sequence a=⟨1,2,4,8,16,⋯⟩ is ∑n≥02nxn.
The generating function of the sequence a=⟨1,3,5,7,9,⋯⟩ is ∑n≥0(2n+1)xn.
In other words, if sequence a has a formula for its general term, then the coefficients of its ordinary generating function are given by that general-term formula.
Basic Operations
Consider the ordinary generating functions of two sequences a,b, denoted by F(x),G(x), respectively. Then
F(x)±G(x)=n∑(an±bn)xn
Therefore, F(x)±G(x) is the ordinary generating function of the sequence ⟨an±bn⟩.
Consider multiplication, that is, convolution:
F(x)G(x)=n∑xni=0∑naibn−i
Therefore, F(x)G(x) is the ordinary generating function of the sequence ⟨∑i=0naibn−i⟩.
Closed Forms
When using generating functions, we do not always work in the form of formal power series. Instead, we convert them to closed forms when appropriate to simplify the calculations.
For example, for ⟨1,1,1,⋯⟩, whose ordinary generating function is F(x)=∑n≥0xn, we can observe that
F(x)x+1=F(x)
Solving this equation gives
F(x)=1−x1
This is the closed form of ∑n≥0xn.
Consider the geometric sequence ⟨1,p,p2,p3,p4,⋯⟩ and its generating function F(x)=∑n≥0pnxn. We have
F(x)px+1F(x)=F(x)=1−px1
The closed form and expanded form of a geometric sequence are commonly used as transformation tools.
Small Exercises
Find the ordinary generating functions of the following sequences (in both formal power series form and closed form). The difficulty increases gradually.
Next, we derive the generating function of the Fibonacci sequence.
The Fibonacci sequence is defined by a0=0,a1=1,an=an−1+an−2(n>1). Let its ordinary generating function be F(x). Then, based on its recurrence, we can similarly write an equation for F(x):
F(x)=xF(x)+x2F(x)−a0x+a1x+a0
Solving it gives
F(x)=1−x−x2x
The next question is: how do we find its expanded form?
Expansion Method One
We may treat x+x2 as a single unit. Then we obtain
In the last step, let n=k+i+1 and change the order of summation. Thus, we obtain the formula for the general term:
an=i=0∑⌊(n−1)/2⌋(in−i−1).
This is not the familiar form involving the golden ratio.
Expansion Method Two
Consider solving an equation with undetermined coefficients:
1−axA+1−bxB=1−x−x2x
Putting the fractions over a common denominator gives
(1−ax)(1−bx)A−Abx+B−aBx=1−x−x2x
Equating the coefficients of the undetermined terms, we obtain
⎩⎨⎧A+B=0−Ab−aB=1a+b=1ab=−1
Solving gives
⎩⎨⎧A=51B=−51a=21+5b=21−5
Then, using the expansion of a geometric sequence, we obtain the formula for the general term of the Fibonacci sequence:
1−x−x2x=n≥0∑xn51((21+5)n−(21−5)n)
This is also known as another closed form of the Fibonacci sequence (with 1−x−x2x being a closed form).
For any polynomials P(x),Q(x), the expansion of the generating function Q(x)P(x) can be found using the method above. In practice, we usually first find the roots of Q(x) and express the denominator in the form ∏(1−pix)di, then determine the numerator terms.
When factoring the denominator, if there are repeated roots, each repeated root requires one more fraction. For example, consider the generating function
G(x)=(1−x)(1−2x)21
For the formula for the general term of its coefficients, we have
G(x)=1−xc0+1−2xc1+(1−2x)2c2
Solving gives
⎩⎨⎧c0c1c2=1=−2=2
Therefore
[xn]G(x)=1−2n+1+(n+1)⋅2n+1
Newton's Binomial Theorem
We redefine the operation of taking binomial coefficients:
(kr)=k!rk(r∈C,k∈N)
Note that the range of r is the complex field. In this case, for α∈C, we have
(1+x)α=n≥0∑(nα)xn
The binomial theorem is actually a special case of Newton's binomial theorem.
Next, we give some example problems to introduce concrete applications of generating functions in OI.
Food
Food
Choose n items from several different kinds of food, subject to the following restrictions for each kind:
Chengde hamburgers: an even number
Cola: 0 or 1
Chicken legs: 0, 1, or 2
Peach Duo: an odd number
Chicken nuggets: a multiple of 4
Buns: 0, 1, 2, or 3
Stir-fried pork with potato slices: at most one.
Bread: a multiple of 3
Each kind of food is counted in units of “items”; a selection is considered one way whenever the total number of items is n. For a given n, calculate the number of ways modulo 10007.
This is a classic generating-function problem. For one kind of food, let an denote the number of ways to choose n items of that kind, and find its generating function. The generating function for the number of ways to choose a total of n items from two kinds of food is the convolution of their generating functions. For multiple kinds of food, the generating function for the number of ways to choose a total of n items is likewise the convolution of their generating functions.
Once we understand that the number of ways can be represented by convolution, we can construct the generating functions (the labels correspond to the food labels in the problem):
n≥0∑x2n=1−x21.
1+x.
1+x+x2=1−x1−x3.
1−x2x.
n≥0∑x4n=1−x41.
1+x+x2+x3=1−x1−x4.
1+x.
1−x31.
Multiplying them all together gives the generating function for the answer:
Then convert it into expanded form (using the fifth exercise from the closed-form exercises):
F(x)=n≥1∑(n−1n+2)xn
Therefore, the answer is (n−1n+2)=(3n+2).
Sweet
「CEOI2004」Sweet
There are n piles of candy. The candies in different piles are of different kinds (that is, all candies in the same pile are of the same kind, and candies in different piles are of different kinds). The i-th pile contains mi candies. You must eat at least a candies but no more than b candies. Find the number of ways to do so.
Two ways are different if and only if the numbers of candies eaten differ, or the number of candies of some kind among the candies eaten differs between the two ways.
n≤10,0≤a≤b≤107,mi≤106.
For pile i, the generating function for the number of ways to eat j candies (which is clearly 1) is
Fi(x)=j=0∑mixj=1−x1−xmi+1
Therefore, the generating function for the number of ways to eat a total of i candies is
G(x)=i=1∏nFi(x)=(1−x)−ni=1∏n(1−xmi+1)
What we need is ∑i=ab[xi]G(x).
Since n≤10, we can expand ∏i=1n(1−xmi+1) by brute force (there are at most 2n terms).
Then apply Newton's binomial theorem to (1−x)−n:
(1−x)−n=i≥0∑(i−n)(−x)i=i≥0∑(in−1+i)xi
In ∏i=1n(1−xmi+1), enumerate the coefficient of the xk term and suppose it is ck. After multiplying it by (1−x)−n, its contribution to the answer is