TITLE: Number of combinations with sets of identical items
QUESTION [8 upvotes]: I was trying to write a python program which takes a string and prints out all possible combinations of characters from it and in turn figure out the number of such possible combinations of a string to verify it's correctness
This made me think about the following problem:
Given N total objects of k distinct kinds such that for i = 1 to k, ri objects are of the same kind, what are the total number of combinations of containing R objects? 
The problem of permutations containing all N objects is much simpler and intuitive, however, I am interested in R < N.
I did some research and could only find this article discussing the problem, where it was suggested that the answer would be the coefficient of
xR in the following equation:

(1+x+x2+..+xr1)(1+x+x2+..+xr2).....(1+x+x2+..+xrk)

Is this the only way to solve this problem? Also how would one solve the problem when the combination length is specified to be at most R instead of exactly R?

REPLY [3 votes]: 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)]