数学归纳法
格式:
证明当n n n 的值为初值时的式子成立 (基础( b a s i c ) (basic) ( b a s i c ) )
假设当n = k n = k n = k 时原式成立,即有 : …
则当n = k + 1 n = k + 1 n = k + 1 时,论证式子恒成立 (归纳( i n d u c t i o n ) (induction) ( i n d u c t i o n ) )
e.g. 1
数学归纳法证明等差数列存在a n = a 1 + ( n − 1 ) d a_n = a_1 + (n - 1)d a n = a 1 + ( n − 1 ) d
当n = 2 n = 2 n = 2 时,a 2 = a 1 + d a_2 = a_1 + d a 2 = a 1 + d ,满足定义式: a n = a n − 1 + d a_n = a_{n - 1} + d a n = a n − 1 + d
假设n = k n = k n = k 时,a k = a 1 + ( k − 1 ) d a_k = a_1 + (k - 1)d a k = a 1 + ( k − 1 ) d 成立,则n = k + 1 n = k + 1 n = k + 1 时,a k + 1 = a k + d = a 1 + ( k − 1 ) d + d = a 1 + k d a_{k + 1} = a_k + d = a_1 + (k - 1)d + d = a_1 + kd a k + 1 = a k + d = a 1 + ( k − 1 ) d + d = a 1 + k d
所以当n ⩾ 2 n \geqslant 2 n ⩾ 2 时,a n = a 1 + ( n − 1 ) d a_n = a_1 + (n - 1)d a n = a 1 + ( n − 1 ) d ■ \qquad \blacksquare ■
e.g. 2
数学归纳法证明平面上的n n n 条直线最多界定的区域个数L n = n ( n + 1 ) 2 + 1 , n ⩾ 0 Ln = \dfrac{n(n + 1)}{2} + 1 , n \geqslant 0 L n = 2 n ( n + 1 ) + 1 , n ⩾ 0
存在递推式:
L 0 = 1 L_0 = 1 L 0 = 1
L n = L n − 1 + n , n > 0 L_n = L_{n - 1} + n,n > 0 L n = L n − 1 + n , n > 0
当n = 1 n = 1 n = 1 时,L 1 = 1 + 1 = L 0 + 1 L_1 = 1 + 1 = L_0 + 1 L 1 = 1 + 1 = L 0 + 1 ,满足递推式
假设n = k n = k n = k 时,L k = k ( k + 1 ) 2 + 1 L_k = \dfrac{k(k + 1)}{2} + 1 L k = 2 k ( k + 1 ) + 1 成立,则n = k + 1 n = k + 1 n = k + 1 时,L k + 1 = k ( k + 1 ) 2 + 1 + ( k + 1 ) = ( k + 1 ) ( k + 2 ) 2 + 1 L_{k + 1} = \dfrac{k(k + 1)}{2} + 1 + (k + 1)=\dfrac{(k + 1)(k + 2)}{2} + 1 L k + 1 = 2 k ( k + 1 ) + 1 + ( k + 1 ) = 2 ( k + 1 ) ( k + 2 ) + 1
所以当n ⩾ 1 n \geqslant 1 n ⩾ 1 时,L n = n ( n + 1 ) 2 + 1 , n ⩾ 0 Ln = \dfrac{n(n + 1)}{2} + 1 , n \geqslant 0 L n = 2 n ( n + 1 ) + 1 , n ⩾ 0 ■ \qquad \blacksquare ■
Miller-Rabin
M i l l e r − R a b i n Miller-Rabin M i l l e r − R a b i n 素性检测
前置知识:
二次探测定理
若 x 2 ≡ 1 ( m o d p ) x ^ 2 \equiv 1 \ (mod \ p) x 2 ≡ 1 ( m o d p ) 且 x ≠ 1 x \ne 1 x = 1 且 x ≠ p − 1 x \ne p - 1 x = p − 1 时, p p p 不是质数
p r o o f . proof. p r o o f .
x 2 ≡ 1 ( m o d p ) x ^ 2 \equiv 1 \ (mod \ p) x 2 ≡ 1 ( m o d p ) , p ∣ ( x + 1 ) ( x − 1 ) p \mid (x + 1)(x - 1) p ∣ ( x + 1 ) ( x − 1 )
若 p p p 为素数,则 p ∣ ( x + 1 ) p \mid (x + 1) p ∣ ( x + 1 ) 或 p ∣ ( x − 1 ) p \mid (x - 1) p ∣ ( x − 1 )
解得 x ≡ 1 ( m o d p ) x \equiv 1 \ (mod \ p) x ≡ 1 ( m o d p ) 或 x ≡ − 1 ( m o d p ) x \equiv -1 \ (mod \ p) x ≡ − 1 ( m o d p )
若 p p p 为合数, 则不一定成立, ( e . g . 25 m o d 8 ) (e.g. \ 25 \ mod \ 8) ( e . g . 2 5 m o d 8 )
( x + 1 ) (x + 1) ( x + 1 ) 和 ( x − 1 ) (x - 1) ( x − 1 ) 可能会各有一部分的 p p p 的因子
费马小定理
对于一个素数 p p p , 有 a p − 1 ≡ 1 ( m o d p ) a ^ {p - 1} \equiv 1 \ (mod \ p) a p − 1 ≡ 1 ( m o d p )
结合这两个理论,我们就能够开始我们的 M i l l e r − R a b i n Miller-Rabin M i l l e r − R a b i n 素性检测
可以证明,正确性大概是 1 − 4 − k 1 - 4 ^ {-k} 1 − 4 − k , 其中 k k k 为二次探测的次数
Pollard-Rho
不妨令 n n n 为我们需要质因数分解的数字
先用 M i l l e r − R a b i n Miller-Rabin M i l l e r − R a b i n 算法判断 n n n 是不是素数
然后考虑构建一个随机函数 f ( x ) = x 2 + c f(x) = x ^ 2 + c f ( x ) = x 2 + c , 其中 c c c 为随机常数
然后考虑随机函数值的差 d d d 与 n n n 的最大公约数, 若 ( d , n ) ≠ 1 (d, n) \ne 1 ( d , n ) = 1 , 则找到了公约数,( d , n ) (d, n) ( d , n ) 就是 n n n 的一个约数
其中随机函数在 m o d n mod \ n m o d n 的意义下有可能出现环的情况, 此时用到 f l o y d floyd f l o y d 判环
然后考虑倍增地求 ( d , n ) (d, n) ( d , n ) 的值, 节约时间, 但是考虑到判环的时候有可能多次错过,所以不用 f l o y d floyd f l o y d 判环而是直接用暴力判环即可
欧拉函数
定义
在学欧拉函数和莫比乌斯函数之前,先不妨设n = p 1 q 1 p 2 q 2 ⋯ p k q k n = {p_1}^{q_1} {p_2}^{q_2} \cdots {p_k}^{q_k} n = p 1 q 1 p 2 q 2 ⋯ p k q k
φ ( n ) \varphi(n) φ ( n ) 是欧拉函数,表示 [ 1 , n ] [1, n] [ 1 , n ] 中与 n n n 互质的数的个数
φ ( n ) = n ( 1 − 1 p 1 ) ( 1 − 1 p 2 ) ⋯ ( 1 − 1 p k ) \varphi(n) = n (1 - \dfrac{1}{p_1})(1-\dfrac{1}{p_2})\cdots(1-\dfrac{1}{p_k}) φ ( n ) = n ( 1 − p 1 1 ) ( 1 − p 2 1 ) ⋯ ( 1 − p k 1 )
基本公式
∑ d ∣ n φ ( d ) = n \sum \limits_{d \mid n} \varphi(d) = n d ∣ n ∑ φ ( d ) = n
P r o o f . Proof. P r o o f .
∑ d ∣ n φ ( n ) = ∑ d ∣ n φ ( n d ) = ∑ d ∣ n ∑ i = 1 n d [ g c d ( n d , i ) = 1 ] = ∑ d ∣ n ∑ i = 1 n [ g c d ( n , i ) = d ] = ∑ i = 1 n ∑ d ∣ n [ g c d ( n , i ) = d ] = ∑ i = 1 n 1 = n \sum\limits_{d \mid n}\varphi(n) =\sum\limits_{d \mid n} \varphi(\dfrac{n}{d}) = \sum\limits_{d \mid n} \sum\limits_{i = 1}^\frac{n}{d}[gcd(\dfrac{n}{d}, i) = 1]=\sum\limits_{d \mid n}\sum\limits_{i = 1}^n[gcd(n, i) = d] = \sum\limits_{i = 1}^n\sum\limits_{d \mid n}[gcd(n, i) = d] = \sum\limits_{i = 1}^n1 = n d ∣ n ∑ φ ( n ) = d ∣ n ∑ φ ( d n ) = d ∣ n ∑ i = 1 ∑ d n [ g c d ( d n , i ) = 1 ] = d ∣ n ∑ i = 1 ∑ n [ g c d ( n , i ) = d ] = i = 1 ∑ n d ∣ n ∑ [ g c d ( n , i ) = d ] = i = 1 ∑ n 1 = n
莫比乌斯函数
定义
μ ( n ) \mu(n) μ ( n ) 是莫比乌斯函数, 定义如下
μ ( n ) = { 1 n = 1 ( − 1 ) k m a x ( q 1 , ⋯ , q k ) = 1 0 o t h e r w i s e \mu(n) = \begin{cases}1 & n = 1 \\
(-1)^k & max(q_1, \cdots , q_k) = 1 \\
0 & otherwise
\end{cases} μ ( n ) = ⎩ ⎪ ⎨ ⎪ ⎧ 1 ( − 1 ) k 0 n = 1 m a x ( q 1 , ⋯ , q k ) = 1 o t h e r w i s e
基本公式
∑ d ∣ n μ ( d ) = [ n = 1 ] , ∑ d ∣ n d × μ ( n d ) = φ ( n ) \sum\limits_{d \mid n}\mu(d)=[n = 1], \sum\limits_{d \mid n} d \times \mu(\dfrac{n}{d}) = \varphi(n) d ∣ n ∑ μ ( d ) = [ n = 1 ] , d ∣ n ∑ d × μ ( d n ) = φ ( n )
φ ( n ) = ∑ i = 1 n [ g c d ( i , n ) = 1 ] = ∑ i = 1 n ∑ d ∣ g c d ( i , n ) μ ( d ) = ∑ d ∣ n μ ( d ) n d = ∑ d ∣ n d μ ( n d ) \varphi(n) = \sum\limits_{i = 1}^n[gcd(i, n) = 1] = \sum\limits_{i = 1}^n \sum\limits_{d \mid gcd(i, n)} \mu(d) = \sum\limits_{d \mid n}\mu(d)\dfrac{n}{d} = \sum\limits_{d \mid n} d\mu(\dfrac{n}{d}) φ ( n ) = i = 1 ∑ n [ g c d ( i , n ) = 1 ] = i = 1 ∑ n d ∣ g c d ( i , n ) ∑ μ ( d ) = d ∣ n ∑ μ ( d ) d n = d ∣ n ∑ d μ ( d n )
莫比乌斯反演
若F ( n ) = ∑ d ∣ n f ( d ) F(n) = \sum\limits_{d \mid n} f(d) F ( n ) = d ∣ n ∑ f ( d ) , 则 f ( n ) = ∑ d ∣ n F ( d ) ∗ μ ( n d ) f(n) = \sum\limits_{d \mid n}F(d) * \mu(\dfrac{n}{d}) f ( n ) = d ∣ n ∑ F ( d ) ∗ μ ( d n )
筛法
基本概念
对于任意的两个正整数 a ⊥ b a \bot b a ⊥ b 满足 f ( a b ) = f ( a ) × f ( b ) f(ab) = f(a) \times f(b) f ( a b ) = f ( a ) × f ( b )
对于任意的两个正整数 a a a , b b b 满足 f ( a b ) = f ( a ) × f ( b ) f(ab) = f(a) \times f(b) f ( a b ) = f ( a ) × f ( b )
h ( n ) = ∑ d ∣ n f ( d ) g ( n d ) h(n) = \sum\limits_{d \mid n} f(d) g(\dfrac{n}{d}) h ( n ) = d ∣ n ∑ f ( d ) g ( d n ) , 称 h ( n ) h(n) h ( n ) 是 f ( n ) f(n) f ( n ) 和 g ( n ) g(n) g ( n ) 的狄利克雷卷积
两个积性函数的狄利克雷卷积得到的函数仍然是积性函数
线性筛
线性筛,也称为欧拉筛
大家都会,就不整理了吧
杜教筛
计算一类特定积性函数前缀和的算法,例如φ ( n ) \varphi(n) φ ( n )
∑ i = 1 n ∑ d ∣ i φ ( d ) = ∑ d = 1 n φ ( d ) × ⌊ n d ⌋ = ∑ d = 1 n ∑ i = 1 n d φ ( i ) \sum\limits_{i = 1}^n \sum\limits_{d \mid i} \varphi(d) = \sum\limits_{d = 1}^n\varphi(d)\times \left\lfloor\dfrac{n}{d}\right\rfloor = \sum\limits{d = 1}^n \sum\limits_{i = 1}^\frac{n}{d}\varphi(i) i = 1 ∑ n d ∣ i ∑ φ ( d ) = d = 1 ∑ n φ ( d ) × ⌊ d n ⌋ = ∑ d = 1 n i = 1 ∑ d n φ ( i )
∑ i = 1 n φ ( i ) = ∑ i = 1 n ∑ d ∣ i φ ( d ) − ∑ d = 2 n ∑ i = 1 ⌊ n d ⌋ φ ( i ) = n ( n + 1 ) 2 − ∑ d = 2 n ∑ i = 1 ⌊ n d ⌋ φ ( i ) \sum_{i=1}^{n} \varphi(i)=\sum_{i=1}^{n} \sum_{d \mid i} \varphi(d)-\sum_{d=2}^{n} \sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i)=\frac{n(n+1)}{2}-\sum_{d=2}^{n} \sum_{i=1}^{\left\lfloor\frac{n}{d}\right\rfloor} \varphi(i) ∑ i = 1 n φ ( i ) = ∑ i = 1 n ∑ d ∣ i φ ( d ) − ∑ d = 2 n ∑ i = 1 ⌊ d n ⌋ φ ( i ) = 2 n ( n + 1 ) − ∑ d = 2 n ∑ i = 1 ⌊ d n ⌋ φ ( i )
所以可以从 n n n 递归到 ⌊ n i ⌋ \left\lfloor\frac{n}{i}\right\rfloor ⌊ i n ⌋ ,众所周知总共只有 n \sqrt{n} n 个结果,用记忆化搜索复杂度是 O ( n 3 4 ) O(n^\frac{3}{4}) O ( n 4 3 ) ,如果预处理前 n 2 3 n^\frac{2}{3} n 3 2 的结果再记忆化复杂度就变成 O ( n 2 3 ) O(n^{\frac{2}{3}}) O ( n 3 2 ) 了
e . g . e.g. e . g . 给定一个 n n n , 求 ∑ i = 1 n μ ( i n ) \sum\limits_{i = 1}^n\mu(in) i = 1 ∑ n μ ( i n ) , 其中 1 ⩽ n ⩽ 1 0 9 1 \leqslant n \leqslant 10^9 1 ⩽ n ⩽ 1 0 9
S o l u t i o n : Solution: S o l u t i o n :
记F ( n , m ) = ∑ i = 1 m μ ( i n ) F(n, m) = \sum\limits_{i = 1} ^ m \mu(i n) F ( n , m ) = i = 1 ∑ m μ ( i n ) , 则有:
F ( n , m ) = ∑ i = 1 m μ ( i n ) = ∑ i = 1 m μ ( i ) μ ( n ) [ g c d ( i , n ) = 1 ] = μ ( n ) ∑ i = 1 m μ ( i ) ∑ d ∣ g c d ( i , n ) μ ( d ) F(n, m) = \sum\limits_{i = 1} ^ m \mu(i n) = \sum\limits_{i = 1}^m \mu(i) \mu(n) [gcd(i, n) = 1] = \mu(n) \sum\limits_{i = 1} ^ m\mu(i)\sum\limits_{d \mid gcd(i, n)} \mu(d) F ( n , m ) = i = 1 ∑ m μ ( i n ) = i = 1 ∑ m μ ( i ) μ ( n ) [ g c d ( i , n ) = 1 ] = μ ( n ) i = 1 ∑ m μ ( i ) d ∣ g c d ( i , n ) ∑ μ ( d )
F ( n , m ) = μ ( n ) ∑ d ∣ n μ ( d ) ∑ i = 1 ⌊ m d ⌋ μ ( i d ) = μ ( n ) ∑ d ∣ n μ ( d ) F ( d , ⌊ m d ⌋ ) F(n, m) = \mu(n) \sum\limits_{d \mid n}\mu(d) \sum\limits_{i = 1}^{\left\lfloor\frac{m}{d}\right\rfloor}\mu(id) = \mu(n) \sum\limits_{d \mid n}\mu(d)F(d, {\left\lfloor\dfrac{m}{d}\right\rfloor}) F ( n , m ) = μ ( n ) d ∣ n ∑ μ ( d ) i = 1 ∑ ⌊ d m ⌋ μ ( i d ) = μ ( n ) d ∣ n ∑ μ ( d ) F ( d , ⌊ d m ⌋ )
n = 1 n = 1 n = 1 时用杜教筛求出,递归地去做, 复杂度大概是 O ( n 2 3 ) O(n ^ {\frac{2}{3}}) O ( n 3 2 )
欧几里得算法
g c d ( a , b ) = g c d ( b , a m o d b ) gcd(a, b) = gcd(b, a \ mod \ b) g c d ( a , b ) = g c d ( b , a m o d b )
l c m ( a , b ) = a × b g c d ( a , b ) lcm(a, b) = \dfrac{a \times b}{gcd(a, b)} l c m ( a , b ) = g c d ( a , b ) a × b
裴蜀定理
给定不全为 0 0 0 的整数 a , b a, b a , b , 存在整数 x , y x, y x , y 使得 a x + b y = g c d ( a , b ) ax + by = gcd(a, b) a x + b y = g c d ( a , b )
e . g . C F 510 D F o x A n d J u m p i n g e.g. \ CF510D \ Fox \ And \ Jumping e . g . C F 5 1 0 D F o x A n d J u m p i n g
给出 n n n 张卡片,分别有 l i l_i l i 和 c i c_i c i
在一条无限长的纸带上,你可以选择花 c i c_i c i 的钱来购买卡片 i i i ,从此以后可以向左或向右跳 l i l_i l i 个单位。问你至少花多少元钱才能够跳到纸带上全部位置。若不行,输出 − 1 -1 − 1
S o l u t i o n . Solution. S o l u t i o n .
考虑整张纸带都能走, 那么我们就要使选定的卡片通过互相加减来变成 1 1 1 , 这样我们就能够走完整张纸带
依据裴蜀定理即得: a x + b y = g c d ( a , b ) ax + by = gcd(a, b) a x + b y = g c d ( a , b ) , 当 a ⊥ b a \bot b a ⊥ b 时有 g c d ( a , b ) = 1 gcd(a, b) = 1 g c d ( a , b ) = 1
所以我们只要用 堆优化dijkstra
求解答案即可, 每次枚举 l 1 , l 2 , ⋯ , l n l_1, l_2, \cdots , l_n l 1 , l 2 , ⋯ , l n 来转移到 g c d ( d i s [ u ] , l i ) gcd(dis[u], l_i) g c d ( d i s [ u ] , l i ) 即可
需要注意的是要用 unordered_map
来维护 dis[]
数组
扩展欧几里得算法
常用来求 a x + b y = g c d ( a , b ) ax + by = gcd(a, b) a x + b y = g c d ( a , b ) 的一组可行解
a x 1 + b y 1 = g c d ( a , b ) ax_1 + by_1 = gcd(a, b) a x 1 + b y 1 = g c d ( a , b )
b x 2 + ( a m o d b ) y 2 = g c d ( b , a m o d b ) bx_2 + (a \ mod \ b) y_2 = gcd(b, a \ mod \ b) b x 2 + ( a m o d b ) y 2 = g c d ( b , a m o d b )
∵ g c d ( a , b ) = g c d ( b , a m o d b ) , ∴ a x 1 + b y 1 = b x 2 + ( a m o d b ) y 2 \because gcd(a, b) = gcd(b, a \ mod \ b), \therefore ax_1 + by_1 = bx_2 + (a \ mod \ b) y_2 ∵ g c d ( a , b ) = g c d ( b , a m o d b ) , ∴ a x 1 + b y 1 = b x 2 + ( a m o d b ) y 2
∵ a m o d b = a − ⌊ a b ⌋ × b , a x 1 + b y 1 = b x 2 + ( a − ⌊ a b ⌋ × b ) y 2 \because a \ mod \ b = a - {\left\lfloor\dfrac{a}{b}\right\rfloor} \times b, ax_1 + by_1 = bx_2 + (a - {\left\lfloor\dfrac{a}{b}\right\rfloor} \times b) y_2 ∵ a m o d b = a − ⌊ b a ⌋ × b , a x 1 + b y 1 = b x 2 + ( a − ⌊ b a ⌋ × b ) y 2
展开移项可得:
x 1 = y 2 , y 1 = x 2 − ⌊ a b ⌋ y 2 x_1 = y_2, y_1 = x_2 - {\left\lfloor\dfrac{a}{b}\right\rfloor}y_2 x 1 = y 2 , y 1 = x 2 − ⌊ b a ⌋ y 2
终止条件为 x = 1 , y = 0 x = 1, y = 0 x = 1 , y = 0 , 可以递归求解
类欧几里得算法
使用一个类似于辗转相除法的方法递归地做函数求和的过程
e . g . e.g. e . g . 给定 a , b , c , n a, b, c, n a , b , c , n 求解∑ i = 0 n ⌊ a i + b c ⌋ \sum\limits_{i = 0}^n{\left\lfloor\dfrac{ai + b}{c}\right\rfloor} i = 0 ∑ n ⌊ c a i + b ⌋ , 其中 n < = 1 0 9 n <= 10^9 n < = 1 0 9
S o l u t i o n . Solution. S o l u t i o n .
核心思想是分类讨论然后将情况分开计算
定义 F ( a , b , c , n ) = ∑ i = 0 n ⌊ a i + b c ⌋ F(a, b, c, n) = \sum\limits_{i = 0}^n{\left\lfloor\dfrac{ai + b}{c}\right\rfloor} F ( a , b , c , n ) = i = 0 ∑ n ⌊ c a i + b ⌋
a ⩾ c , b ⩾ c a \geqslant c, b \geqslant c a ⩾ c , b ⩾ c 时,有:
F ( a , b , c , n ) = ∑ i = 0 n ⌊ a i + b c ⌋ = ∑ i = 0 n ⌊ ( ⌊ a c ⌋ × c + a m o d c ) i + ( ⌊ b c ⌋ × c + b m o d c ) c ⌋ = n ( n + 1 ) 2 ⌊ a c ⌋ + ( n + 1 ) ⌊ b c ⌋ + f ( a m o d c , b m o d c , c , n ) F(a, b, c, n) = \sum\limits_{i = 0}^n{\left\lfloor\dfrac{ai + b}{c}\right\rfloor} = \sum\limits_{i = 0}^n{\left\lfloor\dfrac{({\left\lfloor\frac{a}{c}\right\rfloor} \times c + a \ mod \ c)i + ({\left\lfloor\frac{b}{c}\right\rfloor} \times c + b \ mod \ c)}{c}\right\rfloor} = \dfrac{n(n + 1)}{2} {\left\lfloor\dfrac{a}{c}\right\rfloor} + (n + 1){\left\lfloor\dfrac{b}{c}\right\rfloor} + f(a \ mod \ c, b \ mod \ c, c, n) F ( a , b , c , n ) = i = 0 ∑ n ⌊ c a i + b ⌋ = i = 0 ∑ n ⌊ c ( ⌊ c a ⌋ × c + a m o d c ) i + ( ⌊ c b ⌋ × c + b m o d c ) ⌋ = 2 n ( n + 1 ) ⌊ c a ⌋ + ( n + 1 ) ⌊ c b ⌋ + f ( a m o d c , b m o d c , c , n )
中国剩余定理
计算所有模数的积 n n n ;
对于第 i i i 个方程:
计算 m i = n n i m_{i}=\frac{n}{n_{i}} m i = n i n
计算 m i m_{i} m i 在模 n i n_{i} n i 意义下的 逆元 m i − 1 m_{i}^{-1} m i − 1
计算 c i = m i m i − 1 c_{i}=m_{i} m_{i}^{-1} c i = m i m i − 1 (不要对 n i n_{i} n i 取模)
方程组的唯一解为: a = ∑ i = 1 k a i c i ( m o d n ) \quad a=\sum\limits_{i=1}^{k} a_{i} c_{i} \quad(\bmod \ n) a = i = 1 ∑ k a i c i ( m o d n )
P r o o f Proof P r o o f
找们需要证明上面算法计算所得的 a a a 对于任意 i = 1 , 2 , ⋯ , k i=1,2, \cdots, k i = 1 , 2 , ⋯ , k 满足 a ≡ a i ( m o d n i ) ∘ a \equiv a_{i}\left(\bmod \ n_{i}\right) \circ a ≡ a i ( m o d n i ) ∘ 当 i ≠ j i \neq j i = j 时,有 m j ≡ 0 ( m o d n i ) m_{j} \equiv 0\left(\bmod \ n_{i}\right) m j ≡ 0 ( m o d n i ) , 故 c j ≡ m j ≡ 0 ( m o d n i ) ⋅ c_{j} \equiv m_{j} \equiv 0\left(\bmod \ n_{i}\right) \cdot c j ≡ m j ≡ 0 ( m o d n i ) ⋅ 又有
c i ≡ m i ( m i − 1 m o d n i ) ≡ 1 ( m o d n i ) c_{i} \equiv m_{i}\left(m_{i}^{-1} \bmod \ n_{i}\right) \equiv 1\left(\bmod \ n_{i}\right) c i ≡ m i ( m i − 1 m o d n i ) ≡ 1 ( m o d n i ) , 所以我们有 :
a ≡ ∑ j = 1 k a j c j ( m o d n i ) ≡ a i c i ( m o d n i ) ≡ a i m i ( m i − 1 m o d n i ) ( m o d n i ) ≡ a i ( m o d n i ) \begin{aligned}
a & \equiv \sum_{j=1}^{k} a_{j} c_{j} & &\left(\bmod \ n_{i}\right) \\
& \equiv a_{i} c_{i} & &\left(\bmod \ n_{i}\right) \\
& \equiv a_{i} m_{i}\left(m_{i}^{-1} \bmod \ n_{i}\right) & &\left(\bmod \ n_{i}\right) \\
& \equiv a_{i} & &\left(\bmod \ n_{i}\right)
\end{aligned} a ≡ j = 1 ∑ k a j c j ≡ a i c i ≡ a i m i ( m i − 1 m o d n i ) ≡ a i ( m o d n i ) ( m o d n i ) ( m o d n i ) ( m o d n i )
即对于任意 i = 1 , 2 , ⋯ , k i=1,2, \cdots, k i = 1 , 2 , ⋯ , k , 上面算法得到的 a a a 总是满足 a ≡ a i ( m o d n i ) a \equiv a_{i}\left(\bmod n_{i}\right) a ≡ a i ( m o d n i ) , 即证明了解 同余方程组的算法的正确性。
因为我们没有对输入的 a i a_{i} a i 作特殊限制,所以任何一组输人 { a i } \left\{a_{i}\right\} { a i } 都对应一个解 a a a
另外,若 x ≠ y x \neq y x = y , 则总存在 i i i 使得 x x x 和 y y y 在模 n i n_{i} n i 下不同余。
故系数列表 { a i } \left\{a_{i}\right\} { a i } 与解 a a a 之间是一一映射关系,方程组总是有唯一解。
扩展中国剩余定理
考虑两个同余方程的情况
{ x ≡ c 1 ( m o d m 1 ) x ≡ c 2 ( m o d m 2 ) ⋯ x ≡ c r ( m o d m r ) \begin{cases}x \equiv c_1 \ (mod \ m_1) \\
x \equiv c_2 \ (mod\ m_2) \\
\cdots \\
x \equiv c_r \ (mod \ m_r)
\end{cases} ⎩ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎧ x ≡ c 1 ( m o d m 1 ) x ≡ c 2 ( m o d m 2 ) ⋯ x ≡ c r ( m o d m r )
其中 m 1 , m 2 , ⋯ , m r m_1, m_2, \cdots, m_r m 1 , m 2 , ⋯ , m r 存在 m k 1 m_{k1} m k 1 和 m k 2 m_{k2} m k 2 不互质
因为C R T CRT C R T 要求模数互质,所以这个问题我们并不能用 C R T CRT C R T 来做
从简单的入手,我们考虑两个方程的情况
{ x ≡ c 1 ( m o d m 1 ) x ≡ c 2 ( m o d m 2 ) \begin{cases}x \equiv c_1 \ (mod \ m_1) \\
x \equiv c_2 \ (mod\ m_2) \\
\end{cases} { x ≡ c 1 ( m o d m 1 ) x ≡ c 2 ( m o d m 2 )
可以转化为:
x = c 1 + m 1 k 1 x = c_1 + m_1 k_1 x = c 1 + m 1 k 1
x = c 2 + m 2 k 2 x = c_2 + m_2 k_2 x = c 2 + m 2 k 2
所以 c 1 + m 1 k 1 = c 2 + m 2 k 2 c_1 + m_1 k_1 = c_2 + m_2 k_2 c 1 + m 1 k 1 = c 2 + m 2 k 2
移项得 c 2 − c 1 = m 1 k 1 − m 2 k 2 c_2 - c_1 = m_1 k_1 - m_2 k_2 c 2 − c 1 = m 1 k 1 − m 2 k 2
不妨用 ( m 1 , m 2 ) (m_1, m_2) ( m 1 , m 2 ) 来表示 m 1 , m 2 m_1, m_2 m 1 , m 2 的最大公约数
由裴蜀定理得 ( m 1 , m 2 ) ∣ ( c 2 − c 1 ) (m_1, m_2) \mid (c_2 - c_1) ( m 1 , m 2 ) ∣ ( c 2 − c 1 ) , 否则方程无解
方程两边移项后同时除上 ( m 1 , m 2 ) (m_1, m_2) ( m 1 , m 2 )
有 m 1 ( m 1 , m 2 ) k 1 = c 2 − c 1 ( m 1 , m 2 ) + m 2 ( m 1 , m 2 ) k 2 \dfrac{m_1}{(m_1, m_2)}k_1 = \dfrac{c_2 - c_1}{(m_1, m_2)} + \dfrac{m_2}{(m_1, m_2)}k_2 ( m 1 , m 2 ) m 1 k 1 = ( m 1 , m 2 ) c 2 − c 1 + ( m 1 , m 2 ) m 2 k 2
m 1 ( m 1 , m 2 ) k 1 ≡ c 2 − c 1 ( m 1 , m 2 ) ( m o d m 2 ( m 1 , m 2 ) ) \dfrac{m_1}{(m_1, m_2)}k_1 \equiv \dfrac{c_2 - c_1}{(m_1, m_2)} \ (mod \ \dfrac{m_2}{(m_1, m_2)}) ( m 1 , m 2 ) m 1 k 1 ≡ ( m 1 , m 2 ) c 2 − c 1 ( m o d ( m 1 , m 2 ) m 2 )
令 i n v ( a , b ) inv(a, b) i n v ( a , b ) 表示 a a a 在模 b b b 意义下的逆元
k 1 ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × c 2 − c 1 ( m 1 , m 2 ) ( m o d m 2 ( m 1 , m 2 ) ) k_1 \equiv inv(\dfrac{m_1}{(m_1, m_2)}, \dfrac{m_2}{(m_1, m_2)}) \times \dfrac{c_2 - c_1}{(m_1, m_2)} \ (mod \ \dfrac{m_2}{(m_1, m_2)}) k 1 ≡ i n v ( ( m 1 , m 2 ) m 1 , ( m 1 , m 2 ) m 2 ) × ( m 1 , m 2 ) c 2 − c 1 ( m o d ( m 1 , m 2 ) m 2 )
展开得 : k 1 = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) × c 2 − c 1 ( m 1 , m 2 ) + y × m 2 ( m 1 , m 2 ) k_1 = inv(\dfrac{m_1}{(m_1, m_2)}, \dfrac{m_2}{(m_1, m_2)}) \times \dfrac{c_2 - c_1}{(m_1, m_2)} + y \times \dfrac{m_2}{(m_1, m_2)} k 1 = i n v ( ( m 1 , m 2 ) m 1 , ( m 1 , m 2 ) m 2 ) × ( m 1 , m 2 ) c 2 − c 1 + y × ( m 1 , m 2 ) m 2
又因为 x = c 1 + m 1 k 1 x = c_1 + m_1 k_1 x = c 1 + m 1 k 1
所以 x = i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) m 1 ( m 1 , m 2 ) ( c 2 − c 1 ) + m 1 m 2 ( m 1 , m 2 ) y + c 1 x = inv(\dfrac{m_1}{(m_1, m_2)}, \dfrac{m_2}{(m_1, m_2)}) \dfrac{m_1}{(m_1, m_2)}(c_2 - c_1) + \dfrac{m_1 m_2}{(m_1, m_2)}y + c_1 x = i n v ( ( m 1 , m 2 ) m 1 , ( m 1 , m 2 ) m 2 ) ( m 1 , m 2 ) m 1 ( c 2 − c 1 ) + ( m 1 , m 2 ) m 1 m 2 y + c 1
x ≡ i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) m 1 ( m 1 , m 2 ) ( c 2 − c 1 ) + c 1 ( m o d m 1 m 2 ( m 1 , m 2 ) ) x \equiv inv(\dfrac{m_1}{(m_1, m_2)}, \dfrac{m_2}{(m_1, m_2)}) \dfrac{m_1}{(m_1, m_2)}(c_2 - c_1) + c_1 \ (mod \ \dfrac{m_1 m_2}{(m_1, m_2)}) x ≡ i n v ( ( m 1 , m 2 ) m 1 , ( m 1 , m 2 ) m 2 ) ( m 1 , m 2 ) m 1 ( c 2 − c 1 ) + c 1 ( m o d ( m 1 , m 2 ) m 1 m 2 )
这个式子可以理解为 x ≡ c ( m o d M ) x \equiv c \ (mod \ M) x ≡ c ( m o d M )
其中 c = ( ( i n v ( m 1 ( m 1 , m 2 ) , m 2 ( m 1 , m 2 ) ) ( c 2 − c 1 ) ( m 1 , m 2 ) ) m o d m 2 ( m 1 , m 2 ) ) × m 1 + c 1 c = ((inv(\dfrac{m_1}{(m_1, m_2)}, \dfrac{m_2}{(m_1, m_2)}) \dfrac{(c_2 - c_1)}{(m_1, m_2)}) \ mod \ \dfrac{m_2}{(m_1, m_2)}) \times m_1 + c_1 c = ( ( i n v ( ( m 1 , m 2 ) m 1 , ( m 1 , m 2 ) m 2 ) ( m 1 , m 2 ) ( c 2 − c 1 ) ) m o d ( m 1 , m 2 ) m 2 ) × m 1 + c 1
M = m 1 m 2 ( m 1 , m 2 ) M = \dfrac{m_1 m_2}{(m_1, m_2)} M = ( m 1 , m 2 ) m 1 m 2
扩展到多个方程的话就拿现有已考虑过的方程一个一个与未考虑的方程进行计算
费马小定理
对于g c d ( a , p ) = 1 gcd(a, p) = 1 g c d ( a , p ) = 1 , 且p p p 为质数
a p ≡ a ( m o d p ) a ^ p \equiv a \ (mod \ p) a p ≡ a ( m o d p )
P r o o f 1. Proof \ 1. P r o o f 1 .
引理1. 若n ∤ ( a − b ) , x > 0 , g c d ( x , n ) = 1 n \nmid (a - b) , x > 0, gcd(x, n) = 1 n ∤ ( a − b ) , x > 0 , g c d ( x , n ) = 1 , 则n ∤ x ( a − b ) n \nmid x(a - b) n ∤ x ( a − b )
证明: 该结论显然
引理2. 对于集合A = { 1 , ⋯ p − 1 } A = \{1, \cdots {p - 1}\} A = { 1 , ⋯ p − 1 } , 则集合 { k ∈ A , g c d ( a , p ) = 1 , k × a m o d p } \{ k \in A, gcd(a, p) = 1, k \times a \ mod \ p \} { k ∈ A , g c d ( a , p ) = 1 , k × a m o d p } 与 A A A 等价
证明: 对于互异的 k k k 来说, k × a k \times a k × a 在 m o d p mod \ p m o d p 意义下互异, 由引理1可得 ∵ p ∤ ( k 1 − k 2 ) , a > 0 , g c d ( a , p ) = 1 ∴ p ∤ a × ( k 1 − k 2 ) \because p \nmid (k1 - k2),a > 0, gcd(a, p) = 1 \therefore p \nmid a \times (k1 - k2) ∵ p ∤ ( k 1 − k 2 ) , a > 0 , g c d ( a , p ) = 1 ∴ p ∤ a × ( k 1 − k 2 )
由引理2可得 ∵ ∏ i = 1 p − 1 ( i × a ) ≡ ∏ i = 1 p − 1 i × a p − 1 , ∏ i = 1 p − 1 i ≡ ∏ i = 1 p − 1 i × a p − 1 ( m o d p ) \because \prod \limits_{i = 1}^{p - 1}(i \times a) \equiv \prod \limits_{i = 1}^{p - 1}i \times a ^ {p - 1}, \prod \limits_{i = 1}^{p - 1}i \equiv \prod \limits_{i = 1}^{p - 1}i \times a ^ {p - 1} \ (mod \ p) ∵ i = 1 ∏ p − 1 ( i × a ) ≡ i = 1 ∏ p − 1 i × a p − 1 , i = 1 ∏ p − 1 i ≡ i = 1 ∏ p − 1 i × a p − 1 ( m o d p )
∴ a p − 1 ≡ 1 ( m o d p ) \therefore a^{p - 1} \equiv 1 \ (mod \ p) ∴ a p − 1 ≡ 1 ( m o d p )
P r o o f 2. Proof \ 2. P r o o f 2 .
引理1: 对于二项式系数( p n ) \dbinom{p}{n} ( n p ) , 当p p p 为质数且n = 1 ⋯ p − 1 , p ∣ ( p n ) n = 1 \cdots {p - 1}, p \mid \dbinom{p}{n} n = 1 ⋯ p − 1 , p ∣ ( n p )
证明: 当n = 1 ⋯ p − 1 n = 1 \cdots {p - 1} n = 1 ⋯ p − 1 时, ∵ ( p n ) = p ! n ! ( n − p ) ! \because \dbinom{p}{n} = \dfrac{p!}{n!(n-p)!} ∵ ( n p ) = n ! ( n − p ) ! p ! , p p p 最后不会被分母消去, ∴ n = 1 ⋯ p − 1 , p ∣ ( p n ) \therefore n = 1 \cdots {p - 1}, p \mid \dbinom{p}{n} ∴ n = 1 ⋯ p − 1 , p ∣ ( n p )
考虑 ( b + 1 ) p (b + 1) ^ p ( b + 1 ) p 展开
( b + 1 ) p = ( p p ) b p + ( p p − 1 ) b p − 1 + ⋯ + ( p 1 ) b 1 + ( p 0 ) b 0 ≡ ( p p ) b p + ( p 0 ) b 0 ( m o d p ) ≡ b p + 1 ( m o d p ) ≡ ( b − 1 ) p + 1 + 1 ( m o d p ) ≡ ( b − 2 ) p + 1 + 1 + 1 ( m o d p ) ⋯ ≡ 1 + 1 + ⋯ + 1 + 1 ⏟ b + 1 ( m o d p ) ≡ b + 1 ( m o d p ) \begin{aligned}
(b + 1) ^ p & = \dbinom{p}{p} b ^ p + \dbinom{p}{p - 1} b ^ {p - 1} + \cdots + \dbinom{p}{1} b ^ 1 + \dbinom{p}{0} b ^ 0 \\
& \equiv \dbinom{p}{p} b ^ p + \dbinom{p}{0} b ^ 0 & (mod \ p) \\
& \equiv b ^ p + 1 &(mod \ p) \\
& \equiv (b - 1) ^ p + 1 + 1 &(mod \ p)\\
& \equiv (b - 2) ^ p + 1 + 1 + 1 &(mod \ p) \\
& \cdots \\
& \equiv \begin{matrix}\underbrace{1 + 1 + \cdots + 1 + 1}\\b + 1\end{matrix} & (mod \ p) \\
& \equiv b + 1 & (mod \ p)
\end{aligned} ( b + 1 ) p = ( p p ) b p + ( p − 1 p ) b p − 1 + ⋯ + ( 1 p ) b 1 + ( 0 p ) b 0 ≡ ( p p ) b p + ( 0 p ) b 0 ≡ b p + 1 ≡ ( b − 1 ) p + 1 + 1 ≡ ( b − 2 ) p + 1 + 1 + 1 ⋯ ≡ 1 + 1 + ⋯ + 1 + 1 b + 1 ≡ b + 1 ( m o d p ) ( m o d p ) ( m o d p ) ( m o d p ) ( m o d p ) ( m o d p )
令b = a − 1 b = a - 1 b = a − 1 , 则有 a p ≡ a ( m o d p ) a ^ p \equiv a \ (mod \ p) a p ≡ a ( m o d p )
欧拉定理
对于a ⊥ p a \bot p a ⊥ p
a φ ( p ) ≡ 1 ( m o d p ) a ^ {\varphi(p)} \equiv 1 \ (mod \ p) a φ ( p ) ≡ 1 ( m o d p )
扩展欧拉定理
a b ≡ { a b m o d φ ( p ) g c d ( a , p ) = 1 a b g c d ( a , p ) ≠ 1 , b < φ ( p ) ( m o d p ) a φ ( p ) + b m o d φ ( p ) g c d ( a , p ) ≠ 1 , b ⩾ φ ( p ) a^b \equiv \begin{cases}a ^ {b \ mod \ \varphi(p)}&gcd(a, p) = 1 \\
a ^ b & gcd(a, p) \ne 1, b < \varphi(p) & (mod \ p) \\
a ^ {\varphi(p) + b \ mod \ \varphi(p)} & gcd(a, p) \ne 1, b \geqslant \varphi(p)
\end{cases} a b ≡ ⎩ ⎪ ⎨ ⎪ ⎧ a b m o d φ ( p ) a b a φ ( p ) + b m o d φ ( p ) g c d ( a , p ) = 1 g c d ( a , p ) = 1 , b < φ ( p ) g c d ( a , p ) = 1 , b ⩾ φ ( p ) ( m o d p )
威尔逊定理
( p − 1 ) ! ≡ − 1 ( m o d p ) ⟺ P i s a p r i m e . (p - 1)! \equiv -1 \ (mod \ p) \Longleftrightarrow P \ is \ a \ prime. ( p − 1 ) ! ≡ − 1 ( m o d p ) ⟺ P i s a p r i m e .
卢卡斯定理
( n m ) % p = ( n % p m % p ) ( ⌊ n p ⌋ ⌊ m p ⌋ ) % p \dbinom{n}{m} \% p = \dbinom{n \% p}{m \% p} \dbinom{ \left\lfloor\dfrac{n}{p}\right\rfloor }{ \left\lfloor\dfrac{m}{p}\right\rfloor } \% p ( m n ) % p = ( m % p n % p ) ( ⌊ p m ⌋ ⌊ p n ⌋ ) % p
扩展卢卡斯定理
求 ( n m ) m o d p \dbinom{n}{m} \ mod \ p ( m n ) m o d p 的值
不妨令 x = ( n m ) x = \dbinom{n}{m} x = ( m n )
我们可以先对模数进行质因数分解:
p = p 1 k 1 p 2 k 2 p 3 k 3 ⋯ p i k i p = p_1^{k_1}p_2^{k_2}p_3^{k_3} \cdots p_i^{k_i} p = p 1 k 1 p 2 k 2 p 3 k 3 ⋯ p i k i
有同余方程:
{ x ≡ a 1 ( m o d p 1 k 1 ) x ≡ a 2 ( m o d p 2 k 2 ) ⋯ x ≡ a i ( m o d p i k i ) \begin{cases}x \equiv a_1 & (mod \ p_1^{k_1}) \\
x \equiv a_2 & (mod \ p_2^{k_2}) \\
\cdots \\
x \equiv a_i & (mod \ p_i^{k_i})
\end{cases} ⎩ ⎪ ⎪ ⎪ ⎨ ⎪ ⎪ ⎪ ⎧ x ≡ a 1 x ≡ a 2 ⋯ x ≡ a i ( m o d p 1 k 1 ) ( m o d p 2 k 2 ) ( m o d p i k i )
我们可以用 C R T CRT C R T 将上述同余方程合并起来, 于是我们考虑怎么求 a i a_i a i 的值
考虑 n ! m o d p i k i n! \ mod \ p_i^{k_i} n ! m o d p i k i 的求法:
可以观察到有 ⌊ n p i ⌋ ! × p i × 1 × 2 × ⋯ × ( p i − 1 ) × ( p i + 1 ) × ⋯ n \left\lfloor\dfrac{n}{p_i}\right\rfloor! \times p_i \times 1 \times 2 \times \cdots \times (p_i - 1) \times (p_i + 1) \times \cdots n ⌊ p i n ⌋ ! × p i × 1 × 2 × ⋯ × ( p i − 1 ) × ( p i + 1 ) × ⋯ n
然后我们可以可以递归处理这个 ⌊ n p i ⌋ ! \left\lfloor\dfrac{n}{p_i}\right\rfloor! ⌊ p i n ⌋ ! , 至于 p i p_i p i 的话我们考虑提前把次数预处理出来,然后后面再乘即可
除此之外我们可以发现: 在 m o d p i k i mod \ p_i^{k_i} m o d p i k i 的意义下, ∏ i = 1 p i k i − 1 i ≡ ∏ i = p i k i + 1 2 p i k i − 1 i ( m o d p i k i ) \prod\limits_{i = 1}^{p_i^{k_i} - 1} i \equiv \prod\limits_{i = p_i^{k_i} + 1}^{2p_i^{k_i} - 1} i \ (mod \ p_i^{k_i}) i = 1 ∏ p i k i − 1 i ≡ i = p i k i + 1 ∏ 2 p i k i − 1 i ( m o d p i k i )
即只需要预处理一下 ∏ i = 1 p i k i − 1 i \prod\limits_{i = 1}^{p_i^{k_i} - 1} i i = 1 ∏ p i k i − 1 i , 然后快速幂一下就行了
组合数学
关于这个我觉得能够讲一年
基本定义
规定 0 ! = 1 0! = 1 0 ! = 1
C ( n , r ) = P ( n , r ) r ! = n ! ( n − r ) ! r ! C(n, r) = \dfrac{P(n, r)}{r!} = \dfrac{n!}{(n - r)! r!} C ( n , r ) = r ! P ( n , r ) = ( n − r ) ! r ! n !
P ( n , r ) = n ! ( n − r ) ! P(n, r) = \dfrac{n!}{(n - r)!} P ( n , r ) = ( n − r ) ! n !
Q ( n , r ) = P ( n , r ) r = n ! r ( n − r ) ! Q(n, r) = \dfrac{P(n, r)}{r} = \dfrac{n!}{r(n - r)!} Q ( n , r ) = r P ( n , r ) = r ( n − r ) ! n !
基本定理
定理1 - 1 : 过n n n 个有标志顶点的树的数目为n n − 2 n ^ {n - 2} n n − 2 (C a y l e y Cayley C a y l e y 定理)
定理1 - 2 : 在 n n n 个不同元素中取 r r r 个作允许重复的组合, 其组合数为 ( n + r − 1 r ) \dbinom{n + r - 1}{r} ( r n + r − 1 )
定理1 - 3 : 从 A = 1 , 2 , ⋯ , n A = {1, 2, \cdots , n} A = 1 , 2 , ⋯ , n 中取 r r r 个作不相邻的组合, 其组合数为 ( n − r + 1 r ) \dbinom{n - r + 1}{r} ( r n − r + 1 )
定理1 - 4 : 线性方程 x 1 + x 2 + ⋯ + x n = b x_1 + x_2 + \cdots + x_n = b x 1 + x 2 + ⋯ + x n = b 的非负整数解个数为 ( n + b − 1 b ) \dbinom{n + b - 1}{b} ( b n + b − 1 )
组合数奇偶性
( n k ) m o d 2 ≡ { 1 n & k = k 0 o t h e r w i s e \dbinom{n}{k} \ mod \ 2 \equiv
\begin{cases}
1 & {n \& k = k} \\
0 & otherwise
\end{cases} ( k n ) m o d 2 ≡ { 1 0 n & k = k o t h e r w i s e
二项式定理
( x + y ) n = ∑ i = 0 n ( n i ) x i y n − i (x + y) ^ n = \sum\limits_{i = 0}^n\dbinom{n}{i} x^i y ^ {n - i} ( x + y ) n = i = 0 ∑ n ( i n ) x i y n − i
Pascal
公式
( n m ) = ( n − 1 m − 1 ) + ( n − 1 m ) \dbinom{n}{m} = \dbinom{n - 1}{m - 1} + \dbinom{n - 1}{m} ( m n ) = ( m − 1 n − 1 ) + ( m n − 1 )
图论中的组合数学公式
∑ i = u v ( i k ) = ( v + 1 k + 1 ) − ( u k + 1 ) \sum\limits_{i = u}^v \dbinom{i}{k} = \dbinom{v + 1}{k + 1} - \dbinom{u}{k + 1} i = u ∑ v ( k i ) = ( k + 1 v + 1 ) − ( k + 1 u )
简单组合数学公式
( m + n m ) = ( m + n n ) ⟺ ( n r ) = ( n n − r ) \dbinom{m + n}{m} = \dbinom{m + n}{n} \iff \dbinom{n}{r} = \dbinom{n}{n - r} ( m m + n ) = ( n m + n ) ⟺ ( r n ) = ( n − r n )
( n r ) = ( n − 1 r ) + ( n − 1 r − 1 ) \dbinom{n}{r} = \dbinom{n - 1}{r} + \dbinom{n - 1}{r - 1} ( r n ) = ( r n − 1 ) + ( r − 1 n − 1 )
( n + r + 1 r ) = ( n + r r ) + ( n + r − 1 r − 1 ) + ⋯ + ( n 0 ) \dbinom{n + r + 1}{r} = \dbinom{n + r}{r} + \dbinom{n + r - 1}{r - 1} + \cdots + \dbinom{n}{0} ( r n + r + 1 ) = ( r n + r ) + ( r − 1 n + r − 1 ) + ⋯ + ( 0 n )
( n l ) ( l r ) = ( n r ) ( n − r l − r ) , l ⩾ r \dbinom{n}{l} \dbinom{l}{r} = \dbinom{n}{r} \dbinom{n - r}{l - r}, l \geqslant r ( l n ) ( r l ) = ( r n ) ( l − r n − r ) , l ⩾ r
( x + y ) m = ( m 0 ) x m + ( m 1 ) x m − 1 y + ⋯ + ( m m ) y m (x + y) ^ m = \dbinom{m}{0}x^m + \dbinom{m}{1}x^{m - 1}y + \cdots + \dbinom{m}{m}y^m ( x + y ) m = ( 0 m ) x m + ( 1 m ) x m − 1 y + ⋯ + ( m m ) y m
( m 0 ) + ( m 1 ) + ⋯ + ( m m ) = 2 m \dbinom{m}{0} + \dbinom{m}{1} + \cdots + \dbinom{m}{m} = 2^m ( 0 m ) + ( 1 m ) + ⋯ + ( m m ) = 2 m
( n 0 ) − ( n 1 ) + ( n 2 ) − ⋯ ± ( n n ) = 0 \dbinom{n}{0} - \dbinom{n}{1} + \dbinom{n}{2} -\cdots \pm \dbinom{n}{n} = 0 ( 0 n ) − ( 1 n ) + ( 2 n ) − ⋯ ± ( n n ) = 0
( m + n r ) = ( m 0 ) ( n r ) + ( m 1 ) ( n r − 1 ) + ⋯ + ( m r ) ( n 0 ) \dbinom{m + n}{r} = \dbinom{m}{0} \dbinom{n}{r} + \dbinom{m}{1} \dbinom{n}{r - 1} + \cdots + \dbinom{m}{r} \dbinom{n}{0} ( r m + n ) = ( 0 m ) ( r n ) + ( 1 m ) ( r − 1 n ) + ⋯ + ( r m ) ( 0 n )
( m + n m ) = ( m 0 ) ( n 0 ) + ( m 1 ) ( n 1 ) + ⋯ + ( m m ) ( n m ) , m ⩽ n \dbinom{m + n}{m} = \dbinom{m}{0} \dbinom{n}{0} + \dbinom{m}{1} \dbinom{n}{1} + \cdots + \dbinom{m}{m} \dbinom{n}{m}, m \leqslant n ( m m + n ) = ( 0 m ) ( 0 n ) + ( 1 m ) ( 1 n ) + ⋯ + ( m m ) ( m n ) , m ⩽ n
( r r ) + ( r + 1 r ) + ⋯ + ( n r ) = ( n + 1 r + 1 ) \dbinom{r}{r} + \dbinom{r + 1}{r} + \cdots + \dbinom{n}{r} = \dbinom{n + 1}{r + 1} ( r r ) + ( r r + 1 ) + ⋯ + ( r n ) = ( r + 1 n + 1 )
( 2 n n ) = ( n 0 ) 2 + ( n 1 ) 2 + ⋯ + ( n n ) 2 \dbinom{2n}{n} = \dbinom{n}{0} ^ 2 + \dbinom{n}{1} ^ 2 + \cdots + \dbinom{n}{n} ^ 2 ( n 2 n ) = ( 0 n ) 2 + ( 1 n ) 2 + ⋯ + ( n n ) 2
P r n = n P r − 1 n − 1 = ( n − r + 1 ) P r − 1 n = n n − r P r n − 1 P^n_r = n P^{n - 1}_{r - 1} = (n - r + 1) P^n_{r - 1} = \dfrac{n}{n - r}P^{n - 1}_r P r n = n P r − 1 n − 1 = ( n − r + 1 ) P r − 1 n = n − r n P r n − 1
P r n + 1 = P r n + r P r − 1 n = r ! + r ( P r − 1 n + P r − 1 n − 1 + ⋯ + P r − 1 r ) P^{n + 1}_r = P^n_r + rP^n_{r - 1} = r! + r(P^n_{r - 1} + P^{n - 1}_{r - 1} + \cdots + P^r_{r - 1}) P r n + 1 = P r n + r P r − 1 n = r ! + r ( P r − 1 n + P r − 1 n − 1 + ⋯ + P r − 1 r )
∑ k = 1 n k ( n k ) = n 2 n − 1 \sum\limits_{k = 1}^nk\dbinom{n}{k} = n 2 ^ {n - 1} k = 1 ∑ n k ( k n ) = n 2 n − 1
( n + 1 2 ) + 2 ( n + 1 3 ) = ∑ k = 1 n k 2 \dbinom{n + 1}{2} + 2\dbinom{n + 1}{3} = \sum\limits_{k = 1}^n k ^ 2 ( 2 n + 1 ) + 2 ( 3 n + 1 ) = k = 1 ∑ n k 2
( n r ) = n r ( n − 1 r − 1 ) , 1 ⩽ r ⩽ n \dbinom{n}{r} = \dfrac{n}{r} \dbinom{n - 1}{r - 1}, 1 \leqslant r \leqslant n ( r n ) = r n ( r − 1 n − 1 ) , 1 ⩽ r ⩽ n
( n r ) = n − r + 1 r ( n r − 1 ) , 1 ⩽ r ⩽ n \dbinom{n}{r} = \dfrac{n - r + 1}{r} \dbinom{n}{r - 1}, 1 \leqslant r \leqslant n ( r n ) = r n − r + 1 ( r − 1 n ) , 1 ⩽ r ⩽ n
( n r ) = n n − r ( n − 1 r ) , 0 ⩽ r ⩽ n \dbinom{n}{r} = \dfrac{n}{n - r} \dbinom{n - 1}{r}, 0 \leqslant r \leqslant n ( r n ) = n − r n ( r n − 1 ) , 0 ⩽ r ⩽ n
( m 0 ) ( m n ) + ( m 1 ) ( m − 1 n − 1 ) + ⋯ + ( m n ) ( m − n 0 ) = 2 n ( m n ) \dbinom{m}{0}\dbinom{m}{n} + \dbinom{m}{1} \dbinom{m - 1}{n - 1} + \cdots + \dbinom{m}{n} \dbinom{m - n}{0} = 2 ^ n \dbinom{m}{n} ( 0 m ) ( n m ) + ( 1 m ) ( n − 1 m − 1 ) + ⋯ + ( n m ) ( 0 m − n ) = 2 n ( n m )
( n 0 ) 2 n + ( n 2 ) 2 n − 2 + ⋯ + ( n q ) 2 n − 1 = 3 n + 1 2 , q = 2 [ n 2 ] \dbinom{n}{0} 2 ^ n + \dbinom{n}{2} 2 ^ {n - 2} + \cdots + \dbinom{n}{q} 2 ^ {n - 1} = \dfrac{3 ^ n + 1}{2}, q = 2[\dfrac{n}{2}] ( 0 n ) 2 n + ( 2 n ) 2 n − 2 + ⋯ + ( q n ) 2 n − 1 = 2 3 n + 1 , q = 2 [ 2 n ]
原根和阶
阶
若 a ⊥ m a \bot m a ⊥ m , 使 a l ≡ 1 ( m o d m ) a^l \equiv 1 \ (mod \ m) a l ≡ 1 ( m o d m ) 成立的最小的 l l l , 称为 a a a 关于模 m m m 的阶, 记为 o r d m a ord_ma o r d m a
原根
若 g ⊥ m g \bot m g ⊥ m ,若 o r d m g = φ ( m ) ord_mg = \varphi(m) o r d m g = φ ( m ) , 则称 g g g 为 m m m 的一原根
也可表述为 : g g g 为 m m m 的原根当且仅当集合 { g , g 2 , ⋯ , g φ ( m ) } \{ g, g^2, \cdots , g^{\varphi(m)}\} { g , g 2 , ⋯ , g φ ( m ) } 构成模 m m m 的一个既约剩余系
BSGS
可用于求解 a ⊥ p a \bot p a ⊥ p 时,满足 a x ≡ b ( m o d p ) a ^ x \equiv b \ (mod \ p) a x ≡ b ( m o d p ) 的最小的 x x x
我们不妨令 x = A × ⌈ p ⌉ − B x = A \times \left\lceil\sqrt{p}\right\rceil - B x = A × ⌈ p ⌉ − B
则有 a A × ⌈ p ⌉ − B ≡ b ( m o d p ) a ^ {A \times \left\lceil\sqrt{p}\right\rceil - B} \equiv b \ (mod \ p) a A × ⌈ p ⌉ − B ≡ b ( m o d p )
移项得 a A × ⌈ p ⌉ ≡ b × a B ( m o d p ) a ^ {A \times \left\lceil\sqrt{p}\right\rceil} \equiv b \times a ^ B \ (mod \ p) a A × ⌈ p ⌉ ≡ b × a B ( m o d p )
其中 1 ≤ A , B ≤ p 1 \le A, B \le \sqrt{p} 1 ≤ A , B ≤ p , 于是我们可以枚举 B B B 的取值,然后把关于模 p p p 意义下的余数用 map
存下来 ( 或者用Hash_Table
更好 )
然后枚举 A A A , 计算 a A × ⌈ p ⌉ a ^ {A \times \left\lceil\sqrt{p}\right\rceil} a A × ⌈ p ⌉ 在模 p p p 的意义下的余数, 然后再看 map
中有没有对应的 B B B , 如果存在,输出 A × ⌈ p ⌉ − B A \times \left\lceil\sqrt{p}\right\rceil - B A × ⌈ p ⌉ − B 即可
当然该方法能够求解所有满足条件的 x x x
EXBSGS
考虑 g c d ( a , p ) > 1 gcd(a, p) > 1 g c d ( a , p ) > 1 的情况, 此时不满足 b s g s bsgs b s g s 中的互质条件, 考虑怎样转换题目
我们考虑将原来的式子进行转换
a x ≡ b ( m o d p ) ⇒ a x + p × k = b a ^ x \equiv b \ (mod \ p) \Rightarrow a ^ x + p \times k = b a x ≡ b ( m o d p ) ⇒ a x + p × k = b
不妨令 g c d ( a , p ) = d 0 gcd(a, p) = d_0 g c d ( a , p ) = d 0 , 则有 a x d 0 + k d 0 × p = b d 0 \dfrac{a ^ x}{d_0} + \dfrac{k}{d_0} \times p = \dfrac{b}{d_0} d 0 a x + d 0 k × p = d 0 b
然后我们可以继续考虑 d i = g c d ( b ∏ i = 0 i − 1 d i , a ) d_i = gcd(\frac{b}{\prod\limits_{i = 0}^{i - 1}d_{i}}, a) d i = g c d ( i = 0 ∏ i − 1 d i b , a )
不妨令 D = ∏ i = 0 i d i D = \prod\limits_{i = 0}^{i}d_i D = i = 0 ∏ i d i
最后将式子转化成了 a x D + k D × p = b D \dfrac{a ^ x}{D} + \dfrac{k}{D} \times p = \dfrac{b}{D} D a x + D k × p = D b
即 a x D ≡ b D ( m o d p D ) \dfrac{a ^ x}{D} \equiv \dfrac{b}{D} \ (mod \dfrac{p}{D} ) D a x ≡ D b ( m o d D p )
若 D ∤ p D \nmid p D ∤ p , 则该方程无解
此时即可把这个问题转化为 B S G S BSGS B S G S 求解
一些概念及定义
完全剩余系
指 { 0 , 1 , ⋯ , p − 2 , p − 1 } \{ 0, 1, \cdots, p - 2, p - 1 \} { 0 , 1 , ⋯ , p − 2 , p − 1 } 构成的集合称为模 p p p 的一个完全剩余系
既约剩余系
模 p p p 的一个既约剩余系指的是完全剩余系中与 p p p 互质的元素组成的集合