Stars and bars¶
- Last update: February 20, 2024 Original
Stars and bars is a mathematical technique for solving certain combinatorial problems. It occurs whenever you want to count the number of ways to group identical objects.
Theorem¶
The number of ways to put $n$ identical objects into $k$ labeled boxes is
The proof involves turning the objects into stars and separating the boxes using bars (therefore the name). E.g. we can represent with $bigstar | bigstar bigstar |~| bigstar bigstar$ the following situation: in the first box is one object, in the second box are two objects, the third one is empty and in the last box are two objects. This is one way of dividing 5 objects into 4 boxes.
It should be pretty obvious, that every partition can be represented using $n$ stars and $k - 1$ bars and every stars and bars permutation using $n$ stars and $k - 1$ bars represents one partition. Therefore the number of ways to divide $n$ identical objects into $k$ labeled boxes is the same number as there are permutations of $n$ stars and $k - 1$ bars. The Binomial Coefficient gives us the desired formula.
Number of non-negative integer sums¶
This problem is a direct application of the theorem.
You want to count the number of solution of the equation
with $x_i ge 0$.
Again we can represent a solution using stars and bars. E.g. the solution $1 + 3 + 0 = 4$ for $n = 4$, $k = 3$ can be represented using $bigstar | bigstar bigstar bigstar |$.
It is easy to see, that this is exactly the stars and bars theorem. Therefore the solution is $binom{n + k - 1}{n}$.
Number of positive integer sums¶
A second theorem provides a nice interpretation for positive integers. Consider solutions to
with $x_i ge 1$.
We can consider $n$ stars, but this time we can put at most one bar between stars, since two bars between stars would represent $x_i=0$, i.e. an empty box. There are $n-1$ gaps between stars to place $k-1$ bars, so the solution is $binom{n-1}{k-1}$.
Number of lower-bound integer sums¶
This can easily be extended to integer sums with different lower bounds. I.e. we want to count the number of solutions for the equation
with $x_i ge a_i$.
After substituting $x_i' := x_i - a_i$ we receive the modified equation
with $x_i' ge 0$. So we have reduced the problem to the simpler case with $x_i' ge 0$ and again can apply the stars and bars theorem.
Number of upper-bound integer sums¶
With some help of the Inclusion-Exclusion Principle, you can also restrict the integers with upper bounds. See the Number of upper-bound integer sums section in the corresponding article.
Practice Problems¶
- Codeforces - Array
- Codeforces - Kyoya and Coloured Balls
- Codeforces - Colorful Bricks
- Codeforces - Two Arrays
- Codeforces - One-Dimensional Puzzle
- Contributors:
- jakobkogler (70.73%)
- jxu (13.41%)
- Electron1997 (6.1%)
- adamant-pwn (6.1%)
- fork52 (3.66%)
Ý kiến bạn đọc
Bài viết xem nhiều
-

Phân tích truyện ngắn Lặng Lẽ Sa Pa của Nguyễn Thành Long
-

Top những bài thơ tự do hay, cảm xúc
-

Vẽ Tranh Chống Bạo Lực Học Đường: Cùng Các Em Lan Tỏa Thông Điệp
-
Top 20 Viết đoạn văn thể hiện tình cảm, cảm xúc về một câu chuyện lớp 5 (điểm cao)
-
100+ bài thơ chúc Tết hay, ngắn gọn và ý nghĩa nhất 2026
-

Phong cách sáng tác của Tố Hữu: Chất thơ Trữ tình, chính trị
-
Đoạn văn nêu lí do em yêu thích một câu chuyện về tình yêu thương hoặc lòng biết ơn (hay, ngắn gọn)
-
Top 30 Tập làm một bài thơ tám chữ lớp 9 (điểm cao)
-

Những bài văn nlxh đạt giải quốc gia pdf
-
Viết bài văn thuyết minh về tác phẩm Chí Phèo lớp 11
-
Bộ đề thi học sinh giỏi môn Ngữ văn lớp 7 (40 đề) Đề thi HSG Văn 7 (Có đáp án)



