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 (pronounced as " choose " or sometimes written as ) represents the number of ways to choose a subset of elements from a set of elements. For example, , because the set has subsets of elements:
There are two ways to calculate binomial coefficients:
Method 1: Pascal's Triangle (Dynamic Programming) -
Binomial coefficients can be recursively calculated as follows:
The intuition behind this is to fix an element in the set and choose elements from elements if is included in the set or choose elements from elements, otherwise.
The base cases for the recursion are:
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;}
Java
/** Computes nCk mod p using naive recursion */public static 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;}
Python
def binomial(n: int, k: int, p: int) -> int:"""Computes nCk mod p using naive recursion"""if k == 0 or k == n:return 1return (binomial(n - 1, k - 1, p) + binomial(n - 1, k, p)) % p
Additionally, we can optimize this from to 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 iCjvector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));// base cases described abovefor (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
Java
/** Computes nCk mod p using dynamic programming */public static int binomial(int n, int k, int p) {// dp[i][j] stores iCjint[][] dp = new int[n + 1][k + 1];// base cases described abovefor (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
Python
def binomial(n: int, k, p):"""Computes nCk mod p using dynamic programming"""# dp[i][j] stores iCjdp = [[0] * (k + 1) for _ in range(n + 1)]# base cases described abovefor i in range(n + 1):"""i choose 0 is always 1 since there is exactly one wayto choose 0 elements from a set of i elements
Method 2: Factorial Definition (Modular Inverses) -
Define as . represents the number of permutations of a set of elements. See this AoPS Article for more details.
Another way to calculate binomial coefficients is as follows:
Recall that also represents the number of ways to choose elements from a set of elements. One strategy to get all such combinations is to go through all possible permutations of the elements, and only pick the first elements out of each permutation. There are 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 and .
Since these binomial coefficients are large, problems typically require us to output the answer modulo a large prime such as . Fortunately, we can use modular inverses to divide by and modulo for any prime . Computing inverse factorials online can be very time costly. Instead, we can precompute all factorials in time and inverse factorials in . First, we compute the modular inverse of the largest factorial using binary exponentiation. For the rest, we use the fact that . See the code below for the implementation.
C++
const int MAXN = 1e6;long long fac[MAXN + 1];long long inv[MAXN + 1];/** Computes x^n modulo m in O(log p) time. */long long exp(long long x, long long n, long long m) {x %= m; // note: m * m must be less than 2^63 to avoid ll overflowlong long res = 1;while (n > 0) {
Java
import java.util.*;public class BinomialCoefficients {private static final int MAXN = (int)1e6;private static long[] fac = new long[MAXN + 1];private static long[] inv = new long[MAXN + 1];/** Computes x^n modulo m in O(log p) time. */private static long exp(long x, long n, long m) {x %= m; // note: m * m must be less than 2^63 to avoid ll overflow
Python
MAXN = 10**6fac = [0] * (MAXN + 1)inv = [0] * (MAXN + 1)def exp(x: int, n: int, m: int) -> int:"""Computes x^n modulo m in O(log p) time."""x %= m # note: m * m must be less than 2^63 to avoid ll overflowres = 1
Solution - Binomial Coefficients
The first method for calculating binomial factorials is too slow for this problem since the constraints on and are (recall that the first implementation runs in time complexity). However, we can use the second method to answer each of the 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];
Java
import java.io.*;import java.util.*;public class BinomialCoefficients {private static final int MAXN = (int)1e6;private static final int MOD = (int)1e9 + 7;private static long[] fac = new long[MAXN + 1];private static long[] inv = new long[MAXN + 1];public static void main(String[] args) {
Python
MAXN = 10**6MOD = 10**9 + 7fac = [0] * (MAXN + 1)inv = [0] * (MAXN + 1)Code Snippet: Counting Functions (Click to expand)
Derangements
Focus Problem – try your best to solve this problem before continuing!
The number of derangements of numbers, expressed as , is the number of permutations such that no element appears in its original position. Informally, it is the number of ways hats can be returned to people such that no person recieves their own hat.
Method 1: Principle of Inclusion-Exclusion
Suppose we had events , where event corresponds to person recieving their own hat. We would like to calculate .
We subtract from the number of ways for each event to occur; that is, consider the quantity . 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:
For a set size of , the number of permutations with at least indicies can be computed by choosing a set of size that are fixed, and permuting the other indices. In mathematical terms:
Thus, the problem now becomes computing
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 for this problem)#include <atcoder/modint>using mint = atcoder::modint;using namespace std;int main() {
Java
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int N = scanner.nextInt();int M = scanner.nextInt();long c = 1;for (int i = 1; i <= N; i++) {
Python
N, M = map(int, input().split())c = 1for i in range(1, N + 1):c = (c * i) + (-1 if i % 2 == 1 else 1)c %= Mc += Mc %= Mprint(c, end=" ")print()
Method 2: Dynamic Programming
Suppose person 1 recieved person 's hat. There are two cases:
- If person recieves person 1's hat, then the problem is reduced to a subproblem of size . There are possibilities for in this case, so we add to the current answer .
- If person does not recieve person 1's hat, then we can reassign person 1's hat to be person 's hat (if they recieved person 1's hat, then this would become first case). Thus, this becomes a subproblems with size , are there ways to choose .
Thus, we have
which can be computed in linear time with a simple DP. The base cases are that and .
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() {
Java
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int N = sc.nextInt();int M = sc.nextInt();int a = 1;int b = 0;
Python
N, M = map(int, input().split())a, b = 1, 0print(0, end=" ")for i in range(2, N + 1):c = (i - 1) * (a + b) % Mprint(c, end=" ")a, b = b, cprint()
Problems
Status | Source | Problem Name | Difficulty | Tags | |
---|---|---|---|---|---|
CSES | Easy | Show TagsCombinatorics | |||
CSES | Easy | Show TagsCombinatorics | |||
CF | Easy | Show TagsCombinatorics | |||
CF | Easy | Show TagsBinary Search, Combinatorics | |||
Bronze | Easy | Show TagsCombinatorics | |||
DMOJ | Normal | Show TagsCombinatorics | |||
AC | Normal | Show TagsCombinatorics | |||
Gold | Normal | Show TagsCombinatorics | |||
Gold | Normal | Show TagsBitset, PIE | |||
CF | Normal | Show TagsCombinatorics, DP | |||
Gold | Normal | Show TagsCombinatorics, Prefix Sums | |||
CF | Hard | Show TagsCombinatorics, DP | |||
Gold | Insane | 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!