Number of solutions to equation of varying size, with varying upper-bound range restrictions Given:
$x_1 + x_2 + ... + x_n = k$
where each $x_i$ can have some value between $0$ and $10$ (we might have $0\leq x_1\leq 7$ and we might have $0\leq x_2\leq 9$ and so on...)
How many nonnegative integer solutions are there to an equation like this, where each $x_i$ can take on any of the values in its range restrictions?
Is Mick A's answer applicable to this from the following? Number of solutions to equation, range restrictions per variable
If so, how would Mick A's answer be extended for equations of arbitrary lengths of the left-hand side? How could this be solved for programmatically?
EDIT: My thoughts are that we calculate $S$ and then subtract $S_1, S_2, ..., S_n$ from it, after which we add all intersections $S_1 \cap S_2, ...$ and then we subtract all "three-way intersections" $S_1 \cap S_2 \cap S_3, ... $ and then we add all the "four-way intersections" $S_1 \cap S_2 \cap S_3 \cap S_4,...$ and then we subtract all the "five-way intersections" etc...
Is this a correct approach?
 A: Added an Addendum-2, to make the answer more generic.  Addendum-2 covers the situation where the lower bound on each variable is $1$, rather than $0$.

Added a 2nd (explanatory) Inclusion-Exclusion link, near the start of my answer.

Although the information in this answer has been provided repeatedly in mathSE, I have never observed the information provided at this level of detail.  Therefore, I hope that the posted question is not deleted as a duplicate.  That way, all future questions in this area may specifically refer to this answer as a stand-alone reference.


Identify all solutions to $x_1 + \cdots + x_k = n$ subject to the following constraints:

*

*$x_1, \cdots, x_k$ are all non-negative integers.

*$c_1, \cdots, c_k$ are all fixed positive integers.

*$x_i \leq c_i ~: ~i \in \{1,2,\cdots, k\}.$

Yes, the approach in the linked article generalizes well.  To the best of my knowledge, there are two general approaches:

*

*Generating functions : which I know nothing about.


*Stars and Bars which is also discussed here combined with Inclusion-Exclusion.  See also this answer for an explanation of and justification for the Inclusion-Exclusion formula.
First, I will provide the basic Inclusion Exclusion framework.  Then, within this framework, I will apply Stars and Bars theory.

Let $A$ denote the set of all solutions to the equation $x_1 + \cdots + x_k = n ~: ~x_1, \cdots, x_k$ are all non-negative integers.
For $i \in \{1,2,\cdots, k\}$, let $A_i$ denote the subset of A, where $x_i$ is restricted to being $> c_i$.  That is, $A_i$ represents the specific subset where the upper bound on $x_i$ is violated.
For any finite set $S$, let $|S|$ denote the number of elements in the set $S$.
Then it is desired to enumerate $|A| - |A_1 \cup \cdots \cup A_k|.$
Let $T_0$ denote $|A|$.
For $j \in \{1,2, \cdots, k\}$ let $T_j$ denote
$\displaystyle \sum_{1 \leq i_1 < i_2 < \cdots < i_j \leq k} |A_{i_1} \cap  A_{i_2} \cap \cdots \cap A_{i_j}|.$
That is, $T_j$ represents the sum of $\binom{k}{j}$ terms.
Then, in accordance with Inclusion - Exclusion theory, the desired enumeration is
$$\sum_{i = 0}^k (-1)^iT_i.$$
This concludes the description of the Inclusion-Exclusion framework.
All that remains is to provide a systematic algorithm for enumerating 
$T_0, T_1, \cdots, T_k$.

