And now for some solutions to the cookie distribution problem. I’m actually going to describe four different methods of solution, and thereby (re)discover some nice combinatorial identities along the way. This is what I love about combinatorics—you discover all this rich structure just by coming up with different ways to count things!

### Solution 1: Cookies and Fences

The first solution, as described in a comment by Mike, consists in considering permutations of cookies and fences. If we set up the cookies in a row, we can use four fences to separate them into five sections; the first student will get the first section of cookies, the second student will get the second section, and so on. In the example above, student A gets one cookie, student B gets two, student C doesn’t get any, and students D and E get three and four, respectively. It’s easy to see that every distribution of cookies corresponds to an arrangement of fences, and vice versa. So, how many ways are there to arrange the fences? Think of it this way: we have fourteen “slots”, and into each slot we can put either a cookie or a fence. Choosing a distribution is just choosing in which of the fourteen slots to put the four fences, and there are $\binom{14}{4} = 1001$ ways to choose four out of fourteen slots. (If you don’t know about binomial coefficients, you can read about them here.) Incidentally, this is also the number of ways to distribute four fences to eleven students; just use the cookies to divide the fences into eleven sections (most of which will, of course, be empty). Although I have no idea why students would want fences.

This can be easily generalized: the above argument shows that there are $\binom{c + s - 1}{s - 1}$ ways to distribute c cookies to s students.

So, the problem is solved, right? What more can there be to say about it?

…plenty! This is a nice elegant solution, but it takes some cleverness (and/or luck, and/or experience with this sort of problem) to see it, and in fact it isn’t the first solution I came up with. Looking at some different methods of solution will also illuminate some of the rich structure of binomial coefficients.

### Solution 2: Don’t Be So Mean

It seems mean that sometimes some students don’t get any cookies at all. Wouldn’t it be much nicer if every student got at least one cookie (even if they still might be distributed unevenly)? The key insight, as pointed out by Jonathan, is this: the number of ways to distribute ten cookies to five students is the same as the number of ways to distribute fifteen cookies to five students in such a way that each student gets at least one cookie. The above illustration shows a distribution in which students A, B, C, D, and E get two, three, one, four, and five cookies respectively. Note that this is the distribution of fifteen cookies that corresponds to the distribution of ten cookies illustrated with the fences previously.

So, the number of ways to distribute fifteen cookies to five students so that each gets at least one is $\binom{14}{4}$; more generally, the number of ways to distribute c cookies to s students so each gets at least one is $\binom{c-1}{s-1}$. Also, the total number of ways to distribute c cookies to s students is the same as the number of ways to distribute c+s cookies to s students so that each gets at least one.

### Solution 3: One at a time, please!

Another way of approaching the problem comes courtesy of meichenl. To distribute cookies to s students, we first have to give some number of cookies to the first student; we can give her anywhere from zero to ten cookies. If we give her k cookies, there are c – k cookies left to distribute to the other s – 1 students. If we let $D(c,s)$ stand for the number of ways of Distributing c cookies to s students, we can write this observation as $\displaystyle D(c,s) = \sum_{k=0}^c D(c - k, s - 1).$

That is, we split the distribution up into cases based on how many cookies we give to the first student; in each case we’ve reduced the problem to a simpler distribution problem. Adding everything up, we get the above recurrence—we’ve defined $D(c,s)$ recursively, that is, in terms of itself. This definition actually gets off the ground because we can identify some base cases, that is, simple situations in which we know the answer without breaking things down further. In particular, $D(0,s) = 1$ for any s (there’s only one way to distribute zero cookies—namely, to give zero cookies to each student), and $D(c,1) = 1$ for any c (there’s only one way to distribute cookies to a single lucky student!).

So, once we have this recurrence, what do we do with it? We can use it for direct calculation, as Mark James apparently did; he also used the recurrence in a computer program to print all the distributions. As observed by Daniel Klein, this sort of recurrence is also perfectly suited for a programming technique called dynamic programming; it’s easy to turn this recurrence into a computer program to compute $D(c,s)$ for any values of c and s (although in this particular case, as we’ve seen, there are more efficient ways to compute $D(c,s)$).

But here’s another interesting thing: we already know what $D(c,s)$ is, from solutions 1 and 2: it’s $\binom{c+s-1}{s-1}.$ So if we substitute this into the above recurrence, we get $\displaystyle \binom{c+s-1}{s-1} = \sum_{k=0}^c \binom{c - k + s - 2}{s - 2}$

If we substitute d + 1 in place of s – 1 everywhere, we can make this a little nicer without changing anything (d is just a new variable I made up, it doesn’t stand for anything in particular): $\displaystyle \binom{c+d+1}{d+1} = \sum_{k=0}^c \binom{c + d - k}{d}$

We’ve discovered an identity involving sums of binomial coefficients! Neat! Now, you might think, “OK, let’s prove this by induction.” But we need do no such thing. We have already argued that the expression on the left and the expression on the right count the same things—so they must be equal! This is the epitome of a combinatorial proof: to show that two things are equal, just show that they represent two different ways of counting the same thing.

### Solution 4: How mean do you want to be?

The last solution (at least, the last solution I will discuss in this post!) comes courtesy of Dave. Instead of breaking the distribution into cases according to how many cookies we give to the first student (as in the previous solution), we can break it into cases according to how many students get any cookies: we can give all the cookies to one student, or give them all to two students, or three, and so on.

The first case is easy: if we give all the cookies to one student, there are 5 ways to choose which student to give the cookies to, and only one way to give them the cookies. What if we give all the cookies to two students? Well, there are $\binom{5}{2}$ ways to choose which two students get cookies. Now we want to distribute the ten cookies to these two students in such a way that each student gets at least one cookie (otherwise we are back in the first case). As previously noted in solution 2, there are $\binom{9}{1}$ ways to do this. If three students get cookies, there are $\binom{5}{3}$ ways to choose the three students, and $\binom{9}{2}$ ways to distribute the cookies, and so on. Generalizing, we see that $\displaystyle D(c,s) = \sum_{k=1}^s \binom{s}{k} \binom{c-1}{k-1}.$

Egads! We’ve discovered another combinatorial identity, this one involving a sum of products of binomial coefficients! $\displaystyle \binom{c+s-1}{s-1} = \sum_{k=1}^s \binom{s}{k} \binom{c-1}{k-1}.$

There’s yet another method of solution I’m aware of—the theory of combinatorial species and generating functions—but that can wait for another post, I think we’ve had quite enough combinatorial goodness for one day! Associate Professor of Computer Science at Hendrix College. Functional programmer, mathematician, teacher, pianist, follower of Jesus.
This entry was posted in counting, proof, solutions and tagged , , , , , . Bookmark the permalink.

### 4 Responses to Distributing cookies: solutions

1. Jonathan says:

So the approach is beautiful – collect multiple solutions, and then… show that they count the same thing.

Very nice wrap up, thank you.

2. Badal says:

The solution(s) cover a lot of territory, even though it is written for a general audience.
Thanks.