1

As in the title, the problem is to choose $n$ elements from $k$ classes, in a way that from each class there are at most $r$ elements chosen (that is, from $0$ to $r$, inclusive).

The specific instance of the problem I'm trying to solve is to choose $12$ elements from $12$ classes, each with at most two elements.

One approach is the one in the accepted answer of Number of combinations of k types from set n with limited number of each type, where the problem becomes counting the number of solutions of

$$ \sum_{i=1}^{12} a_i = 12, a_i \leq 2 .$$

Then, we compute the number of combinations with repetition, with the stars-and-bars method, which is $12+24-1 \choose 12$. And we have then to go and apply the constraint, using the inclusion-exclusion principle. So, we start by removing the solutions in which one class has more than one element, i.e., $a_i = 3$ for some $i$. This is equivalent to the number of solutions of

$$\sum_{i=1}^{12} a_i = 9.$$

This is equal to $9+24-1 \choose 9$ and there are $12$ classes, so we have to subtract $12 {9+24-1 \choose 9}$. According to the inclusion-exclusion principle, we have now to add the cases where two classes violate the constraint. This is equivalent to the number of solutions of

$$\sum_{i=1}^{12} a_i = 6,$$

and is equal to $6+24-1 \choose 6$ and there are $11\cdot12$ ways of picking the two classes that violate the constraint. So we have to add $11 \cdot 12 {6+24-1 \choose 6}$.

Continuing, we have to subtract triplets, that is, the solutions to the equation

$$\sum_{i=1}^{12} a_i = 3,$$

a total of $10\cdot11\cdot12 {3+24-1 \choose 3}$.

So the final solution becomes

$${12+24-1 \choose 12 }- 12 {9+24-1 \choose 9} + 11 \cdot 12 {6+24-1 \choose 6} - 10\cdot11\cdot12 {3+24-1 \choose 3}.$$

Is this approach correct? Also, is there any more general approach?

N. F. Taussig
  • 76,571
Paul92
  • 470
  • 1
    If you are selecting $12$ elements from $12$ classes, then your initial equation should be $$x_1 + x_2 + x_3 + \cdots + x_{12} = 12$$ which has $\binom{12 + 12 - 1}{12}$ solutions since we must choose which $12$ of the $23$ positions occupied by $12$ ones and $12 - 1$ addition signs will be filled with ones. – N. F. Taussig Apr 13 '22 at 09:11
  • 1
    For a generating function approach, I would have thought you want the coefficient of $x^n$ in the expansion of $(1+x+x^2+\cdots+x^r)^k = \left(\frac{1-x^{r+1}}{1-x}\right)^k$, thought this is much smaller than your result (for $r=2,n=k=12$ it gives $73789$) so may be answering a different question – Henry Apr 13 '22 at 09:18
  • Problem is a duplicate. A solution to the more general problem, which involves Stars-and-Bars + Inclusion-Exclusion is given here. – user2661923 Apr 13 '22 at 10:36
  • @user2661923 OP already knows the method, and is asking us to check their work. Your link does not help with that. – Mike Earnest Apr 13 '22 at 14:42
  • @MikeEarnest Good point; I did not read his question that closely. However, reading the posted question more closely, the OP is asking $2$ questions: [1] Is his work valid? [2] Is there a better method? Absent a generating functions approach, the answer I linked to answers both questions, because [1] Either his work agrees with the approach that I blueprinted or it doesn't and [2] He can infer that I (for one) can think of no other approach (besided generating functions, which I know nothing about). So, it is questionable whether there is value in the link that I provided. – user2661923 Apr 13 '22 at 14:47

1 Answers1

1

Your strategy is correct, but your solution is not.

Let $x_i$, $1 \leq i \leq 12$, be the number of elements from the $i$th class. Since we want to select a total of $12$ elements, with at most $2$ from any class, we want to find the number of solutions of the equation $$x_1 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} = 12 \tag{1}$$ subject to the constraints that $x_i \leq 2, 1 \leq i \leq 12$.

If we initially ignore those constraints, a particular solution of equation 1 in the nonnegative integers corresponds to the placement of $12 - 1 = 11$ addition signs in a row of $12$ ones. For instance, $$1 + + 1 1 1 + 1 + + + 1 1 + 1 + 1 1 + + 1 1 +$$ corresponds to the solution $x_1 = 1, x_2 = 0, x_3 = 3, x_4 = 1, x_5 = x_6 = 0, x_7 = 2, x_8 = 1, x_9 = 2$, $x_{10} = 0, x_{11} = 2, x_{12} = 0$. The number of solutions of equation 1 in the nonnegative integers is the number of ways we can place $12 - 1 = 11$ addition signs in a row of $12$ ones, which is $$\binom{12 + 12 - 1}{12 - 1} = \binom{23}{11}$$ since we must choose which $12 - 1 = 11$ of the $12 + 12 - 1 = 23$ positions required for $12$ ones and $11$ addition signs will be filled with addition signs.

From these, we must subtract those solutions which violate one or more of the constraints. Observe that at most four constraints can be violated since $5 \cdot 3 = 15 > 12$.