Stars and Bars theory:
(1)
$T_0 = \binom{n + [k-1]}{k-1}.$
(2)
To enumerate $A_i$, you have to enumerate the number of solutions to $x_1 + \cdots + x_k = n ~: x_i > c_i$.
The standard approach is to set $y_i = x_i - (c_i + 1),$ with all of the rest of the variables $y_1, \cdots, y_k = x_1, \cdots, x_k,$ respectively.
Then, there is a one to one correspondence between the solutions that you are trying to enumerate and the solutions to
$y_1 + \cdots + y_i + \cdots + y_k = n - (c_i + 1) ~: ~$ each variable is restricted to the non-negative integers.
Here, $n - (c_i + 1) < 0$ implies that there are $0$ solutions.
Otherwise, per (1) above, the number of solutions is
$\displaystyle \binom{n - [c_i + 1] + [k-1]}{k-1}.$
As defined, $T_1 = \sum_{i = 1}^k |A_i|$, so now, the procedure for enumerating $T_1$ is clear.

(3)
Consider how to enumerate $T_2$ which denotes
$\displaystyle \sum_{1 \leq i_1 < i_2 \leq k} |A_{i_1} \cap A_{i_2}|.$
That is, $T_2$ denotes the summation of $\binom{k}{2}$ terms.
I will illustrate how to specifically enumerate $|A_1 \cap A_2|$, with the understanding that this same approach should also be used on the other 
$\displaystyle \left[ \binom{k}{2} - 1\right]$ terms.
The approach will be very similar to that used in (2) above.
Let $y_1 = x_1 - (c_1 + 1).$
Let $y_2 = x_2 - (c_2 + 1).$ 
Let $y_i = x_i ~: ~3 \leq i \leq k$.
Then, there is a one to one correspondence between $|A_1 \cap A_2|$ and the number of non-negative integer solutions to
$y_1 + y_2 + y_3 + \cdots + y_k = n - (c_1 + 1) - (c_2 + 1).$
Again, if $n - (c_1 + 1) - (c_2 + 1) < 0$, then the number of solutions $= 0$.
Otherwise, again per (1) above, the number of solutions is given by
$\displaystyle \binom{n - [c_1 + 1] - [c_2 + 1] + [k-1]}{k-1}.$

(4)
Now, consider how to enumerate $T_j ~: ~3 \leq j \leq k$ which denotes
$\displaystyle \sum_{1 \leq i_1 < i_2 < \cdots < i_j \leq k} |A_{i_1} \cap A_{i_2} \cap \cdots \cap A_{i_j}|.$
That is, $T_j$ denotes the summation of $\binom{k}{j}$ terms.
I will illustrate how to specifically enumerate $|A_1 \cap A_2 \cap \cdots \cap A_j|$, with the understanding that this same approach should also be used on the other 
$\displaystyle \left[ \binom{k}{j} - 1\right]$ terms, when $j < k$.
The approach will be very similar to that used in (3) above.
Let $y_1 = x_1 - (c_1 + 1).$
Let $y_2 = x_2 - (c_2 + 1).$ 
$\cdots$ 
Let $y_j = x_j - (c_j + 1).$ 
Let $y_i = x_i ~: ~j < i \leq k.$ 
Then, there is a one to one correspondence between $|A_1 \cap A_2 \cap \cdots \cap A_j|$ and the number of non-negative integer solutions to
$y_1 + y_2 + y_3 + \cdots + y_k = 
n - (c_1 + 1) - (c_2 + 1) - \cdots - (c_j + 1).$
Again, if $n - (c_1 + 1) - (c_2 + 1) - \cdots - (c_j + 1) < 0$, then the number of solutions $= 0$.
Otherwise, again per (1) above, the number of solutions is given by
$\displaystyle \binom{n - [c_1 + 1] - [c_2 + 1] - \cdots - (c_j + 1) + [k-1]}{k-1}.$
This completes the Stars and Bars theory.

