# Can I use “stars and bars” to count the number of ways to distribute $N$ balls into $K$ black and $M$ white boxes?

Given $K$ black boxes amd $M$ white boxes, and a total number of balls $k + m = N,$ can I use some modified "stars and bars" combinatorics to count the number of ways to distribute the balls into the boxes? Within each color, the boxes are indistinguishable.

EDIT: I am interested in the particular case when 1 << m << k if that helps.

• Are these black boxes distinguishable between themselves? Why bother pointing out the color of the boxes at all if so and just call it $K+M$ distinguishable boxes total and just remember in the back of your mind that some of them are black and others are white... Stars and bars in the first place is used for when the boxes are distinguishable and the balls are indistinguishable. You need to clarify what are distinguishable in your problem and what are not. – JMoravitz Nov 9 '17 at 0:37
• I assume from the problem statement that boxes of each color are indistinguishable. In which case you will need to compute some partition numbers, a rather more complicated procedure... – user7530 Nov 9 '17 at 0:40
• @user7530 You are right, boxes of the same color are indistinguishable. – Ruslan Muhamadiarov Nov 9 '17 at 0:49
• Yeah, stars-and-bars only works when the boxes are all distinguishable. Otherwise, where would you put the bars? – darij grinberg Nov 9 '17 at 6:40

Consider first the case of $K$ indistinguishable boxes and $N$ balls. Since only the multiset of ball counts, and not their order, matters, the number of ways of distributing the balls into the boxes is the number of partitions of $N$ into at most $K$ parts. Let's call this number $P_K(N)$; there is no closed-form formula for this number, but it can be computed either recursively (it's a standard "dynamic programming" problem in computer science) or using generating functions.
Now if you have two types of boxes, you need to count the number of ways of first partitioning the balls into those that will go into the white boxes, and those that go in the black boxes, and then, the number of ways of putting those balls into those boxes: $$\sum_{i=0}^N \left[P_K(i)P_{M}(N-i)\right].$$