返回「邏輯與算法」主頁

本页面仅收录 Useful Formulas & Theorems in CP。对于知识点,另见 邏輯與算法:雜項 。
略去繁杂的证明和算法流程,多写柿子少废话。基本是看到什么有用的好吃的柿子就会塞进来。
文中可能出现中英文混用的情况,因为作者会优先使用更便于表达的语言(x。

恒等式

极值对偶恒等式

x+y=max⁡(x,y)+min⁡(x,y)∣x−y∣=max⁡(x,y)−min⁡(x,y)2max⁡(x,y)=(x+y)+∣x−y∣2min⁡(x,y)=(x+y)−∣x−y∣∣x−y∣=∣max⁡(x,y)−min⁡(x,y)∣x+y=(x∨y)+(x∧y)x⊕y=(x∨y)−(x∧y)2(x∨y)=(x+y)+(x⊕y)2(x∧y)=(x+y)−(x⊕y)x⊕y=(x∨y)⊕(x∧y)\begin{aligned} x + y & = \max(x, y) + \min(x, y) \\ |x-y| & = \max(x, y) - \min(x, y) \\ 2 \max(x, y) & = (x + y)+ |x-y| \\ 2 \min(x, y) & = (x + y) - |x-y| \\ \lvert x - y \rvert & = \lvert \max(x, y) - \min(x, y) \rvert \\ \\ x+y & = (x \vee y) + (x \wedge y) \\ x \oplus y & = (x \vee y) - (x \wedge y) \\ 2 (x \vee y) & = (x + y) + (x \oplus y) \\ 2 (x \wedge y) & = (x + y) - (x \oplus y) \\ x \oplus y & = (x \lor y) \oplus (x \land y) \end{aligned}

Number Theory

p-adic valuation

For a prime pp:

  • If n=pkmn = p^k m, where p∤mp \nmid m, then νp(n)=k\nu_p(n) = k is defined as the pp-adic valuation of nn.
  • x≡y(modpr)  ⟺  pr∣x−y  ⟺  νp(x−y)≥rx \equiv y \pmod{p^r} \iff p^r \mid x-y \iff \nu_p(x-y) \ge r
  • νp(ab)=νp(a)+νp(b)\nu_p(ab) = \nu_p(a) + \nu_p(b)
  • νp(a/b)=νp(a)−νp(b)\nu_p(a/b) = \nu_p(a) - \nu_p(b)

Let n∈N+n \in \mathbb{N}^+ have the prime factorization n=∏p∣npνp(n)\displaystyle n = \prod_{p \mid n} p^{\nu_{p}(n)}.

  • (a,b)=gcd⁡(a,b)=∏ppmin⁡(νp(a),νp(b))(a, b) = \gcd(a, b) = \prod_{p} p^{\min(\nu_{p}(a), \nu_{p}(b))}
  • [a,b]=lcm⁡(a,b)=∏ppmax⁡(νp(a),νp(b))[a, b] = \operatorname{lcm}(a, b) = \prod_{p} p^{\max(\nu_{p}(a), \nu_{p}(b))}
  • ab=(a,b)[a,b]a b = (a, b)[a, b]
  • a⊥b  ⟺  (a,b)=1a \perp b \iff (a, b) = 1

Lifting The Exponent Lemma (LTE)

Let p≠2p \neq 2,

p∣x−y, p∤x  ⟹  νp(xn−yn)=νp(x−y)+νp(n)2∤n, p∣x+y, p∤x  ⟹  νp(xn+yn)=νp(x+y)+νp(n)\begin{aligned} p \mid x - y, \ p \nmid x & \implies \nu_p(x^n-y^n)=\nu_p(x-y) + \nu_{p}(n) \\ 2 \nmid n, \ p \mid x + y, \ p \nmid x & \implies \nu_p(x^n+y^n)=\nu_p(x+y) + \nu_{p}(n) \end{aligned}

Let p=2p = 2,

2∤n, 2∤x, 2∤y  ⟹  ν2(xn−yn)=ν2(x−y)2∣n, 2∤x, 2∤y  ⟹  ν2(xn−yn)=ν2(x−y)+ν2(x+y)+ν2(n)−12∤n, 2∤x, 2∤y  ⟹  νp(xn+yn)=νp(x+y)+νp(n)\begin{aligned} 2 \nmid n, \ 2 \nmid x, \ 2 \nmid y & \implies \nu_2(x^n-y^n)=\nu_2(x-y) \\ 2 \mid n, \ 2 \nmid x, \ 2 \nmid y & \implies \nu_2(x^n-y^n)=\nu_2(x-y)+\nu_2(x+y)+\nu_2(n)-1 \\ 2 \nmid n, \ 2 \nmid x, \ 2 \nmid y & \implies \nu_p(x^n+y^n)=\nu_p(x+y) + \nu_{p}(n) \end{aligned}

Proof. 设 n=2kmn=2^k m,其中 k=ν2(n)≥1k = \nu_2(n)\ge1,mm 为奇数。
令 A=x2k,B=y2kA=x^{2^k},B=y^{2^k},由 Am−Bm=(A−B)(Am−1+Am−2B+⋯+Bm−1)A^m-B^m=(A-B)(A^{m-1}+A^{m-2}B+\cdots+B^{m-1}),括号内是 mm 个奇数之和,故为奇数。因此 ν2(xn−yn)=ν2(x2k−y2k)\nu_2(x^n-y^n)=\nu_2(x^{2^k}-y^{2^k})。
用平方差公式 x2k−y2k=(x−y)(x+y)∏j=1k−1(x2j+y2j)x^{2^k}-y^{2^k}=(x-y)(x+y)\prod_{j=1}^{k-1}(x^{2^j}+y^{2^j}) 展开,ν2(x2k−y2k)=ν2(x−y)+ν2(x+y)+∑j=1k−1ν2(x2j+y2j)\nu_2(x^{2^k}-y^{2^k})=\nu_2(x-y) + \nu_2(x+y) + \sum_{j=1}^{k-1}\nu_2(x^{2^j}+y^{2^j})。
考虑到奇数的平方模 88 余 11,所以对每个 j≥1j\ge1,都有 x2j+y2j≡1+1=2(mod8)x^{2^j}+y^{2^j}\equiv 1+1=2\pmod8,即 ν2(x2j+y2j)=1\nu_2 (x^{2^j}+y^{2^j})=1,代回原式即证。

Legendre Formula (中:勒让德).

νp(n!)=∑k=1pk⩽n⌊npk⌋=n−Sp(n)p−1.\nu_{p}(n!)=\displaystyle\sum\limits_{k=1}^{p^k \leqslant n}\bigg\lfloor\dfrac{n}{p^k}\bigg \rfloor=\dfrac{n-S_p(n)}{p-1}.

where Sp(n)S_p(n) denotes the sum of the digits of nn written in base pp, and

Sp(n)={1,if n=1Sp(np),if p∣nSp(n−1)+1,otherwise.S_p(n)= \begin{cases} 1, & \text {if} n=1 \\ \displaystyle S_p\Big(\frac{n}{p}\Big), & \text {if} p\mid n \\ S_p(n-1)+1, & \text {otherwise}. \end{cases}

Ex 求 M=∑i=1nv5(i!)\displaystyle M=\sum_{i=1}^{n}v_5(i!)。([VJwin6H - 阶乘之乘](https: //www.luogu.com.cn/problem/P2388))

M=∑i=1nv5(i!)=∑i=1n∑k=15k≤i⌊i5k⌋=∑k=15k≤n∑i=1n⌊i5k⌋=∑k=15k≤n[5k+2⋅5k+⋯+(⌊n5k⌋−1)⋅5k+⌊n5k⌋(n mod 5k+1)]=∑k=15k≤n[5k2⌊n5k⌋(⌊n5k⌋−1)+⌊n5k⌋(n mod 5k+1)]=∑5≤i=5k≤n[i2⌊ni⌋(⌊ni⌋−1)+⌊ni⌋(n mod i+1)]\begin{align} M &= \sum_{i=1}^{n}v_5(i!) = \sum_{i=1}^{n}\sum_{k=1}^{5^k\le i}\bigg\lfloor\dfrac{i}{5^k}\bigg\rfloor = \sum_{k=1}^{5^k\le n}\sum_{i=1}^{n}\bigg\lfloor\dfrac{i}{5^k}\bigg\rfloor \\ &= \sum_{k=1}^{5^k\le n}\bigg[ 5^k +2\cdot5^k +\dots +\bigg(\bigg\lfloor\dfrac{n}{5^k}\bigg\rfloor-1\bigg)\cdot5^k +\bigg\lfloor\dfrac{n}{5^k}\bigg\rfloor\Big(n\bmod{5^k}+1\Big) \bigg] \\ &= \sum_{k=1}^{5^k\le n}\bigg[\frac{5^k}{2}\bigg\lfloor\dfrac{n}{5^k}\bigg\rfloor\bigg(\bigg\lfloor\dfrac{n}{5^k}\bigg\rfloor-1\bigg) +\bigg\lfloor\dfrac{n}{5^k}\bigg\rfloor\Big(n\bmod{5^k}+1\Big) \bigg] \\ &= \sum_{5\le i=5^k\le n}^{}\bigg[\frac{i}{2}\bigg\lfloor\dfrac{n}{i}\bigg\rfloor\bigg(\bigg\lfloor\dfrac{n}{i}\bigg\rfloor-1\bigg) +\bigg\lfloor\dfrac{n}{i}\bigg\rfloor\Big(n\bmod{i}+1\Big) \bigg] \\ \end{align}

Arithmetic Functions

通俗地讲,input 是 integer 的 function,即 Arithmetic Function。

Completely Multiplicative

  • For all x,yx, y, f(xy)=f(x)f(y)f(x y) = f(x) f(y).
  • f(n)=∏p∣n[f(p)]νp(n)\displaystyle f(n) = \prod_{p \mid n} [f(p)]^{\nu_{p}(n)}.
  • If ff and gg are completely multiplicative, then so are f(nk)f(n^{k}), [f(n)]k[f(n)]^{k}, (f⋅g)(n)=f(n)g(n)(f \cdot g)(n) = f(n)g(n), (f∗g)(n)(f \ast g)(n).

Examples:

  1. Unit Function I(n)=[n=1]I(n) = [n = 1]
    • Identity for Dirichlet convolution, 类比单位矩阵
  2. Power Function id⁡k(n)=nk\operatorname{id}_{k}(n) = n^{k}
    • Constant Function id⁡0(n)=1(n)=1\operatorname{id}_{0}(n) = 1(n) = 1
    • Identity Function id⁡1(n)=n\operatorname{id}_{1}(n) = n

Multiplicative

  • For all x⊥yx \perp y, f(xy)=f(x)f(y)f(x y) = f(x) f(y).
  • f(n)=∏p∣nf(pνp(n))\displaystyle f(n) = \prod_{p \mid n} f(p^{\nu_{p}(n)}).
  • f(pνp(n))≠[f(p)]νp(n)\displaystyle f(p^{\nu_{p}(n)}) \neq [f(p)]^{\nu_{p}(n)}.
  • If ff and gg are multiplicative, then so are f(nk)f(n^{k}), fk(n)f^{k}(n), (f⋅g)(n)=f(n)g(n)(f \cdot g)(n) = f(n)g(n), (f∗g)(n)(f \ast g)(n).

Examples:

  1. Divisor Function σk(n)=∑d∣ndk\displaystyle \sigma_{k}(n) = \sum_{d \mid n} d^{k}
    • 约数个数 (Number of Divisors): τ(n)=σ0(n)=∑d∣n1=∏p∣n(νp(n)+1)\displaystyle \tau(n) = \sigma_0(n) = \sum_{d \mid n} 1 = \prod_{p \mid n} \big(\nu_{p}(n) + 1\big)
    • 约数和 (Sum of Divisors): σ(n)=σ1(n)=∑d∣nd=∏p∣npνp(n)+1−1p−1\displaystyle \sigma(n) = \sigma_1(n) = \sum_{d \mid n} d = \prod_{p \mid n} \frac{p^{\nu_{p}(n)+1} - 1}{p - 1}
  2. Möbius Function μ(n)={1,n=10,∃d>1,d2∣n(−1)ω(n),otherwise\mu(n) = \begin{cases} 1, & n = 1 \\ 0, & \exists d > 1, d^{2} \mid n \\ (-1)^{\omega(n)}, & \text{otherwise} \end{cases}
    • μ=1−1  ⟺  μ∗1=I  ⟺  ∑d∣nμ(d)=[n=1]=I(n)\mu = 1^{-1} \iff \mu * 1 = I \iff \displaystyle \sum_{d \mid n} \mu(d) = [n = 1] = I(n)
    • ∑d∣n,d∣mμ(d)=[n⊥m]\displaystyle \sum_{d \mid n, d \mid m} \mu(d) = [n \perp m]
  3. Euler’s Totient Function φ(n)=∑i=1n[i⊥n]=n∏p∣n(1−1p)\displaystyle \varphi(n) = \sum_{i=1}^n [i \perp n] = n \prod_{p \mid n} \left(1 - \frac{1}{p}\right) — 1∼n1\sim n 中与 nn 互素的数量
    • ∑i=1ni [i⊥n]=nφ(n)2(n>1)\displaystyle \sum_{i=1}^{n} i \ [i \perp n] = \frac{n\varphi(n)}{2} \quad (n > 1) — 1∼n1\sim n 中与 nn 互素的数之和
    • φ=id∗μ  ⟺  φ∗1=id  ⟺  ∑d∣nφ(d)=n\displaystyle \varphi = \text{id} \ast \mu \iff \varphi \ast 1 = \text{id} \iff \sum_{d \mid n} \varphi(d) = n
    • ∑d∣n,d∣mφ(d)=(n,m)\displaystyle\sum_{d \mid n, d \mid m} \varphi(d) = (n, m)

Additive

  • For all x⊥yx \perp y, f(xy)=f(x)+f(y)f(x y) = f(x) + f(y).
  • Example: 不同素因子个数 ω(n)\omega(n),在 n≤109n \le 10^{9} 的范围内,ω(n)\omega(n) 的最大值为 9

Completely Additive

  • For all x,yx, y, f(xy)=f(x)+f(y)f(x y) = f(x) + f(y).
  • Example: 所有素因子个数 (计重) Ω(n)\Omega(n)

Theorems

Fundamental Theorem of Arithmetic (FTA). Every integer greater than 1 can be represented as a product of prime numbers in only one way, apart from the order of the factors.

Prime Number

  1. Prime Number Theorem. lim⁡n→∞π(n)n/ln⁡(n)=1\displaystyle \lim_{n \to \infty} \dfrac{\pi(n)}{n/\ln(n)} = 1
  2. Density of Square-free Integers. The number of square-free integers Q(n)Q(n) up to nn satisfies: lim⁡n→∞Q(n)n=6π2=1ζ(2)\displaystyle \lim_{n \to \infty} \dfrac{Q(n)}{n} = \frac{6}{\pi^{2}} = \frac{1}{\zeta(2)}
  3. Chebyshev’s Theorem. For every integer n>1n > 1, there exists at least one prime pp such that n<p<2nn < p < 2n. (2025 Shenyang C)
  4. Twin Prime Conjecture. There are infinitely many pairs of primes (p,p+2)(p, p+2).
  5. Dirichlet’s Theorem on Arithmetic Progressions. For any two coprime positive integers aa and dd, the arithmetic progression a,a+d,a+2d,a+3d,…a, a+d, a+2d, a+3d, \dots contains infinitely many prime numbers.
  6. Green-Tao Theorem. The set of prime numbers contains arbitrarily long arithmetic progressions a,a+d,a+2d,a+3d,…a, a+d, a+2d, a+3d, \dots

Congruences

  1. Bézout’s Lemma. a,b∈Z  ⟹  a,b \in \mathbb{Z} \implies there exists x,y∈Zx,y \in \mathbb{Z} such that ax+by=gcd⁡(a,b)ax + by = \gcd(a, b). This integer solution can be found using the Extended Euclidean Algorithm (aka. EXGCD).
  2. Fermat’s Little Theorem. p∤a  ⟹  ap−1≡1(modp)p \nmid a \implies a^{p - 1} \equiv 1 \pmod p.
  3. Euler’s Theorem. a⊥n  ⟹  aφ(n)≡1(modn)a \perp n \implies a^{\varphi(n)} \equiv 1 \pmod n.
  4. Extend Euler’s Theorem. ab≡{ab mod φ(n),a⊥naba⊥̸n,b⩽φ(n)ab mod φ(n)+φ(n)a⊥̸n,b>φ(n)(modn)a^{b} \equiv \begin{cases} a^{b \bmod \varphi(n)}, & a \perp n \\ a^{b} & a \not \perp n, b \leqslant \varphi(n) \\ a^{b \bmod \varphi(n) + \varphi(n)} & a \not \perp n, b > \varphi(n) \end{cases} \pmod n
  5. Lemma. x≡x−1(modp)  ⟺  x≡±1x \equiv x^{-1} \pmod p \iff x \equiv \pm 1.
  6. Wilson’s Theorem. p∈N2  ⟹  p∈P  ⟺  (p−1)!≡−1(modp)p \in \mathbb{N}_{2} \implies p \in \mathbb{P} \iff (p-1)! \equiv -1 \pmod p.
  7. 中国剩余定理 (Chinese Remainder Theorem, CRT) Let mim_i be pairwise coprime positive integers. The system of linear congruences x≡ai(modmi)x \equiv a_i \pmod{m_i} has a unique solution modulo M=∏imiM = \prod_{i} m_{i}, given by x≡∑iaiMiMi−1(modM)x \equiv \sum_{i} a_{i} M_{i} M_{i}^{-1} \pmod{M}, where Mi=MmiM_{i} = \dfrac{M}{m_{i} }, and Mi−1M_{i}^{-1} denotes the multiplicative inverse of MiM_{i} modulo mim_{i} (i.e., MiMi−1≡1(modmi)M_{i} M_{i}^{-1} \equiv 1 \pmod{m_{i} }).

Computational Geometry

Distance

two points x=(x1,x2,…,xn)x = (x_{1}, x_{2}, \dots, x_{n}) and y=(y1,y2,…,yn)y = (y_{1}, y_{2}, \dots, y_{n}) in an nn-dimensional space

Minkowski Distance (general)Lp(x,y)=∑i=1n∣xi−yi∣ppManhattan Distance (diamond)L1(x,y)=∑i=1n∣xi−yi∣Euclidean Distance (circle)L2(x,y)=∑i=1n(xi−yi)2Chebyshev Distance (square)L∞(x,y)=lim⁡p→+∞Lp(x,y)=max⁡i∣xi−yi∣\begin{aligned} \text{Minkowski Distance (general)} \qquad & L_{p}(x, y) = \sqrt[p]{\sum_{i=1}^{n} \lvert x_{i} - y_{i} \rvert^{p} } \\ \text{Manhattan Distance (diamond)} \qquad & L_{1}(x, y) = \sum_{i=1}^{n} \lvert x_{i} - y_{i} \rvert \\ \text{Euclidean Distance (circle)} \qquad & L_{2}(x, y) = \sqrt{\sum_{i=1}^{n} (x_{i} - y_{i})^{2} } \\ \text{Chebyshev Distance (square)} \qquad & L_{\infty}(x, y) = \lim_{p \to +\infty} L_{p}(x, y) = \max_{i} \lvert x_{i} - y_{i} \rvert \end{aligned}

Manhattan to Chebyshev

Let u=x+y, v=x−yu=x+y, \ v=x-y, then L1(x,y)=L∞(u,v)L_1(x, y) = L_\infty(u, v).

Proof:

L1(x,y)=∣x1−x2∣+∣y1−y2∣=max⁡{∣(x1+y1)−(x2+y2)∣,∣(x1−y1)−(x2−y2)∣}=max⁡{∣u1−u2∣,∣v1−v2∣}=L∞(u,v)\begin{aligned} L_1(x, y) &= |x_{1} - x_{2}| + |y_{1}- y_{2}| \\ &= \max\{|(x_{1} + y_{1}) - (x_{2} + y_{2})|, |(x_{1} - y_{1}) - (x_{2} - y_{2})|\} \\ &= \max\{|u_{1} - u_{2}|, |v_{1} - v_{2}|\} \\ &= L_\infty(u, v) \end{aligned}

Chebyshev to Manhattan

Let u=x+y2, v=x−y2u=\dfrac{x+y}{2}, \ v=\dfrac{x-y}{2}, then L∞(x,y)=L1(u,v)L_{\infty}(x, y) = L_1(u, v).

Proof.

L∞(x,y)=max⁡{∣x1−x2∣,∣y1−y2∣}=∣x1+y12−x2+y22∣+∣x1−y12−x2−y22∣=∣u1−u2∣+∣v1−v2∣=L1(u,v)\begin{aligned} L_{\infty}(x, y) &= \max\{|x_{1}-x_{2}|, |{y_{1}-y_{2}|}\} \\ &= {\bigg|\dfrac{x_{1} + y_{1} }{2}-\dfrac{x_{2} + y_{2} }{2}\bigg| + \bigg|\dfrac{x_{1} - y_{1} }{2}-\dfrac{x_{2} - y_{2} }{2}\bigg|} \\ &= {|u_{1} - u_{2}| + |v_{1} - v_{2}|} \\ &= L_{1}(u, v) \end{aligned}