Section8.2Another look at distributing apples or folders¶ permalink
A recurring problem so far in this book has been to consider problems that ask about distributing indistinguishable objects (say apples) to distinct entities (say children). We started in Chapter 2 by asking how many ways there were to distribute \(40\) apples to \(5\) children so that each child is guaranteed to get at least one apple and saw that the answer was \(C(39,4)\text{.}\) We even saw how to restrict the situation so that one of the children was limited and could receive at most \(10\) apples. In Chapter 7, we learned how to extend the restrictions so that more than one child had restrictions on the number of apples allowed by taking advantage of the Principle of Inclusion-Exclusion. Before moving on to see how generating functions can allow us to get even more creative with our restrictions, let's take a moment to see how generating functions would allow us to solve the most basic problem at hand.
Example8.4
We already know that the number of ways to distribute \(n\) apples to \(5\) children so that each child gets at least one apple is \(C(n-1,4)\text{,}\) but it will be instructive to see how we can derive this result using generating functions. Let's start with an even simpler problem: how many ways are there to distribute \(n\) apples to one child so that each child receives at least one apple? Well, this isn't too hard, there's only one way to do it—give all the apples to the lucky kid! Thus the sequence that enumerates the number of ways to do this is \(\{a_n\colon n\geq 1\}\) with \(a_n=1\) for all \(n\geq 1\text{.}\) Then the generating function for this sequence is
\begin{equation*}
x+x^2+x^3+\cdots = x(1+x+x^2+x^3+\cdots) = \frac{x}{1-x}.
\end{equation*}
How can we get from this fact to the question of five children? Notice what happens when we multiply
\begin{equation*}
(x+x^2+\cdots)(x+x^2+\cdots)(x+x^2+\cdots)(x+x^2+\cdots)
(x+x^2+\cdots).
\end{equation*}
To see what this product represents, first consider how many ways can we get an \(x^6\text{?}\) We could use the \(x^2\) from the first factor and \(x\) from each of the other four, or \(x^2\) from the second factor and \(x\) from each of the other four, etc., meaning that the coefficient on \(x^6\) is \(5 = C(5,4)\text{.}\) More generally, what's the coefficient on \(x^n\) in the product? In the expansion, we get an \(x^n\) for every product of the form \(x^{k_1}x^{k_2}x^{k_3}x^{k_4}x^{k_5}\) where \(k_1+k_2+k_3+k_4+k_5 = n\text{.}\) Returning to the general question here, we're really dealing with distributing \(n\) apples to \(5\) children, and since \(k_i> 0\) for \(i=1,2,\dots,5\text{,}\) we also have the guarantee that each child receives at least one apple, so the product of the generating function for one child gives the generating function for five children.
Let's pretend for a minute that we didn't know that the coefficients must be \(C(n-1,4)\text{.}\) How could we figure out the coefficients just from the generating function? The generating function we're interested in is \(x^5/(1-x)^5\text{,}\) which you should be able to pretty quickly see satisfies
\begin{align*}
\frac{x^5}{(1-x)^5} \amp =
\frac{x^5}{4!}\frac{d^4}{dx^4}\left(\frac{1}{1-x}\right) =
\frac{x^5}{4!}\sum_{n=0}^\infty n(n-1)(n-2)(n-3)x^{n-4}\\
\amp =\sum_{n=0}^\infty \frac{n(n-1)(n-2)(n-3)}{4!}x^{n+1} =
\sum_{n=0}^\infty \binom{n}{4}x^{n+1}.
\end{align*}
The coefficient on \(x^n\) in this series \(C(n-1,4)\text{,}\) just as we expected.
We could revisit an example from Chapter 7 to see that if we wanted to limit a child to receive at most \(4\) apples, we would use \((x+x^2+x^3+x^4)\) as its generating function instead of \(x/(1-x)\text{,}\) but rather than belabor that here, let's try something a bit more exotic.
Example8.5
A grocery store is preparing holiday fruit baskets for sale. Each fruit basket will have \(20\) pieces of fruit in it, chosen from apples, pears, oranges, and grapefruit. How many different ways can such a basket be prepared if there must be at least one apple in a basket, a basket cannot contain more than three pears, and the number of oranges must be a multiple of four?
SolutionIn order to get at the number of baskets consisting of \(20\) pieces of fruit, let's solve the more general problem where each basket has \(n\) pieces of fruit. Our method is simple: find the generating function for how to do this with each type of fruit individually and then multiply them. As in the previous example, the product will contain the term \(x^n\) for every way of assembling a basket of \(n\) pieces of fruit subject to our restrictions. The apple generating function is \(x/(1-x)\text{,}\) since we only want positive powers of \(x\) (corresponding to ensuring at least one apple). The generating function for pears is \((1+x+x^2+x^3)\text{,}\) since we can have only zero, one, two, or three pears in basket. For oranges we have \(1/(1-x^4) = 1+x^4+x^8+\cdots\text{,}\) and the unrestricted grapefruit give us a factor of \(1/(1-x)\text{.}\) Multiplying, we have
\begin{equation*}
\frac{x}{1-x} (1+x+x^2+x^3) \frac{1}{1-x^4} \frac{1}{1-x} =
\frac{x}{(1-x)^2(1-x^4)} (1+x+x^2+x^3).
\end{equation*}
Now we want to make use of the fact that \((1+x+x^2+x^3) =(1-x^4)/(1-x)\) to see that our generating function is
\begin{equation*}
\frac{x}{(1-x)^3} = \frac{x}{2}\sum_{n=0}^\infty n(n-1)x^{n-2} =
\sum_{n=0}^\infty\frac{n(n-1)}{2} x^{n-1} =
\sum_{n=0}^\infty\binom{n}{2} x^{n-1} =
\sum_{n=0}^\infty\binom{n+1}{2} x^n.
\end{equation*}
Thus, there are \(C(n+1,2)\) possible fruit baskets containing \(n\) pieces of fruit, meaning that the answer to the question we originally asked is \(C(21,2) = 210\text{.}\)
Example8.6
Find the number of integer solutions to the equation
\begin{equation*}
x_1 + x_2 + x_3 = n
\end{equation*}
(\(n\geq 0\) an integer) with \(x_1 \geq 0\) even, \(x_2\geq 0\text{,}\) and \(0\leq x_3\leq 2\text{.}\)
SolutionAgain, we want to look at the generating function we would have if each variable existed individually and take their product. For \(x_1\text{,}\) we get a factor of \(1/(1-x^2)\text{;}\) for \(x_2\text{,}\) we have \(1/(1-x)\text{;}\) and for \(x_3\) our factor is \((1+x+x^2)\text{.}\) Therefore, the generating function for the number of solutions to the equation above is
\begin{equation*}
\frac{1+x+x^2}{(1-x)(1-x^2)} = \frac{1+x+x^2}{(1+x)(1-x)^2}.
\end{equation*}
In calculus, when we wanted to integrate a rational function of this form, we would use the method of partial fractions to write it as a sum of “simpler” rational functions whose antiderivatives we recognized. Here, our technique is the same, as we can readily recognize the formal power series for many rational functions. Our goal is to write
\begin{equation*}
\frac{1+x+x^2}{(1+x)(1-x)^2} = \frac{A}{1+x} + \frac{B}{1-x} +
\frac{C}{(1-x)^2}
\end{equation*}
for appropriate constants, \(A\text{,}\) \(B\text{,}\) and \(C\text{.}\) To find the constants, we clear the denominators, giving
\begin{equation*}
1+x+x^2 = A(1-x)^2 + B(1-x^2) + C(1+x).
\end{equation*}
Equating coefficients on terms of equal degree, we have:
\begin{align*}
1 \amp = A+B+C\\
1 \amp = -2A + C\\
1 \amp = A - B
\end{align*}
Solving the system, we find \(A=1/4\text{,}\) \(B=-3/4\text{,}\) and \(C=3/2\text{.}\) Therefore, our generating function is
\begin{equation*}
\frac{1}{4}\frac{1}{1+x} -\frac{3}{4} \frac{1}{1-x} +\frac{3}{2}
\frac{1}{(1-x)^2} = \frac{1}{4}\sum_{n=0}^\infty (-1)^n x^n -
\frac{3}{4} \sum_{n=0}^\infty x^n + \frac{3}{2}\sum_{n=0}^\infty n
x^{n-1}.
\end{equation*}
The solution to our question is thus the coefficient on \(x^n\) in the above generating function, which is
\begin{equation*}
\frac{(-1)^n}{4} - \frac{3}{4} + \frac{3(n+1)}{2},
\end{equation*}
a surprising answer that would not be too easy to come up with via other methods!