Get number of additions that lead to a specific sum with given summands Suppose you have $n$ variables. Each of these variables (e.g. $a$) have their own interval between $0$ and $a_i$ (whole numbers). The only rule is that all these variables have to add up to a given value $s$.
An example:
$n = 3$, so the three variables are $a$, $b$ and $c$
$a_i = 2$, so $0\le$ $a$ $\le2$
$b_i = 3$, so $0\le$ $b$ $\le3$
$c_i = 4$, so $0\le$ $c$ $\le4$
$s = 6$, so $a+b+c=6$
The 9 unique possible solutions for these given variables are the following (the order of the variables is still important, so solution 7 and solution 9 are not the same):
$a=0$    $b=2$    $c=4$
$a=0$    $b=3$    $c=3$
$a=1$    $b=1$    $c=4$
$a=1$    $b=2$    $c=3$
$a=1$    $b=3$    $c=2$
$a=2$    $b=0$    $c=4$
$a=2$    $b=1$    $c=3$
$a=2$    $b=2$    $c=2$
$a=2$    $b=3$    $c=1$
What is the correct formula or program to only get the number of unique, possible solutions for any value of $n$, $s$ and the other variables, which would be 9 in this case?
 A: Suppose you have $k$ variables, and you want to add it up to $n$:
$$f_n(x) = \frac{1}{n!}\frac{d^n}{dx^n}\prod_{i=1}^k \frac{1-x^{a_i+1}}{1-x}$$
Then your solution is $f(0)$.
To come up with this, start with the generating functions for your constraints:
$$(1+x+...x^{a_i}) = \frac{1-x^{a_i+1}}{1-x}$$
Suppose we compute part of this product with your example:
$$(1+x+x^2)(1+x+x^2+x^3)(1+x+x^2+x^3+x^4)$$
Then multiply the $x$ from the first by the $x^2$ in the second and the $x^3$ in the last. This gives $x^6$, and corresponds to the $a=1,b=2,c=3$ solution. 
Then coefficient of $x^n$ in that product is the number of solutions. I used the derivative and factorial, then evaluation at $x=0$ to get this, but if you're using a CAS it probably has a more efficient coefficient function.
A: Let me work out for this particular case. You should be able to put it into a formula. 
If you can't, I'll see later.
We can use stars and bars with inclusion-exclusion.
Without any restrictions, there are ${6+3-1\choose 3-1} = {8\choose 2}= 28$ solutions.
To subtract cases that violate, (say) the $a_i$ restriction, pre-place $(2+1)=3$ in the $a_i$ compartment, and so on, then for combos of two, and combos of three, if possible, so
$${8\choose2} - \left[{5\choose 2} + {4\choose 2} + {3\choose 2}\right] = 9$$
Here it was not possible to violate constraints for combos of 2 or 3, but suppose we raise s to 16, then we would have
$${18\choose2} - \left[{15\choose 2} + {14\choose 2} + {13\choose 2}\right] + \left[{11\choose 2} + {10\choose 2} + {9\choose 2}\right] - \left[{6\choose 2}\right]$$ 
