Codeforces Round 1133 (Div. 1, Div. 2) is coming soon! Register here. ×

Saksham_Sahgal's blog

By Saksham_Sahgal, 17 months ago, In English

Stars and Bars – An Intuitive Explanation

Clasic Problem

The classic Stars and Bars problem asks:
Given n identical items (stars) and r boxes, in how many ways can you distribute the items among the boxes?
There are no restrictions — any box can have zero or more items.

Think of it as N stars (5 in this example). Now, if you use 2 sticks to act as barriers between stars, it will divide the stars into 3 regions, depending on the placement of the barriers. We can assume the stars in each region are stars in the boxes. so if we have to distribute into 3 boxes we need 2 sticks to divide into 3 regions; consequently, to divide into r regions we would need r-1 sticks.

Now to find the number of ways we can just permutate all of these by (n+r-1)! and then remove the repetitive permutations by dividing by = $$$(n!)(r-1)!$$$.

So the Number of ways becomes = $$$\frac{(n+r-1)!}{n!(r-1)!}$$$.

Now, we can also represent this with the equation = $$$x_1 + x_2 + x_3 ... x_r = n$$$ where $$$x_i$$$ represents the number of stars $$$ith$$$ box got and in this case $$$ 0 \lt = x_i$$$, and the solution for this is $$$\frac{(n+r-1)!}{(n!)(r-1)!}$$$.

stars and bars with lower bounds :

Keeping the above formula in mind, now what if, there is a constraint saying we cannot have any box with less than $$$l$$$ $$$(l \lt = n)$$$ stars. which means now constraint on $$$x_i$$$ is $$$ l \lt = x_i $$$, so to solve this now we can use substitution, we can say $$$x_i = y_i + l$$$ where $$$ 0 \lt = y_i$$$.

so if we substitute this in the equation $$$x_1 + x_2 + x_3 .. x_r = n$$$ we get $$$y_1 + y_2 + y_3 .. y_r = n - r*l$$$

so the solution is $$$\frac{(n - r(l-1)-1)!}{(n - r*l)!(r-1)!}$$$.

stars and bars with upper bounds :

Now what if there is a constraint saying we cannot have any box with more than $$$l$$$ $$$(l \lt = n)$$$ stars. which means now the constraint on $$$x_i$$$ is $$$ x_i \lt = l $$$, so to solve this now becomes little tricky. for simplicity we can assume we have r = 3. so $$$x_1 + x_2 + x_3 = n$$$

We can calculate the solution for this with $$$\frac{(n+r-1)!}{n!(r-1)!}$$$ and substract =

  • (case when $$$x_1 \gt l$$$)
  • (case when $$$x_2 \gt l$$$)
  • (case when $$$x_3 \gt l$$$)

and add to compensate for double substraction :

  • (case when $$$x_1$$$ and $$$x_2$$$ > l)
  • (case when $$$x_1$$$ and $$$x_3$$$ > l)
  • (case when $$$x_2$$$ and $$$x_3$$$ > l)

Subtract to compensate for addition :

  • (case when $$$x_1$$$ and $$$x_2$$$ and $$$x_3$$$ > l)

How to calculate Cases =

eg - when $$$x_1 \gt l$$$ : $$$y_1 = x_1 - (l+1)$$$ since $$$x_1 \gt l$$$ (ie) $$$x_1 \gt = l+1$$$ so $$$y_1 \gt = 0$$$

$$$x_1 + x_2 + x_3 = n$$$

$$$y_1 + x_2 + x_3 = n - (l + 1)$$$

So $$$n$$$ will become $$$n - (l+1)$$$

So the Number of ways becomes = $$$\frac{(n - (l + 1)+r-1)!}{(n - (l + 1))!(r-1)!}$$$. = $$$\frac{(n - l + 1)!}{(n - (l + 1))!(2)!}$$$

similarly you can calculate for case 2 and 3.

for case 4 5 and 6 :

$$$n$$$ will become $$$n - 2*(l+1)$$$ so: $$$\frac{(n - 2*l)!}{(n - 2l -2))!(2)!}$$$

For case 7 : $$$n$$$ will become $$$n - 3*(l+1)$$$ so: $$$\frac{(n-3l-1)!}{(n-3l-3)!(2)!}$$$.

Hence —

  • Case 1,2,3 substract : $$$\frac{(n - l + 1)!}{(n - (l + 1))!(2)!}$$$
  • Case 4,5,6 add : $$$\frac{(n - 2*l)!}{(n - 2l -2))!(2)!}$$$
  • Case 7substract :$$$\frac{(n-3l-1)!}{(n-3l-3)!(2)!}$$$.

Here is a problem that uses this Approach — Link

  • Vote: I like it
  • +14
  • Vote: I do not like it

| Write comment?
»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

How should I correct my combinatorics skills , I just cannot approach them with that ease as I do with other topics !

»
17 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

The way that I look at it is that: The general case is $$$x_i \geq 1$$$ and $$$x_1 +\cdots+x_r = n$$$. It's like we have $$$n$$$ stars and we have to place $$$r-1$$$ bars between them so the number of ways is $$$n-1$$$, i.e, the number of places, choose $$$r-1$$$, i.e, the number of bars to be placed. $$$x_i \geq 0$$$ is just a special case with lower-bounds being 0.

Also the case of lower-bounds can be generalized to if $$$x_i \geq l_i$$$ then the number of ways is $$$\binom{n-1+\sum 1-l_i}{r-1}$$$.

Another problem that can be solved using stars and bars is if $$$x_i\text{ are positive multiples of }d$$$ then the number of ways is $$$\binom{n/d-1}{r-1}$$$.

Combining the two ideas above we can solve the problem such that $$$x_i \% d = c_i$$$, in this case the number of ways would be $$$\binom{(n-\sum c_i)/d+r-1}{r-1}$$$.

A problem that can be solved using this: 1942E - Farm Game

I love stars and bars!