Combinatorics

Binomial coefficients

The binomial coefficient C(n, k) is the number of ways to choose items out of when the order doesn't matter. You compute it with Pascal's triangle when is at most a few thousand, and with factorials modulo a prime when is up to 10^6. To list the choices themselves rather than count them, use backtracking.

Stars and bars

Stars and bars counts the ways to share identical items among distinct groups, such as candies among children or the solutions of x_1 + x_2 + \dots + x_k = n in non-negative integers. It writes each sharing as a row of items and dividers, so the count becomes a binomial coefficient. If a group may hold only a limited number of items, you also need inclusion–exclusion.

Was this page helpful?