Stars and bars¶

Chủ nhật - 08/02/2026 15:51
  • 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%)

Mình là Khánh, người sáng lập nghengu.vn – nơi chia sẻ niềm yêu thích với tiếng Nghệ, tiếng Việt và những phương ngữ đa dạng. Mình mong muốn lan toả vẻ đẹp của tiếng mẹ đẻ đến nhiều người hơn. Nếu thấy nội dung hữu ích, bạn có thể ủng hộ bằng cách donate hoặc mua sản phẩm giáo dục qua các liên kết tiếp thị trong bài viết.

Cảm ơn bạn đã đồng hành!

Tổng số điểm của bài viết là: 0 trong 0 đánh giá

  Ý kiến bạn đọc

.
Bạn đã không sử dụng Site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây
https://thoitietviet.edu.vn đọc sách online https://xemthoitiet.com.vn https://thoitiet24.edu.vn RR88 fun88 เข้าระบบ TOPCLUB 88xx 79king ssc88 Cm88 CM88 https://open88s.com/ C168 ufabet https://webmarket.jpn.com/ Sv388 Socolive TV Link nbet XX88 Socolive KJC https://okvip26.com/ Xoilac TV Live trực tiếp Cakhia TV Nohu90 Xoilac TV Socolive https://tt8811.net https://789pai.com https://mmoo.com.de https://go88.net/ c168 com five88 oxbet one88 xo88 https://playta88.com/ Bongdalu FUN88 ok9 kèo nhà cái 5 zowin.sh Cakhia TV Trực tiếp bóng đá Fun88 Bet KJC lu88 W 88 Alo789 FLY88 FLY88 OK9 COM oxbet five88 net88 https://c168.tel/ https://c168b.com/ 789bet f8bet f8bet new88 new88 ta88 debet fabet cakhiatv Ok365 OPEN88.COM https://sunwin97.in.net https://383sports.baby 84win B52CLUB ZBET NET88 C168 xem bóng đá luongsontv http://cracks.ru.com/ ok9 c168 c168 c168 https://bongdalu.us.com/ https://socolive2.cv/ F8bet C168 Bet168 new88 Socolive TV https://oxbet.cheap/ https://tx88d.com/ https://nohu.photo/ ok8386 ok9 red88 new88 new88 new88 Yo88 88VV Vin777 ok8386 https://open88.mobi/ f8bet TT88 new88 f8bet https://rophim.ws I9BET tỷ lệ kèo 999bet Tài Xỉu Online da88 9bet https://f8bet.ae.org Sun win Go88 789club Keo nha cai 5