Combinatorics – Number of Solutions for Sum of Variables with Constraints

balls-in-binscombinatoricsinclusion-exclusion

Let $n,k,r$ be positive integers. The number of all nonnegative solutions of the Diophantine Equation $x_1+x_2+\dots+x_k=n$ is $\binom{n+k-1}{n}$. Is there a general formula for the number of solutions of the equation $x_1+x_2+\dots+x_k=n$ with $x_i\le r$ for every $i\in \{1,2,\dots,k\}$?

If one defines $A_i$ to be the number of solutions with $x_i>r$ then the answer will be $\binom{n+k-1}{n}-|A_1\cup\dots\cup A_k|$. I think it can give a complicated formula. What is the formula?

Best Answer

This sort of problem can be solved using inclusion-exclusion. For your problem, this leads to

$$ \sum_{t=0}^k(-1)^t\binom kt\binom{n-t(r+1)+k-1}{k-1}\;, $$

where, contrary to convention, the binomial coefficient stands for $0$ if the upper index is less than the lower index, and where $t$ counts the number of variables for which the constraint is violated.

For a derivation, including the case of different constraints $x_i\le r_i$, see Balls in Bins with Limited Capacity.

Related Question