Skip to content

Math

Modular arithmetic, number theory, combinatorics, and convolution.

Headers

Header Summary
adjugate_matrix.hpp 伴随矩阵与代数余子式
batch_inverse.hpp 一次除法批量求逆
big_integer_addition.hpp 多进制有符号大整数加法
big_integer_division.hpp 十进制大整数除法
big_integer_multiplication.hpp 多进制大整数乘法
binomial.hpp 素数模组合数与排列数
binomial_mod.hpp 合数模组合数
bivariate_formal_power_series.hpp 二元形式幂级数
characteristic_polynomial.hpp 矩阵特征多项式
combinatorial_sequences.hpp 常用组合计数数列
convolution.hpp 位运算卷积与子集卷积
convolution_f2_64.hpp 二元扩域运算与卷积
convolution_large.hpp 超出单次 NTT 的分块卷积
convolution_mod.hpp 任意模数卷积
convolution_mod_2_64.hpp 模二的 64 次幂卷积
decimal_power_mod.hpp 超长十进制指数模幂
determinant_mod.hpp 合数模行列式
dirichlet_prefix.hpp 整除商值与分块下标
discrete_log.hpp 非互质最小离散对数
distinct_subsequences.hpp 不同非空子序列计数
divisor_summatory.hpp 约数个数前缀和
divisor_table.hpp 逐点约数个数与约数和
dujiao_sieve.hpp 杜教筛求积性函数和
exponential_polynomial_sum.hpp 多项式乘指数的求和
extended_gcd.hpp 扩欧、整数方程与 exCRT
f2_matrix.hpp 异或矩阵消元与方程
factorize.hpp 64 位素性测试与因数分解
finite_field_polynomial_factorization.hpp 有限域多项式因式分解
fixed_discrete_log.hpp 固定底数多次离散对数
floor_monoid_product.hpp 整除格路径的有序乘积
floor_sum.hpp 有符号整除下取整求和
formal_power_series.hpp 幂级数对数、指数与开方
formal_power_series_composition.hpp 截断形式幂级数复合
fraction.hpp 精确有理数四则与比较
gaussian_integer.hpp 高斯整数运算与 gcd
hafnian.hpp 无符号带权配对计数
hex_big_integer_division.hpp 十六进制大整数除法
increasing_sequence_bounds.hpp 有界非降序列计数
integer_kth_root.hpp 精确整数 k 次方根
lagrange_interpolation.hpp 连续点值的单点插值
large_factorial.hpp 大参数阶乘取模
linear_algebra.hpp 秩、行列式与线性方程
linear_recurrence.hpp 线性递推求项与恢复
matrix.hpp 矩阵乘法与快速幂
min_of_mod_linear.hpp 模一次函数最小值
minmax_inclusion_exclusion.hpp 顺序统计量的最值容斥
mod_kth_root.hpp 素数模 k 次方根
mod_power_of_two_multiplicative_convolution.hpp 模二的幂乘法卷积
mod_sqrt.hpp 素数模平方根
modular_multiplicative_convolution.hpp 素数模乘法卷积
montmort.hpp 错排数
multiplicative_function_sum.hpp 大范围积性函数前缀和
multivariate_convolution.hpp 多元截断卷积
multivariate_cyclic_convolution.hpp 多维循环卷积
newton_basis.hpp Newton 插值基转换
nim_product.hpp 64 位 nim 乘法
pfaffian.hpp 斜对称矩阵配对计数
polynomial.hpp 多项式四则与求逆
polynomial_gcd.hpp 多项式 gcd、模逆与求根
polynomial_multipoint.hpp 多点求值与插值
polynomial_special_points.hpp 连续与等比点值、多项式连乘
prime.hpp 线性筛与常用积性函数
prime_binomial_table.hpp 运行时素数模组合数表
prime_summatory.hpp 素数个数与素数和
primitive_root.hpp 模原根
q_binomial_prime.hpp 01 串逆序对加权和与有限域子空间计数
range_product_changes.hpp 离线区间乘积变化
rational_approximation.hpp 有界分子分母的分数逼近
rational_reconstruction.hpp 从模剩余恢复有界分数
segmented_sieve.hpp 分段素数枚举
set_power_series.hpp 集合幂级数复合
sparse_formal_power_series.hpp 稀疏形式幂级数
sparse_matrix_determinant.hpp 稀疏矩阵行列式
squarefree_count.hpp 无平方因子整数计数
stern_brocot.hpp 正有理数与二叉树路径
stirling_small_prime.hpp 小素数模大下标 Stirling 数
subarray_gcd.hpp 所有子数组 gcd 压缩
subset_sum_count.hpp 各目标和的子集计数
sum_two_squares.hpp 二平方和构造与计数
tetration_mod.hpp 非互质模幂塔
xor_basis.hpp 子集异或最值与可达性