Suppose $x_1 > 2$. Then $x_1' = x_1 - 3$ is a nonnegative integer. Substituting $x_1' + 3$ for $x_1$ in equation 1 yields \begin{align*} x_1' + 3 + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 12\\ x_1' + x_2 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 9 \tag{2} \end{align*} Equation 2 is an equation in the nonnegative integers with $$\binom{9 + 12 - 1}{12 - 1} = \binom{20}{11}$$ solutions.

By symmetry, there are the same number of solutions which violate the restriction that $x_i \leq 2$ for each of the $12$ variables. Hence, we need to subtract $$\binom{12}{1}\binom{20}{11}$$ from the number of solutions of equation 1.

If we do so, we will have subtracted too much since we will have subtracted each solution in which two constraints are violated twice, once for each way we could have designated one of the variables as the variable which exceeded $2$. We only want to subtract such cases once, so we need to add them to the total.

Suppose $x_1 > 2$ and $x_2 > 2$. Then both $x_1' = x_1 - 3$ and $x_2' = x_2 - 3$ are nonnegative integers. Substituting $x_1' + 3$ for $x_1$ and $x_2' + 3$ for $x_2$ in equation 1 yields \begin{align*} x_1' + 3 + x_2' + 3 + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 12\\ x_1' + x_2' + x_3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 6 \tag{3} \end{align*} Equation 3 is an equation in the nonnegative integers with $$\binom{6 + 12 - 1}{12 - 1} = \binom{17}{11}$$ solutions.

By symmetry, there are the same number of solutions for each of the $\binom{12}{2}$ ways we could violate two of the restrictions. Hence, we need to add $$\binom{12}{2}\binom{17}{11}$$ to the total.

However, adding these cases to the total adds too much. We have subtracted each case which violates three of the restrictions three times, once for each of the three ways we could have designated one of those three variables as the one which violates the restriction, and added them three times, once for each of the $\binom{3}{2}$ ways we could have designated two of those three variables as the ones which violate the restrictions. Therefore, we still need to subtract those cases which violate three of the restrictions.

Suppose $x_1 > 2$, $x_2 > 2$, and $x_3 > 2$. Then $x_1' = x_1 - 3$, $x_2' = x_2 - 3$, and $x_3' = x_3 - 3$ are nonnegative integers. Substituting $x_1' + 3$ for $x_1$, $x_2' + 3$ for $x_2$, and $x_3' + 3$ for $x_3$ in equation 1 yields \begin{align*} x_1' + 3 + x_2' + 3 + x_3' + 3 + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 12\\ x_1' + x_2' + x_3' + x_4 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 3 \tag{4} \end{align*} Equation 4 is an equation in the nonnegative integers with $$\binom{3 + 12 - 1}{12 - 1} = \binom{14}{11}$$ solutions.

By symmetry, there are the same number of solutions for each of the $\binom{12}{3}$ ways we could violate three of the restrictions. Hence, we need to add $$\binom{12}{3}\binom{14}{11}$$ to the total.

Now, we have subtracted too much. We subtracted each case in which four of the restrictions are violated four times, once for each of the four ways we could have designated one of the four variables which violated the restrictions as the one which violated the restriction; then added them six times, once for each of the $\binom{4}{2}$ ways we could have designated two of the four variables which violated the restrictions as the two which violated the restrictions; then subtracted them four times, once for each of the $\binom{4}{3}$ ways we could have designated three of the four variables which violated the restrictions as the three which violated the restrictions. Therefore, we have subtracted those cases in which four of the restrictions are violated $4 - 6 + 4 = 2$ times. We only want to subtract them once, so we must add them to the total.

Suppose $x_1 > 2$, $x_2 > 2$, $x_3 > 2$, and $x_4 > 2$. Then $x_1' = x_1 - 3$, $x_2' = x_2 - 3$, $x_3' = x_3 - 3$, and $x_4' = x_4 - 3$ are nonnegative integers. Substituting $x_1' + 3$ for $x_1$, $x_2' + 3$ for $x_2$, $x_3' + 3$ for $x_3$, and $x_4' + 3$ for $x_4$ in equation 1 yields \begin{align*} x_1' + 3 + x_2' + 3 + x_3' + 3 + x_4' + 3 + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 12\\ x_1' + x_2' + x_3' + x_4' + x_5 + x_6 + x_7 + x_8 + x_9 + x_{10} + x_{11} + x_{12} & = 0 \tag{5} \end{align*} Equation 5 is an equation in the nonnegative integers with one solution since all the variables must be equal to zero.

By symmetry, there are the same number of solutions for each of the $\binom{12}{4}$ ways we could violate four of the restrictions. Hence, we need to add $$\binom{12}{4}$$ to the total.

Thus, by the Inclusion-Exclusion Principle, the number of admissible solutions is $$\binom{23}{11} - \binom{12}{1}\binom{20}{11} + \binom{12}{2}\binom{17}{11} - \binom{12}{11}\binom{14}{11} + \binom{12}{4}\binom{11}{11}$$ For a more general approach, you can use generating functions. In this case, you want to find the coefficient of $x^{12}$ in the expansion of $$(1 + x + x^2)^{12}$$ which takes some work.

N. F. Taussig
  • 76,571