30

I want to find the number of nonnegative integer solutions to $$x_1+x_2+x_3+x_4=22$$ which is also the number of combinations with replacement of $22$ items in $4$ types.

How do I apply stars and bars for this? What if there is an inequality or the lower bounds on the $x_i$ are not zero?

More generally, what do I use if the $x_i$ are multiplied by integer constants, such as in this equation? $$3x_1+2x_2+x_3+x_4=47$$

Parcly Taxel
  • 101,001
  • 20
  • 110
  • 191

4 Answers4

33

Yes, the Stars-and-Bars approach works great here, but you should know that there are two "versions" of the Stars-and-Bars approach. In both versions, we look for the number of distinct integer solutions to an equation such as yours.

In the first version, we require that every $x_i$ must be a positive integer.

In the second version, the restriction eases to include all non-negative $x_i$.

So, for example in your case, $x_1= 0, x_2=9, x_3=0, x_4=13$ would be one distinct solution in the second version, but would not be a solution in the first version.

I. positive integers $x_i$

For any pair of positive integers n and k, the number of distinct k-tuples of positive integers whose sum is $n$ is given by the binomial coefficient $${n - 1\choose k-1}.$$

In your case, $k = 4, n=22$. So the number of distinct solutions $(x_1, x_2, x_3, x_4)$ where the $x_i \in \mathbb Z, x_i>0$ is given by $$\binom{22-1}{4-1} = \binom{21}{3} = \frac{21!}{3!18!} = 1330$$


II. non-negative integers $x_i$

For any pair of natural numbers n and k, the number of distinct k-tuples of non-negative integers (which includes the possibility that one or more of the $x_i$ are zero) whose sum is $n$ is given by the binomial coefficient $$\binom{n + k - 1}{n} = \binom{n+k-1}{k-1}.$$

In your problem, $k = 4, n = 22.$ Here, the distinct solutions $(x_1, x_2, x_3, x_4)$ will include those from $I.$, but also allows 4-tuples in which one or more of the $x_i$ are zero: $x_i \in \mathbb Z, x_i\geq 0$.

$$\binom{22 + 4 -1}{22} = \binom{25}{22} = \dfrac{25!}{22!3!} = 2300$$

amWhy
  • 1
  • 168
  • 272
  • 496
  • Could you help me with my newer stars and bars problem? Noone has seen it – Partly Putrid Pile of Pus Aug 28 '14 at 12:52
  • 1
    Isn't the formula (n+k-1) choose k and not choose n? The equivalent would be (n+k-1) choose (n+k-1-k = n-1)? – thbcm Feb 21 '15 at 22:54
  • See how I've defined n, k. See [Wikipedia](http://en.wikipedia.org/wiki/Stars_and_bars_%28combinatorics%29) – amWhy Feb 21 '15 at 23:36
  • 1
    @inggumnator No, sorry. I think you're mixing up $n$ and $k$. The desired sum is $n$; $k$ is the number of $x_i$ (variables) whose sum is $n$. Go to the link I've posted above to re-educate yourself. Note that $$\binom{n+k-1}{n} = \binom{n+l -1}{k-1}$$ – amWhy Dec 02 '16 at 22:39
  • 1
    How did you decide what is $n$ and what is $k$? – Archer Dec 18 '17 at 08:55
18

The star method: consider 22 balls and 3 separations (because you have 4 boxes). I denote $*$ for the balls and $\Big |$ for the separation. Then it's the number of permutation of:

$$\left\{\underbrace{*\ *\ \cdots *\ }_{22\ balls}\Big|\hspace{0.5cm}\Big|\hspace{0.5cm} \Big|\hspace{0.5cm}\right\}$$

There is $25!$ permutations but the permutation of the balls together and the permutations of the separation together doesn't give a new combinaison, so you have to divide $25!$ by $3!22!$ and it gives $$\frac{25!}{3!22!}=\binom{25}{3}$$ different solutions.

idm
  • 11,544
  • 3
  • 30
  • 68
6

How to use the stars and bars method?

For $x_i\ (i=1,2,3,4)\in\mathbb N$, we have $$x_1+x_2+x_3+x_4=22\iff (x_1-1)+(x_2-1)+(x_3-1)+(x_4-1)=18.$$

Here, note that $x_i-1\ (i=1,2,3,4)$ are non-negative integers.

Choosing $4-1=3$ places (for bars) from $18+(4-1)$ places (for bars and stars) leads the answer is $\binom{18+(4-1)}{4-1}=\binom{21}{3}=1330.$

mathlove
  • 130,005
  • 9
  • 114
  • 281
2

Inequalities

To count nonnegative integer solutions of $$x_1+x_2+x_3+x_4\le22$$ add a slack variable $x_5$ to the left-hand side representing the gap between $x_1+x_2+x_3+x_4$ and $22$, which is by definition nonnegative: $$x_1+x_2+x_3+x_4+x_5=22$$ Stars and bars then gives $\binom{22+4}4=14950$ solutions.

General bounds

To count integer solutions of $$x_1+x_2+x_3+x_4=22$$ with $-2\le x_1\le7$ and all other $x_i$ bounds unchanged from the basic problem, first add $2$ to both sides and set $y=x_1+2$ with $0\le y\le9$: $$y+x_2+x_3+x_4=24$$ Without $y$'s upper bound, stars and bars gives $\binom{24+3}3=2925$ solutions. We need to remove solutions with $y\ge10$; we count these unwanted solutions like the lower bound case, by defining another nonnegative integer variable $z=y-10$ and simplifying: $$z+x_2+x_3+x_4=14$$ There are $\binom{14+3}3=680$ solutions to this, so the final answer is $2925-680=2245$.

Multiple nonzero lower bounds can be handled independently. Multiple upper bounds need to be handled using inclusion–exclusion. In particular, the number of ways $n$ ordered dice with faces from $0$ to $k-1$ can sum to $b$ is $$\sum_{i=0}^n(-1)^i\binom ni\binom{b-ki+n-1}{n-1}$$


Generating functions

Both of the above generalisations are themselves special cases of the generating function method, where the number of solutions to $\sum_ia_ix_i=n$ is found as the $x^n$ coefficient of a polynomial or power series whose factors encode information about a corresponding monomial.

A monomial $a_ix_i$ where $0\le p\le x_i\le q$ and $a_i$ is a constant positive integer corresponds to the factor $$\sum_{k=p}^q(x^{a_i})^k=(x^{a_i})^p\frac{1-(x^{a_i})^{q-p+1}}{1-x^{a_i}}$$ where $x^{q-p+1}$ disappears if $q=\infty$. For example, the number of nonnegative integer solutions to $3x_1+2x_2+x_3+x_4=47$ is the $x^{47}$ coefficient of $$\frac1{(1-x^3)(1-x^2)(1-x)^2}$$ which is $3572$.

Modular considerations may help to simplify such a problem. The number of nonnegative integer solutions to $x_1+5x_2+10x_3+25x_4+50x_5+100x_6=147$ equals the number of nonnegative integer solutions to $x_2+2x_3+5x_4+10x_5+20x_6\le29$, because $x_1$ must be of the form $5k+2$, because all other multipliers are divisible by $5$.

Parcly Taxel
  • 101,001
  • 20
  • 110
  • 191