18

The number of ways to put $n$ unlabeled balls in $k$ distinct bins is $$\binom{n+k-1}{k-1} .$$ Which makes sense to me, but what I can't figure out is how to modify this formula if each bucket has a max of $m$ balls.

EDIT: What I've tried:

I got to the generating function $$(1-x^{m+1})^k(1-x)^{-k}$$ which ends up giving me $$\sum_{r(m+1)+r_2=n} \binom{k}{r}(-1)^{r_2}\binom{k+r_2-1}{r_2}$$

But when programing this:

def distribute_max(total,buckets,mmax):
  ret = 0
  for r in xrange(total//(mmax+1)+1):
    r_2 = total - r*(mmax+1)
    ret += choose(buckets,r) * (-1)**r_2 * choose(buckets + r_2 - 1,r_2)
  return ret

I'm getting terribly wrong answers. Not sure which step I screwed up.

Dan
  • 587

3 Answers3

16

As a check I did it with an inclusion-exclusion argument, getting

$$\sum_{i=0}^{\left\lfloor\frac{n}{m+1}\right\rfloor}(-1)^i\binom{k}i\binom{n+k-1-i(m+1)}{k-1}\,.\tag{1}$$

The nature of the inclusion-exclusion argument is such that $\binom{k}i\binom{n+k-1-i(m+1)}{k-1}$ is the number of ways to distribute the $n$ balls so that at least $i$ of the $k$ bins have more than $m$ balls. In that case some $i$ bins contain altogether at least $i(m+1)$ balls, so clearly we must have $i(m+1)\le n$, i.e., $i\le\left\lfloor\frac{n}{m+1}\right\rfloor$ (since $i$ is an integer).

Clearly it is also the case that $i$ cannot exceed $k$, but we don’t take explicit note of that in the upper limit of the summation $(1)$, because any term with $i>k$ has a factor $\binom{k}i=0$ anyway. One could make this explicit by writing the upper limit as $\min\left\{k,\left\lfloor\frac{n}{m+1}\right\rfloor\right\}$, but it is not necessary to do so.

Setting $r=i$ and $r_2=n-i(m+1)$ to match your notation, I make this

$$\sum_{r=0}^{\left\lfloor\frac{n}{m+1}\right\rfloor}(-1)^r\binom{k}r\binom{k+r_2-1}{r_2}\,.$$

It appears that you’ve the wrong exponent on $-1$.

Brian M. Scott
  • 616,228
  • 2
    +1. Charalambides's Enumerative Combinatorics (Exercise 9.6, p. 360) gives the same expression. – Mike Spivey Dec 07 '11 at 23:39
  • 4
    @Brian What are the sum limits in your formula? The expression from book Enumerative Combinatorics (p. $360$) is $L(n,k,m)=\sum_{j=0}^k (-1)^j C_k^j C_{k+n-j(m+1)-1}^{k-1}$ according to your notation, where $C_k^j$ means the binomial coefficient. Unfortunately, $L(n,k,m)=0$ for any $n \leq k \cdot m$ (Mathematica' showed me that). Somewhere the mistake lives. Can you kindly help me with this problem? – Piotr Semenov Feb 03 '14 at 21:20
  • 3
  • 1
    It seems to me that in order to make this work without failing one should have $0\le k\le \frac n{m+1}$. – Miles Eagle Mar 06 '19 at 09:55
  • @MilesEagle I think you meant to say $0\leq r\leq \frac{n}{m+1}$. So the sum on $r$ goes from $0$ to the smaller of $k$ and $\lfloor \frac{n}{m+1}\rfloor$ – Quantum Mechanic Jan 18 '24 at 22:54
  • @BrianM.Scott thanks for the response, but that makes no sense. If you have three balls and want to place them in three bins that each can take one ball, why would there be zero combinations? Rather, ignore terms with $i>3/(1+1)$ and you just get $10-9=1$ combination. One can also see this by using the generating function approach and noting that $r_2$ cannot be negative – Quantum Mechanic Jan 19 '24 at 15:45
  • @QuantumMechanic: You are of course right, and I’ve deleted the comment. (For some reason I was thinking that $k=2$ by the time I got to the end of the computation, so that $0$ would have been correct with $m=1$ and $n-3$.) I’ll add a bit to the answer tonight or tomorrow to clarify the limits. – Brian M. Scott Jan 20 '24 at 01:59
  • @BrianM.Scott no problem, thanks for responding. Your answer helped me regardless – Quantum Mechanic Jan 20 '24 at 16:02
  • I know that the $\binom{k}{r}$ enforces $r\leq k$, but once you put one limit up I would have put the minimum of $k$ and $n/(m+1)$ as the limit... sorry for being so pedantic, it's correct regardless – Quantum Mechanic Jan 21 '24 at 22:46
  • 1
    @QuantumMechanic: I thought about it but decided that I preferred the simpler expression. I also like to reinforce the fact that $\binom{n}k$ makes sense for all non-negative integers $k$, since students often seem predisposed to assume that $k$ must satisfy $0\le k\le n$. – Brian M. Scott Jan 21 '24 at 23:06
  • 1
    @QuantumMechanic: I have, however, gone ahead and expanded the explanatory note. – Brian M. Scott Jan 21 '24 at 23:13
3

Let's denote the problem as $D(n,k,m)$, then when $mk-n \leq m$, the answer can be given as: $D(n,k,m)=\binom{km-n+k-1}{k-1}$, where $m \leq n$.

It can be easily verified using $D(5,2,3)=2$, or $D(6,3,3)=10$, etc..

Let me try to explain it in more details with my poor English (sorry for that).

Suppose we start from the state that all bins are filled with $m$ balls in each. Then the task for us is to eliminate $mk-n$ balls from these $k$ bins. To ditribute the $mk-n$ "elimination" into $k$ bins, we have $\binom{km-n+k-1}{k-1}$ different ways if $mk-n \leq m$, i.e. the number of elimination in each bin will not exceed $m$.

1

The number of ways to put $n$ balls in $k$ boxes with in each box a maximum of $m$ balls is the coefficient of $[q^n]$ in the $\text{QBinomial}(k+m,k,q)$.

For example, the number of different ways to put $n = 5$ balls in $k = 4$ boxes with in each box no more than $m = 3$ balls is given by
$$ \text{QBinomial}(7,4,q) = (q^6+q^5+q^4+q^3+q^2+q+1)(q^2-q+1)(q^4+q^3+q^2+q+1) = q^{12}+q^{11}+2q^{10}+3q^9+4q^8+4q^7+5q^6+4q^5+4q^4+3q^3+2q^2+q+1. $$ The coefficient of $q^5$ is $4$. Hence there are four ways. These four ways are $(2,3)$, $(1,1,3)$, $(1,2,2)$, $(1,1,1,2)$.

  • The boxes are distinct, so there are 40 ways for the n=5,k=4,m=3 example. In particular, 12 each for (2,3), (1,1,3) and (1,2,2), and 4 for (1,1,1,2). The code in the question, with the correction from the accepted answer, gives 40. – Evgeni Sergeev May 16 '17 at 04:50