Count the number of integer solutions to $x_1+x_2+\cdots+x_5=36$

Suppose you had no constraints on the $x_i$. Your problem is addressed by the "Stars and Bars" method (or, if you do not wish to count the order of your summands, the Partition Function).

Now, your first question can be reduced to the question with no constraints by asking:

What is the number of non-negative integer solutions to $x_1 + x_2 + x_4 + x_5 = 14$?

What I did was set $x_3 = 11$ and subtract 11 from the righthand side and remove $x_3$ from consideration, since we may as well regard $x_3$ as a constant.

For $x_1$ and $x_4$, subtract 4 and 7, respectively, from the righthand side. Now we care only that $x_1 \geq 0$ and $x_4 \geq 0$. In other words, we want the number of non-negative integer solutions to $x_1 + x_2 + x_4 + x_5 = 14$.

To take care of a constraint like $x_5 \leq 5$, first find the number of solutions with no constraints on $x_5$. Then, find the number of solutions with $x_5 \geq 6$. Subtract the latter from the former.


In all your case, essentially we want to find the number of natural number solutions for the following equation:

$$\displaystyle \sum_{i=1}^{n} a_i = N$$ where $a_i \in \mathbb{N}$

The method is as follows:

Consider $N$ sticks.

$| | | | | | | | ... | | |$

We want to do partition these $N$ sticks into $n$ parts.

This can be done if we draw $n-1$ long vertical lines in between these $N$ sticks.

The number of gaps between these $N$ sticks is $N-1$.

So the total number of ways of drawing these $n-1$ long vertical lines in between these $N$ sticks is $C(N-1,n-1)$.

So the number of natural number solutions for $\displaystyle \sum_{i=1}^{n} a_i = N$ is $C(N-1,n-1)$.

If we are interested in the number of non-negative integer solutions, all we need to do is replace $a_i = b_i - 1$ and count the number of natural number solutions for the resulting equation in $b_i$'s.

i.e. $\displaystyle \sum_{i=1}^{n} (b_i - 1) = N$ i.e. $\displaystyle \sum_{i=1}^{n} b_i = N + n$.

So the number of non-negative integer solutions to $\displaystyle \sum_{i=1}^{n} a_i = N$ is given by $C(N+n-1,n-1)$.