Formula reference

Binomial and multinomial identities

Unless stated otherwise, all parameters are nonnegative integers and \(\binom nk=0\) when \(k\notin[0,n]\).

\[ \binom nk=\binom{n-1}{k-1}+\binom{n-1}k,\qquad \binom nk=\binom n{n-k},\qquad \sum_{k=0}^{n}\binom nk=2^n. \]

Vandermonde and its central special case:

\[ \sum_k\binom rk\binom s{n-k}=\binom{r+s}n,\qquad \sum_{k=0}^{n}\binom nk^2=\binom{2n}n. \]

Hockey-stick, weighted sums, and binomial inversion:

\[ \sum_{i=r}^{n}\binom ir=\binom{n+1}{r+1},\qquad \sum_{k=0}^{n}k\binom nk=n2^{n-1},\qquad \sum_{k=0}^{n}k(k-1)\binom nk=n(n-1)2^{n-2}. \]
\[ g_n=\sum_{k=0}^{n}\binom nk f_k \iff f_n=\sum_{k=0}^{n}(-1)^{n-k}\binom nk g_k. \]
\[ \binom{n}{n_1,\ldots,n_m}=\frac{n!}{\prod_i n_i!} \quad\left(\sum_i n_i=n\right),\qquad (x_1+\cdots+x_m)^n =\sum_{n_1+\cdots+n_m=n}\binom{n}{n_1,\ldots,n_m}\prod_i x_i^{n_i}. \]

Stirling, Eulerian, and Bell numbers

The unsigned first-kind numbers count permutations with \(k\) cycles; the second-kind numbers count partitions into \(k\) nonempty unlabeled blocks:

\[ c(n,k)=c(n-1,k-1)+(n-1)c(n-1,k),\qquad \sum_{k=0}^{n}c(n,k)x^k=x(x+1)\cdots(x+n-1). \]
\[ S(n,k)=S(n-1,k-1)+kS(n-1,k) =\frac1{k!}\sum_{j=0}^{k}(-1)^{k-j}\binom{k}{j}j^n. \]
\[ x^n=\sum_{k=0}^{n}S(n,k)x(x-1)\cdots(x-k+1),\qquad B_n=\sum_{k=0}^{n}S(n,k),\qquad \sum_{n\ge0}B_n\frac{x^n}{n!}=e^{e^x-1}. \]
\[ \text{For }n\ge1,\quad \left\langle{n\atop k}\right\rangle =(n-k)\left\langle{n-1\atop k-1}\right\rangle +(k+1)\left\langle{n-1\atop k}\right\rangle =\sum_{j=0}^{k}(-1)^j\binom{n+1}{j}(k+1-j)^n. \]

Here \(\left\langle{n\atop k}\right\rangle\) counts permutations of \([n]\) with exactly \(k\) descents; it is zero outside \(0\le k<n\).

Common counting formulas

\[ C_n=\frac1{n+1}\binom{2n}{n},\qquad C_{n+1}=\sum_{i=0}^{n}C_iC_{n-i},\qquad D_n=(n-1)(D_{n-1}+D_{n-2}) =n!\sum_{k=0}^{n}\frac{(-1)^k}{k!}. \]
\[ \text{nonnegative stars and bars}=\binom{n+k-1}{k-1},\qquad \text{positive stars and bars}=\binom{n-1}{k-1}. \]
\[ p(0)=1,\quad p(n)=0\ (n<0),\qquad \sum_{n\ge0}p(n)x^n=\prod_{k\ge1}\frac1{1-x^k},\qquad p(n)=\sum_{k\ne0}(-1)^{k+1}p\!\left(n-\frac{k(3k-1)}2\right). \]

Burnside, necklaces modulo rotation with \(q\) colors, and labeled trees:

