Suppose I have numbers the $\{0,1,2,3\}$ in how many ways can one arrange them so the sum would be equal to 5? For instance, $$ \begin{array}{cccc} 0 & 1 & 2 & 2 \\ 1 & 0 & 2 & 2 \\ 1 & 2 & 0 & 2\\ 1 & 2 & 2 & 0\\ 0& 2 & 2 & 1\\ \vdots & & & \\ 2 & 3 & 0 & 0 \\ \vdots \end{array} $$ As you can see I am looking for ALL (including permutations) possible combinations (such that the sum of each row is 5). I was wondering if this can be represented by binomial coefficients?
side note: This is just a simplified example, I would want to then generalise such that sum of each row would be (0 to 8), i.e. In how many ways one can arrange 0,1,2,3 so the sum would be 0 and 1 and ... 8. But I wanted to know how this works first for a given sum.
Let us consider the Diophantine Equation $x_1+x_2+x_3+x_4 = 5$ with integer solutions. We place the restriction that for all $i$ we have $0\le x_i \le 3$. This is a well-known problem. First, we consider the case with no upper bound, then we subtract the cases where an upper bound is violated.
We consider the case when $0\le x_i$ (with no upper bound). There are $\dbinom{5+4-1}{5} = \dbinom{8}{5}$ ways to generate this sum. Now, we use Inclusion/Exclusion to subtract off sums that violate the upper bound of 3. It is not possible for two of the summands to be at least 4 (since we have the lower bound of zero already). So, only one upper bound can be violated at a time, but any one of the four will be violated. So, we assume $x_1\ge 4$ while $0\le x_2,x_3,x_4$. Solutions with these lower bounds are in one-to-one correspondence with solutions to the Diophantine equation with $y_1=x_1-4$, $y_2=x_2, y_3=x_3,y_4=x_4$ and $0\le y_i$. Now, we have $5=x_1+x_2+x_3+x_4 = y_1+4+y_2+y_3+y_4$ so $y_1+y_2+y_3+y_4 = 1$. This has $\dbinom{1+4-1}{1} = \dbinom{4}{1}$ solutions. Since any of the upper bounds could be violated in this way, we have the total number of solutions (with both upper and lower bounds) as:
$$\dbinom{8}{5}-4\dbinom{4}{1} = 40$$