[Math] Distribute $N$ objects to $K$ boxes such that no box has more than $c$ objects in it

combinatorics

I'm trying to find a way to calculate a problems such as this: if you have $n$ objects and $k$ indistinguishable boxes, how do you put in $n$ objects such that each box has no more than $C$, where $C \le k$.

For example, I have $6$ objects that need to be placed in $4$ boxes, where no box can have more than $4$ objects in it. I tried $\displaystyle\binom{n+k-1} {k-1}$. I got $84$ ways. But that is too big. When I do it by hand, I get $74$.

Would like actual explanation.

Best Answer

Added: The answer below is based on the assumption that your calculation with $\binom{n+k-1}{k-1}$ was relevant. However, this is the case if and only if the it is the objects that are indistinguishable, and the boxes can be distinguished, which (I now note) contradicts your statement of the problem. If in fact the boxes are indistinguishable, then the problem is altogether different. If the objects are distinguishable, the answer will involve Stirling numbers of the second kind; if not, it will involve partitions of an integer and be even messier.


You start with the $84$ ways that you calculated, but then you have to subtract the distributions that violate the upper limit. This is a standard inclusion-exclusion argument; the actual result for essentially this problem can be found in this question and answer.

In more detail: A first approximation to the desired result is, as you tried, $\dbinom{n+k-1}{k-1}$. However, this counts all possible distributions, not just that have at most $C$ objects in each box, so it’s necessary to subtract the distributions that violate this limit. We’ll first count the distributions that violate it for the first box. Those all have at least $C+1$ objects in the first box, so we can count them by first putting $C+1$ objects in Box 1 and then distributing the remaining $n-C-1$ objects freely amongst the $k$ boxes. This can be done in $$\binom{n-C-1+k-1}{k-1}=\binom{n-C+k-2}{k-1}$$ ways. There are just as many distributions that violate the limit on the second box, just as many again that violate it on the third box, and so on through the $k$ boxes, so there are (to a first approximation) $$k\binom{n-C+k-2}{k-1}$$ distributions that violate it for at least one box. That leaves us with $$\binom{n+k-1}{k-1}-k\binom{n-C+k-2}{k-1}\tag{0}$$ acceptable distributions as a second approximation.

Unfortunately, we’ve now subtracted too much, if $n$ is large enough: a distribution that violates the limit for two boxes has been subtracted twice. Thus, we must add back in all distributions that violate the limit for two boxes. Pick a pair of boxes; how many distributions violate the limit for both boxes of that pair? Those are the distributions that have $C+1$ objects in each of the two boxes and the remaining $n-2C-2$ objects distributed arbitrarily amongst the $k$ boxes. There are $$\binom{n-2C-2+k-1}{k-1}=\binom{n-2C+k-3}{k-1}$$ such distributions. And there are $\binom{k}2$ pairs of boxes, so altogether we must add back in $$\binom{k}2\binom{n-2C+k-3}{k-1}$$ distributions to get a third approximation of $$\binom{n+k-1}{k-1}-k\binom{n-C+k-2}{k-1}+\binom{k}2\binom{n-2C+k-3}{k-1}$$ acceptable distributions.

This time we’ve potentially added too much: distributions that violate the limit for three boxes have been added back in more than once, so we have to subtract a correction. And then we’ll have to correct for having subtracted too much in the case of distributions that go over the limit for four boxes. And so on.

The correction for distributions violating the limit for $i$ boxes must be made for every one of the $\binom{k}i$ sets of $i$ boxes, so it will have a factor of $\binom{k}i$. As you can see from the pattern up to this point, it will be a positive correction if $i$ is even and a negative correction if $i$ is odd; this can be handled with a factor of $(-1)^i$. Finally, the number of distributions that go over the limit on a particular set of $i$ boxes is

$$\binom{n-iC-i+k-1}{k-1}=\binom{n-iC+k-(i+1)}{k-1}\;,$$

so the correction term is $$(-1)^i\binom{k}i\binom{n-iC+k-(i+1)}{k-1}\;.\tag{1}$$

The original approximation of $\dbinom{n+k-1}{k-1}$ is simply $(1)$ with $i=0$, so the final answer is $$\sum_{i\ge 0}(-1)^i\binom{k}i\binom{n-iC+k-(i+1)}{k-1}\;.$$ Of course all terms with $i>k$ are $0$, so this is a finite sum.

In general there isn’t a simple closed form. To get that, you have to know something about $C$ and $n$. For example, you can’t exceed the upper limit on more than one box if $n<2C+2$; if that’s the case, only one correction term is non-zero, and you can simplify the answer to $(0)$.