\[ |X/G|=\frac1{|G|}\sum_{g\in G}|\operatorname{Fix}(g)|,\qquad N_q(n)=\frac1n\sum_{d\mid n}\varphi(d)q^{n/d}. \]
\[ \#\text{ trees}=n^{n-2},\qquad \#\text{ trees with degrees }d_i =\frac{(n-2)!}{\prod_i(d_i-1)!} \quad\left(d_i\ge1,\ \sum_i d_i=2n-2\right). \]

For the Laplacian \(L\) of an undirected graph, let \(L^{(r)}\) denote the matrix obtained by deleting row and column \(r\). Then the Matrix-Tree theorem gives

\[ \tau(G)=\det L^{(r)}. \]

For a strongly connected Eulerian directed multigraph, fix an edge \(e\) whose tail is \(w\). Let \(t_w(G)\) be the number of spanning in-arborescences rooted at \(w\), and let \(\operatorname{EC}_e(G)\) count edge-distinct Euler circuits whose first edge is \(e\). The BEST theorem states

\[ \operatorname{EC}_e(G)=t_w(G) \prod_{v\in V}(\operatorname{outdeg}(v)-1)!. \]

Finite sums and inclusion-exclusion

\[ \sum_{i=0}^{n}x^i=\frac{x^{n+1}-1}{x-1},\qquad \sum_{i=0}^{n}ix^i =\frac{x-(n+1)x^{n+1}+nx^{n+2}}{(1-x)^2}. \]
\[ \sum_{i=1}^{n}i=\frac{n(n+1)}2,\qquad \sum_{i=1}^{n}i^2=\frac{n(n+1)(2n+1)}6,\qquad \sum_{i=1}^{n}i^3=\left(\frac{n(n+1)}2\right)^2. \]
\[ \left|\bigcup_{i=1}^{n}A_i\right| =\sum_{\varnothing\ne I\subseteq[n]}(-1)^{|I|+1} \left|\bigcap_{i\in I}A_i\right|. \]

Number theory

\[ \gcd(a,b)\operatorname{lcm}(a,b)=|ab|,\qquad \varphi(n)=n\prod_{p\mid n}\left(1-\frac1p\right),\qquad \sum_{d\mid n}\varphi(d)=n. \]
\[ \gcd(a,m)=1\Longrightarrow a^{\varphi(m)}\equiv1\pmod m,\qquad a^{p-1}\equiv1\pmod p\quad(p\text{ prime},\ p\nmid a). \]
\[ g(n)=\sum_{d\mid n}f(d) \iff f(n)=\sum_{d\mid n}\mu(d)g(n/d),\qquad \sum_{d\mid n}\mu(d)=[n=1]. \]

For coprime \(m,n\), if \(x\equiv a\pmod m\) and \(x\equiv b\pmod n\):

\[ x\equiv a+m\bigl((b-a)m^{-1}\bmod n\bigr)\pmod{mn}. \]

Generating functions and interpolation

\[ [x^n](A(x)B(x))=\sum_{k=0}^{n}a_kb_{n-k},\qquad a_n=\sum_{i=1}^{d}c_i a_{n-i} \Longrightarrow A(x)=\frac{P(x)}{1-c_1x-\cdots-c_dx^d}. \]
\[ P(x)=\sum_{i=0}^{n}y_i \prod_{\substack{0\le j\le n\\j\ne i}} \frac{x-x_j}{x_i-x_j},\qquad P(x_i)=y_i. \]

Newton identities, with power sums \(p_i=\sum_jx_j^i\) and elementary symmetric polynomials \(e_i\):

\[ ke_k=\sum_{i=1}^{k}(-1)^{i-1}e_{k-i}p_i. \]

Probability

\[ \mathbb E\!\left[\sum_iX_i\right]=\sum_i\mathbb E[X_i],\qquad \operatorname{Var}(X)=\mathbb E[X^2]-\mathbb E[X]^2,\qquad \mathbb E[\mathbf1_A]=\Pr(A). \]
\[ X\sim\operatorname{Bin}(n,p):\quad \Pr(X=k)=\binom nkp^k(1-p)^{n-k},\quad \mathbb E[X]=np,\quad\operatorname{Var}(X)=np(1-p). \]
\[ \Pr(X=k)_{\rm hypergeom} =\frac{\binom Kk\binom{N-K}{n-k}}{\binom Nn},\qquad \Pr(A\mid B)=\frac{\Pr(B\mid A)\Pr(A)}{\Pr(B)}. \]