PrevNext
Not Frequent
 0/14

Combinatorics

Authors: Jesse Choe, Aadit Ambadkar, Dustin Miao

How to count.

If you've never encountered any combinatorics before, AoPS is a good place to start.

Resources
AoPS

practice problems, set focus to Counting and Probability.

AoPS

good book

Resources

Resources
CPH

module is based off of this

cp-algo
HE

teaches fundamental combinatorics with a practice problem at the end

AoPS

teaches basic combinatorics concepts

AoPS

teaches more advanced combinatorics concepts

CF

a good blog about the expected value

CF

a good blog about the inclusion-exclusion principle

If you prefer watching videos instead, here are some options:

Resources
Youtube

playlist by mathemaniac

Youtube

lectures 16-23

Youtube

Errichto video regarding expected value and sums of subsets

Binomial Coefficients

Focus Problem – try your best to solve this problem before continuing!

The binomial coefficient (nk)\binom{n}{k} (pronounced as "nn choose kk" or sometimes written as nCk{}_nC_k) represents the number of ways to choose a subset of kk elements from a set of nn elements. For example, (42)=6\binom{4}{2} = 6, because the set {1,2,3,4}\{1,2,3,4\} has 66 subsets of 22 elements:

{1,2},{1,3},{1,4},{2,3},{2,4},{3,4}\{1, 2\}, \{1, 3\}, \{1, 4\}, \{2, 3\}, \{2, 4\}, \{3, 4\}

There are two ways to calculate binomial coefficients:

Method 1: Pascal's Triangle (Dynamic Programming) - O(n2)\mathcal{O}(n^2)

Binomial coefficients can be recursively calculated as follows:

(nk)=(n1k1)+(n1k)\binom{n}{k} = \binom{n - 1}{k - 1} + \binom{n - 1}{k}

The intuition behind this is to fix an element xx in the set and choose k1k − 1 elements from n1n − 1 elements if xx is included in the set or choose kk elements from n1n − 1 elements, otherwise.

The base cases for the recursion are:

(n0)=(nn)=1\binom{n}{0} = \binom{n}{n} = 1

because there is always exactly one way to construct an empty subset and a subset that contains all the elements.

This recursive formula is commonly known as Pascal's Triangle.

A naive implementation of this would use a recursive formula, like below:

C++

/** Computes nCk mod p using naive recursion */
int binomial(int n, int k, int p) {
if (k == 0 || k == n) {
return 1;
}
return (binomial(n - 1, k - 1, p) + binomial(n - 1, k, p)) % p;
}

Additionally, we can optimize this from O(2n)\mathcal{O}(2^n) to O(n2)\mathcal{O}(n^2) using dynamic programming (DP) by caching the values of smaller binomials to prevent recalculating the same values over and over again. The code below shows a bottom-up implementation of this.

C++

