Number of combinations with repetitions (Constrained)

Let $A=\sum_{i=1}^n a_i$.

If there were no upper bounds on the $x_i$, the number of solutions would be $N(k-A,n)$, where

$$N(k,n)\;=\;\binom{k+n-1}{n-1}$$

is the number of weak $n$-compositions of $k$.

Now, if we exclude solutions in which some collection of the $a_i>b_i$, inclusion-exclusion gives us the following for the fully constrained situation:

$$\sum_{S\subseteq [n]}(-1)^{|S|}\; N\left(k-A-\sum\nolimits_{i\in S}1+(b_i-a_i),\;n \right)$$

where $[n]=\{1,\dots ,n\}$.


$x^{b_i}+x^{b_i-1}+x^{b_i-2}+\dots+x^{a_i}=\frac{x^{b_i+1}-x^{a_i}}{x-1}$ is the generating function for the number of ways to roll a $k$ on a die with faces consisting of $a_i\dots b_i$ pips. The generating function for the number of ways to roll a $k$ as the the sum of $n$ such dice is the product of their generating functions.

Thus, the answer is the coefficient of $x^k$ in $$ \prod_{i=1}^n\frac{x^{b_i+1}-x^{a_i}}{x-1} $$