Find the number of solutions to $n_1 +n_2 + n_3 + n_4 = 12$ when values of $n_k$ have restrictions Find the number of solutions in integers to
$$n_1 + n_2 + n_3 + n_4 = 12$$
satisfying $0 \le n_1 \le 4, 0 \le n_2 \le 5, 0 \le n_3 \le 8, \text{and } 0\le n_4 \le 9$
I'm having trouble with this. I can see that $C(12 + 4 - 1, 12)$ is the solution when the only restriction is $n_k \ge 0$. However, I'm unsure how to get to (the textbook solution):
$C(12+4-1,12) - [C(7+4-1,7)+C(6+4 -1, 6)+C(3+4-1,3)+C(2+4-1,2)-C(1+4-1,1)]$

Let $A$ denote nonnegative solutions to $n_1+n_2+n_3+n_4=12$ and $A_i$ denote those solutions where the upper bound on $n_i$ is violated, then inclusion/exclusion gives
$$|\overline{A_1\cup A_2\cup A_3\cup A_4}|=|A|-(|A_1|+|A_2|+|A_3|+|A_4|)+(|A_1\cap A_2|+\cdots)-\cdots\\
=\binom{12+4-1}{12}-\left(\binom{7+4-1}7+\binom{6+4-1}6+\binom{3+4-1}3+\binom{2+4-1}2\right)+\binom{1+4-1}1$$
where e.g. $|A_1|=\binom{7+4-1}7$ because the solutions there can be recast as solutions to $(m_1+5)+n_2+n_3+n_4=12$ with $m_1,n_i\ge0$ or $m_1+n_2+n_3+n_4=7$, and omitted terms are because they are empty (i.e. they reduce to a sum of nonnegative integers to a negative integer).