/** Computes nCk mod p using dynamic programming */
int binomial(int n, int k, int p) {
// dp[i][j] stores iCj
vector<vector<int>> dp(n + 1, vector<int> (k + 1, 0));
// base cases described above
for (int i = 0; i <= n; i++) {
/*
* i choose 0 is always 1 since there is exactly one way
* to choose 0 elements from a set of i elements

Method 2: Factorial Definition (Modular Inverses) - O(nlogMOD)\mathcal{O}(n\log MOD)

Define n!n! as n×(n1)×(n2)×1n \times (n - 1) \times (n - 2) \times \ldots 1. n!n! represents the number of permutations of a set of nn elements. See this AoPS Article for more details.

Another way to calculate binomial coefficients is as follows:

(nk)=n!k!(nk)!\binom{n}{k} = \frac{n!}{k!(n-k)!}

Recall that (nk)\binom{n}{k} also represents the number of ways to choose kk elements from a set of nn elements. One strategy to get all such combinations is to go through all possible permutations of the nn elements, and only pick the first kk elements out of each permutation. There are n!n! ways to do so. However, note the the order of the elements inside and outside the subset does not matter, so the result is divided by k!k! and (nk)!(n − k)!

Since these binomial coefficients are large, problems typically require us to output the answer modulo a large prime pp such as 109+710^9 + 7.

Fortunately, we can use modular inverses to divide n!n! by k!k! and (nk)!(n - k)! modulo pp for any prime pp. In our case, MOD=109+7MOD = 10^9+7 is prime, so we can utilize modular inverses. However, computing inverse factorials online can be very time costly. Instead, we can precompute all factorials in O(n)\mathcal{O}(n) time and inverse factorials in O(nlogMOD)\mathcal{O}(n\log MOD) by taking the inverses of each factorial. See the code below for the implementation.

C++

const int MAXN = 1e6;
long long fac[MAXN + 1], inv[MAXN + 1];
/** Computes x^y modulo p in O(log p) time. */
long long exp(long long x, long long y, long long p) {
long long res = 1; x %= p;
while (y) {
if (y & 1) {
res *= x; res %= p;

Solution - Binomial Coefficients

The first method for calculating binomial factorials is too slow for this problem since the constraints on aa and bb are (1ba106)(1 \leq b \leq a \leq 10^6) (recall that the first implementation runs in O(n2)\mathcal{O}(n^2) time complexity). However, we can use the second method to answer each of the nn queries in constant time by precomputing factorials and their modular inverses.

C++

#include <iostream>
using namespace std;
using ll = long long;
const int MAXN = 1e6;
const int MOD = 1e9 + 7;
ll fac[MAXN + 1];
ll inv[MAXN + 1];

Derangements

Focus Problem – try your best to solve this problem before continuing!

The number of derangements of nn numbers, expressed as !n!n, is the number of permutations such that no element appears in its original position. Informally, it is the number of ways nn hats can be returned to nn people such that no person recieves their own hat.

Method 1: Principle of Inclusion-Exclusion

Suppose we had events E1,E2,,EnE_1, E_2, \dots, E_n, where event EiE_i corresponds to person ii recieving their own hat. We would like to calculate n!E1E2Enn! - \lvert E_1 \cup E_2 \cup \dots \cup E_n \rvert.

We subtract from n!n! the number of ways for each event to occur; that is, consider the quantity n!E1E2Enn! - \lvert E_1 \rvert - \lvert E_2 \rvert - \dots - \lvert E_n \rvert. This undercounts, as we are subtracting cases where more than one event occurs too many times. Specifically, for a permutation where at least two events occur, we undercount by one. Thus, add back the number of ways for two events to occur. We can continue this process for every size of subsets of indices. The expression is now of the form:

n!E1E2En=k=1n(1)k(number of permutations with k fixed points)n! - \lvert E_1 \cup E_2 \cup \dots \cup E_n \rvert = \sum_{k = 1}^n (-1)^k \cdot (\text{number of permutations with $k$ fixed points})

For a set size of kk, the number of permutations with at least kk indicies can be computed by choosing a set of size kk that are fixed, and permuting the other indices. In mathematical terms:

(nk)(nk)!=n!k!(nk)!(nk)!=n!k!{n \choose k}(n-k)! = \frac{n!}{k!(n-k)!}(n-k)! = \frac{n!}{k!}

Thus, the problem now becomes computing

n!k=0n(1)kk!n!\sum_{k=0}^n\frac{(-1)^k}{k!}

which can be done in linear time.

C++

#include <bits/stdc++.h>
// https://atcoder.github.io/ac-library/document_en/modint.html (included in grader)
#include <atcoder/modint>
using mint = atcoder::modint;
using namespace std;
int main() {
int N, M;

Method 2: Dynamic Programming

Suppose person 1 recieved person ii's hat. There are two cases:

  1. If person ii recieves person 1's hat, then the problem is reduced to a subproblem of size n2n - 2. There are n1n - 1 possibilities for ii in this case, so we add to the current answer (n1)!(n2)(n - 1)\cdot !(n - 2).
  2. If person ii does not recieve person 1's hat, then we can reassign person 1's hat to be person ii's hat (if they recieved person 1's hat, then this would become first case). Thus, this becomes a subproblems with size n1n - 1, are there n1n - 1 ways to choose ii.

Thus, we have

!n=(n1)(!(n2)+!(n1))!n = (n - 1)(!(n - 2) + !(n - 1))

which can be computed in linear time with a simple DP. The base cases are that !0=1!0 = 1 and !1=0!1 = 0.

C++

#include <bits/stdc++.h>
// https://atcoder.github.io/ac-library/document_en/modint.html (included in grader)
#include <atcoder/modint>
using mint = atcoder::modint;
using namespace std;
int main() {
int N, M;

Problems

StatusSourceProblem NameDifficultyTags
CSESEasy
Show TagsCombinatorics
CSESEasy
Show TagsCombinatorics
CSESEasy
Show TagsCombinatorics
CFEasy
Show TagsCombinatorics
CFEasy
Show TagsBinary Search, Combinatorics
BronzeEasy
Show TagsCombinatorics
DMOJNormal
Show TagsCombinatorics
GoldNormal
Show TagsBitset, PIE
CFNormal
Show TagsCombinatorics, DP
GoldNormal
Show TagsCombinatorics, Prefix Sums
CFHard
Show TagsCombinatorics, DP
GoldInsane
Show TagsBinary Search, Combinatorics, Math, Probability

Module Progress:

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

PrevNext