Number of combinations with sets of identical items

All methods of solving the problem are equivalent to finding the appropriate coefficient in the polynomial. It is possible, using elementary algebra, to construct a formula with no more than $2^k$ terms (as shown below), but simply multiplying out the original polynomial is usually easier and less error-prone.

The given polynomial is $$\prod_{i=1}^k1+x+x^2+\cdots+x^{r_i}=\prod_{i=1}^k\frac{1-x^{r_i+1}}{1-x}=(1-x)^{-k}\prod_{i=1}^k1-x^{r_i+1}$$

Now, $$(1-x)^{-k}=\sum_{i=0}^\infty\binom{k-1+i}ix^i$$

and $$\prod_{i=1}^k1-x^{r_i+1}=\sum_{S\subset\{r_1\cdots r_k\}}(-1)^{|S|}x^{\sum_{\alpha\in S}(\alpha+1)}$$ which means $1$, minus all $x^{r_i+1}$ taken one at a time, plus all $x^{r_i+r_j+2}$ taken two at a time, minus all $x^{r_i+r_j+r_k+3}$ taken three at a time, etc.

Putting it together, let $U_n$ be the multiset of all submultisets $S$ of $\{r_1+1,r_2+1,\cdots r_k+1\}$ such that the sum $s(S)=\sum_{\alpha\in S}\alpha$ is always less than or equal to $n$. Then the formula is $$\sum_{S\in U_n}(-1)^{|S|}\binom{k-1+n-s(S)}{n-s(S)}$$ which is equivalent to the one given by san.

In the example of $r_1=2,\ r_2=1,\ r_3=2,\ n=3$ we get $U_3=\{\emptyset,\{3\},\{2\},\{3\}\}$ and $$\binom53-\binom20-\binom31-\binom20=10-1-3-1=5$$ as expected.

Instead of all that, we could just multiply out in a tableau. For example, take the $r$-values $5,3,2$:

$$\begin{array}{ccccccccccc}1&1&1&1&1&1\\1&2&3&4&4&4&3&2&1\\1&3&6&9&11&12&11&9&6&3&1\end{array}$$ The first row $(A_{1,j})$ contains $r_1+1$ ones. For $i>1$, the $i$th row is formed by summing each group of $r_i+1$ adjacent numbers in the preceding row: $$A_{i,j}=\sum_{k=0}^{r_i}A_{i-1,j-k}$$assuming everything out-of-range to be zero. For example, the third row is formed as $$0+0+1,\ 0+1+2,\ 1+2+3,\ 2+3+4,\ 3+4+4,\ 4+4+4,\ldots$$

Immediately we can read off that there are $11$ combinations of $6$ objects, etc. In Python you could write something like newrow = [sum(row[max(0,i-r):i+1]) for i in range(len(row)+r)]


I think the presentation as coefficients of that polynomial is the simplest possible answer. A more geometric presentation is as the number of integer points of the intersection of the hyperplane $\mathcal{P}: x_1+\dots+x_k=R$ in $\Bbb{R}^k$ with the parallelepiped $I_1\times\dots\times I_k$, where $I_i=[0,r_i]$. That is, $$ \# \ \Bbb{N}_0^k\cap (I_1\times\dots\times I_k) \cap \mathcal{P} $$

$\textbf{EDIT:}$

From this geometric interpretation one can get a combinatoric expresion for the number, which we will call $N(r_0,\dots,r_k,R)$. For this, it is convenient to consider $k+1$ sets, with $r_0,\dots,r_k$ elements respectively (it makes the formula look nicer). Then define $$ F=\{J\subset \{0,\dots,k\}, \text{such that } S_J:=\sum_{i\in J}r_i\le R-|J|\}, $$ and we have $$ N(r_0,\dots,r_k,R)=\sum_{J\in F}(-1)^{|J|}\binom{R-S_J-|J|+k}{k} $$

You can verify the formula for example in the case that for all $i$ we have $r_i\ge R$, where it gives the correct number $\binom{R+k}{k}$ (since $\emptyset\in F$ and $S_{\emptyset}=0$); and also in the case where only one $r_i<R$ (lets assume for example $r_0$), where it gives the correct number $\binom{R+k}{k}-\binom{R-r_0-1+k}{k}$.

The idea of the proof is that you start with a $k$-simplex with $\binom{R+k}{k}$ integer points and each $r_i<R$ cuts out a smaller $k$-simplex (of sidelength $R-r_0-1$), which you have to substract, but if $r_i+r_j\le R-2$, then you have to add the points in a smaller $k$-simplex (with sidelength $R-(r_i+r_j)-2$), which you have substracted twice and you continue with the same method for all $J\in F$.

With regard to the question "Also how would one solve the problem when the combination length is specified to be at most R instead of exactly R", note that $$ \sum_{r=0}^{R}N(r_0,\dots,r_k,r)=N(r_0,\dots,r_k,R,R). $$