Addendum
Shortcuts
Suppose, for example, that $c_1 = c_2 = \cdots = c_k$.
Then, to enumerate $T_j$, all that you have to do is enumerate 
$|A_1 \cap A_2 \cdots \cap A_j|$.
When $j < k$, the other $\displaystyle \left[\binom{k}{j} - 1\right]$ terms will be identical.
Therefore, $T_j$ will enumerate as
$\displaystyle \binom{k}{j} \times |A_1 \cap A_2 \cdots \cap A_j|$.
Further, if only some of the variables $c_1, c_2, \cdots, c_k$ are equal to each other, you can employ similar (but necessarily sophisticated) intuition to presume that some of the $\binom{k}{j}$ terms will have the same enumeration.
Also, in practice, you typically don't have to manually enumerate each of $T_1, T_2, \cdots, T_k.$
The following concept is based on the assumption (which may be false) that 
$(c_1 + 1) + (c_2 + 1) + \cdots + (c_k + 1) > n.$
Re-order the scalars $c_1, \cdots c_k$ in ascending order, as 
$m_1 \leq m_2 \leq \cdots \leq m_k.$
Find the smallest value of $j$ such that $(m_1 + 1) + \cdots + (m_j + 1) > n$.
Then $T_j, T_{j+1}, \cdots, T_k$ must all equal $0$.

Addendum-2
This section is being added to make the answer more generic.  This section is not on point re the originally posted problem, because the originally posted problem (presumably) intended that each variable have a lower bound of $0$.
Suppose that you are asked to identify the number of solutions to the following problem:

*

*$x_1 + x_2 + \cdots + x_k = n ~: ~n \in \Bbb{Z^+}.$


*$x_1, x_2, \cdots, x_k \in \Bbb{Z^+}.$


*For $~i \in \{1,2,\cdots,k\}, ~x_i \leq c_i ~: ~c_i \in \Bbb{Z^+}.$
Basic Stars and Bars theory assumes that the lower bound of (for example) $~x_1, x_2, \cdots, x_k~$ is $~0~$ rather than $~1.$
The easiest adjustment is to take the preliminary step of converting the problem into the desired form, through the change of variables
$$y_i = x_i - 1 ~: ~i \in \{1,2,\cdots, k\}.$$
Then, the revised problem, which will have the same number of solutions, will be:

*

*$y_1 + x_2 + \cdots + y_k = (n-k).$


*$y_1, y_2, \cdots, y_k \in \Bbb{Z_{\geq 0}}.$


*For $~i \in \{1,2,\cdots,k\}, ~y_i \leq (c_i - 1) ~: ~c_i \in \Bbb{Z^+}.$
A: Let upper bound of $x_1,x_2..,x_n$ be given as $i_n$, then the answer is the coefficient of $x^k$ in the product:
$$ S= \prod_{j=1}^n (1 + x+x^2...+x^{i_1})$$
Now, suppose one was doing the problem by hand , then we can use a trick to reduce the calculations required in the above step. Consider the product,
$$ S'= \prod_{j=1}^n( \frac{1}{1-x} -\sum_{p=i_1+1}^k x^p)$$
It can be observed that in the above, the coefficient of $x^k$ is that of the one in $S$. The above form is also easier to compute. For instance, consider this problem:

Number of ways we can draw 6 chocolates drawn from 15 chocolates out of which 4 are blue, 5 are red and 6 are green if chocolates of the same colour are not distinguishable?

We are looking for solution of the equation $x_1 + x_2 + x_3 = 15$ with $0 \leq x_1 \leq 4$, $0 \leq  x_2 \leq 5$ , $0 \leq x_3 \leq 6$. We have,
$$ S'=  (\frac{1}{1-x} -x^5 -x^6)( \frac{1}{1-x} -x^6 ) ( \frac{1}{1-x} )= (\frac{1}{1-x} -x^5 -x^6)\left[\frac{1}{(1-x)^2} - \frac{x^6}{(1-x) }\right] \equiv \frac{1}{(1-x)^3} - \frac{x^6}{(1-x)^2}-\frac{x^5}{(1-x)^2} - \frac{x^6}{(1-x)^2}= \frac{1}{(1-x)^3} - \frac{2x^6}{(1-x)^2}  - \frac{x^5}{(1-x)^2} $$
The above is quite easily calculable.
