# Distinct ways to put $N$ balls in $M$ boxes such that there is no more than $K$ balls in each box? [duplicate]

The question is: in how many different ways can I put $$N$$ indistinguishable balls into $$M$$ distinguishable boxes such that each box contains no more than $$K$$ balls it it?

A more general problem: if $$K$$ is different for different boxes, $$K_i$$ for the box $$i$$, $$i=1,...,M$$.

I tried to find a way to use "stars and bars" method here, but didn't succeed. I would be grateful if anyone could explain how to solve such a task or provide a reference.

• I swear I’ve seen this question asked a few times on this site recently Jun 11, 2020 at 16:31
• Here I gave a thorough answer to the first problem. In this answer Marc van Leeuwen uses generating functions to deal with both problems. Jun 11, 2020 at 16:57

Let $$a_j$$ defines the number of elements in the $$j$$-th box, then you want to count the $$(a_1,\ldots ,a_M)\in (\mathbb N \cup \{0\}) ^M$$ such that $$a_j\leqslant K$$ for each $$j\in\{1,\ldots,M\}$$ and $$\sum_{j=1}^Ma_j=N$$. Then note that this number is just the coefficient of $$x^N$$ in the expansion of the polynomial $$(1+x+x^2+\ldots +x^K)^M$$, this is notated as $$[x^N](1+x+x^2+\ldots +x^K)^M$$.
For the general case it is just $$[x^N]\prod_{j=1}^M(1+x+x^2+\ldots +x^{K_j})$$
{"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 17, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}