原根的性质
原根的指数
如果 g g g 是 m m m 的原根,那么 < g , g 1 , g 2 , ⋯ , g φ ( m ) > \displaystyle \left<g, g^1, g^2, \cdots, g^{\varphi(m)} \right> ⟨ g , g 1 , g 2 , ⋯ , g φ ( m ) ⟩ 形成模 m m m 的一个简化剩余系
因为 g φ ( m ) ≡ 1 ( m o d m ) \displaystyle g^{\varphi(m)} \equiv 1(\bmod \ m) g φ ( m ) ≡ 1 ( m o d m ) ,实际上
< 1 , g 1 , g 2 , ⋯ , g φ ( m ) − 1 > \displaystyle \left<1, g^1, g^2, \cdots, g^{\varphi(m)-1} \right> ⟨ 1 , g 1 , g 2 , ⋯ , g φ ( m ) − 1 ⟩ 形成模 m m m 的一个简化剩余系
如果 ( a , m ) = 1 (a, m) = 1 ( a , m ) = 1 ,那么在区间 0 ⩽ k ⩽ φ ( m ) − 1 0 \leqslant k \leqslant\varphi(m) - 1 0 ⩽ k ⩽ φ ( m ) − 1 中有唯一的 k k k ,使得 a ≡ g k ( m o d m ) \displaystyle a \equiv g^k (\bmod \ m) a ≡ g k ( m o d m )
k k k 称为以 g g g 为底,a a a 对模 m m m 的指数,写为 k = ind g ( a ) \displaystyle k = \text{ind}_g(a) k = ind g ( a ) ,也可以写成 ind ( a ) \text{ind}(a) ind ( a )
指数运算
令 g g g 是模 m m m 的一个原根,并且 ( a , m ) = ( b , m ) = 1 (a, m) = (b, m) = 1 ( a , m ) = ( b , m ) = 1 ,那么有
(1) ind ( a b ) ≡ ind ( a ) + ind ( b ) ( m o d φ ( m ) ) \textbf{(1)} \quad \text{ind}(ab) \equiv \text{ind}(a) + \text{ind}(b) (\bmod \ \varphi(m)) (1) ind ( a b ) ≡ ind ( a ) + ind ( b ) ( m o d φ ( m ) )
我们有 g ind ( a ) ≡ a ( m o d m ) , g ind ( b ) ≡ b ( m o d m ) \displaystyle g^{\text{ind}(a)} \equiv a(\bmod \ m), \quad g^{\text{ind}(b)} \equiv b(\bmod m) g ind ( a ) ≡ a ( m o d m ) , g ind ( b ) ≡ b ( m o d m )
由此 α m + a = g ind ( a ) , β m + b = g ind ( b ) \displaystyle \alpha m + a = g^{\text{ind}(a)}, \ \beta m + b = g^{\text{ind}(b)} α m + a = g ind ( a ) , β m + b = g ind ( b )
可以推出 g ind ( a ) + ind ( b ) = ( α m + a ) ( β m + b ) ≡ a b ( m o d m ) ≡ g ind ( a b ) ( m o d m ) g^{\text{ind}(a) + \text{ind}(b)} = (\alpha m + a)(\beta m + b) \equiv ab (\bmod m) \equiv g^{\text{ind}(ab)} (\bmod \ m) g ind ( a ) + ind ( b ) = ( α m + a ) ( β m + b ) ≡ a b ( m o d m ) ≡ g ind ( a b ) ( m o d m )
因为 ind ( a b ) ∈ [ 0 , φ ( m ) − 1 ] \text{ind}(ab) \in [0, \varphi(m) - 1] ind ( a b ) ∈ [ 0 , φ ( m ) − 1 ] ,我们可以有 ind ( a b ) ≡ ind ( a ) + ind ( b ) ( m o d φ ( m ) ) \text{ind}(ab) \equiv \text{ind}(a) + \text{ind}(b) (\bmod \ \varphi(m)) ind ( a b ) ≡ ind ( a ) + ind ( b ) ( m o d φ ( m ) )
或者这么看,ind ( a b ) \text{ind}(ab) ind ( a b ) 和 ind ( a ) + ind ( b ) \text{ind}(a) + \text{ind}(b) ind ( a ) + ind ( b ) 在 φ ( m ) \varphi(m) φ ( m ) 的剩余系中相等即可
(2) ind ( a n ) ≡ n ind ( a ) ( m o d φ ( m ) ) , n ⩾ 1 \textbf{(2)} \quad \text{ind}(a^n) \equiv n \text{ind}(a) \ (\bmod \ \varphi(m)), \ n \geqslant 1 (2) ind ( a n ) ≡ n ind ( a ) ( m o d φ ( m ) ) , n ⩾ 1
同样,( g ind ( a ) ) n = ( a + α m ) n ≡ a n ( m o d m ) ≡ g ind ( a n ) ( m o d m ) \displaystyle \left( g^{\text{ind}(a)} \right)^n = (a + \alpha m)^n \equiv a^{n} (\bmod \ m) \equiv g^{\text{ind}(a^n)} (\bmod \ m) ( g ind ( a ) ) n = ( a + α m ) n ≡ a n ( m o d m ) ≡ g ind ( a n ) ( m o d m )
(3) ind ( 1 ) = 0 , ind ( g ) = 1 \textbf{(3)} \quad \text{ind}(1) = 0, \ \text{ind}(g) = 1 (3) ind ( 1 ) = 0 , ind ( g ) = 1
g ind ( g ) ≡ g ( m o d m ) g^{\text{ind}(g)} \equiv g(\bmod \ m) g ind ( g ) ≡ g ( m o d m ) ,因为 ( g , m ) = 1 (g, m) = 1 ( g , m ) = 1 ,所以有 g ind ( g ) − 1 ≡ 1 ≡ g 0 ( m o d m ) g^{\text{ind}(g) - 1} \equiv 1 \equiv g^0(\bmod \ m) g ind ( g ) − 1 ≡ 1 ≡ g 0 ( m o d m )
在模 m m m 的剩余系中,只能 ind ( g ) − 1 = 0 \text{ind}(g) - 1 = 0 ind ( g ) − 1 = 0
同样 1 ind ( 1 ) ≡ 1 ( m o d m ) 1^{\text{ind}(1)} \equiv 1(\bmod \ m) 1 ind ( 1 ) ≡ 1 ( m o d m ) ,所以 ind ( 1 ) \text{ind}(1) ind ( 1 ) 只能是 0 0 0
(4) ind ( − 1 ) = φ ( m ) 2 , m ⩾ 2 \textbf{(4)} \quad \text{ind}(-1) = \displaystyle \frac{\varphi(m)}{2}, \ m \geqslant 2 (4) ind ( − 1 ) = 2 φ ( m ) , m ⩾ 2
g φ ( m ) 2 ≡ − 1 ( m o d m ) \displaystyle g^{\frac{ \varphi(m) }{2}} \equiv -1 (\bmod \ m) g 2 φ ( m ) ≡ − 1 ( m o d m ) ,我们不妨来求一下 g φ ( m ) / 2 g^{\varphi(m) / 2} g φ ( m ) / 2 ,令 t = φ ( m ) / 2 t = \varphi(m) / 2 t = φ ( m ) / 2
f ( g ) = ( g t ) 2 = g φ ( m ) ≡ 1 ( m o d m ) \displaystyle f(g) = \left(g^{t}\right)^2 = g^{\varphi(m)} \equiv 1(\bmod m) f ( g ) = ( g t ) 2 = g φ ( m ) ≡ 1 ( m o d m )
不妨设 f ( g ) = α m + 1 f(g) = \alpha m + 1 f ( g ) = α m + 1 ,f ( g ) f(g) f ( g ) 为完全平方数 ,它只能是 ( k m + 1 ) 2 (km + 1)^2 ( k m + 1 ) 2 或者是 ( k m − 1 ) 2 (km-1)^2 ( k m − 1 ) 2 的形式
即 g φ ( m ) / 2 = k m + 1 g^{\varphi(m) / 2} = km + 1 g φ ( m ) / 2 = k m + 1 或者 k m − 1 km - 1 k m − 1 ,如果 g φ ( m ) / 2 ≡ 1 ( m o d m ) g^{\varphi(m) / 2} \equiv 1(\bmod \ m) g φ ( m ) / 2 ≡ 1 ( m o d m ) ,与原根定义矛盾
所以只能 g φ ( m ) / 2 ≡ − 1 ( m o d m ) g^{\varphi(m) / 2} \equiv -1(\bmod \ m) g φ ( m ) / 2 ≡ − 1 ( m o d m )
(5) \textbf{(5)} (5) 如果 g ′ g' g ′ 也是模 m m m 的一个原根,那么 ind g ( a ) ≡ ind g ′ ( a ) ⋅ ind g ( g ′ ) ( m o d φ ( m ) ) \text{ind}_g(a) \equiv \text{ind}_{g'}(a) \cdot \text{ind}_g(g') \ (\bmod \varphi(m)) ind g ( a ) ≡ ind g ′ ( a ) ⋅ ind g ( g ′ ) ( m o d φ ( m ) )
因为 g ′ g' g ′ 也是模 m m m 的一个原根,所以有 g ′ = g + k m g' = g + km g ′ = g + k m ,下面来证明 ind g ( g ′ ) = 1 \text{ind}_g(g') = 1 ind g ( g ′ ) = 1
g ind g ( g ′ ) ≡ g ′ ≡ ( g + k m ) ≡ g ( m o d m ) g^{\text{ind}_g(g')} \equiv g' \equiv (g + km) \equiv g(\bmod \ m) g ind g ( g ′ ) ≡ g ′ ≡ ( g + k m ) ≡ g ( m o d m ) ,因为 ( g , m ) = 1 (g, m) = 1 ( g , m ) = 1 ,所以 ind g ( g ′ ) = 1 \text{ind}_g(g') = 1 ind g ( g ′ ) = 1
接下来只需要证明 ind g ( a ) ≡ ind g ′ ( a ) \text{ind}_g(a) \equiv \text{ind}_{g'}(a) ind g ( a ) ≡ ind g ′ ( a ) ,考虑 g ind g ( a ) , g ind g ′ ( a ) \displaystyle g^{\text{ind}_g(a)}, \ g^{\text{ind}_g'(a)} g ind g ( a ) , g ind g ′ ( a )
g ind g ( a ) ≡ a ( m o d m ) , ( g ′ ) ind g ′ ( a ) ≡ a ( m o d m ) \displaystyle g^{\text{ind}_g(a)} \equiv a(\bmod \ m), \ (g')^{\text{ind}_{g'}(a)} \equiv a (\bmod \ m) g ind g ( a ) ≡ a ( m o d m ) , ( g ′ ) ind g ′ ( a ) ≡ a ( m o d m )
二项展开 ( g ′ ) ind g ′ ( a ) = ( g + k m ) ind g ′ ( a ) ≡ g ind g ′ ( a ) ( m o d m ) \displaystyle (g')^{\text{ind}_{g'}(a)} = \left(g+km\right)^{\text{ind}_{g'}(a)} \equiv g^{\text{ind}_{g'}(a)}(\bmod m) ( g ′ ) ind g ′ ( a ) = ( g + k m ) ind g ′ ( a ) ≡ g ind g ′ ( a ) ( m o d m )
a ≡ ( g ′ ) ind g ′ ( a ) ≡ g ind g ′ ( a ) ≡ g ind g ( a ) ( m o d m ) a \equiv (g')^{\text{ind}_g'(a)} \equiv g^{\text{ind}_g'(a)} \equiv g^{\text{ind}_g(a)} (\bmod \ m) a ≡ ( g ′ ) ind g ′ ( a ) ≡ g ind g ′ ( a ) ≡ g ind g ( a ) ( m o d m ) ,证毕
NTT
根据原根理论 ,对于素数 p p p ,如果 < g 1 , g 2 , g 3 , ⋯ , g p − 1 > \left<g^1, g^2, g^3, \cdots, g^{p-1} \right> ⟨ g 1 , g 2 , g 3 , ⋯ , g p − 1 ⟩ 取遍 { 1 , 2 , ⋯ , p − 1 } \{1, 2, \cdots, p-1 \} { 1 , 2 , ⋯ , p − 1 } 的所有整数
那么 g g g 就是关于素数 p p p 的原根
这样,在 FFT 中,用 g n g_n g n 代替 ω n \omega_n ω n ,其中 g n = g p − 1 n \displaystyle g_n = g^{\frac{p-1}{n}} g n = g n p − 1 ,n n n 是 2 2 2 的幂
那么有如下性质
g n n = g p − 1 = g φ ( p ) = 1 \displaystyle g_n^n = g^{p-1} = g^{\varphi(p)} = 1 g n n = g p − 1 = g φ ( p ) = 1
g n n / 2 = g n ( p − 1 ) / 2 ≡ − 1 ( m o d p ) \displaystyle g_n^{n/2} = g_n^{(p-1)/2} \equiv -1 (\bmod \ p) g n n / 2 = g n ( p − 1 ) / 2 ≡ − 1 ( m o d p )
g d n d k = g d k ( p − 1 ) / d n = g k ( p − 1 ) / n = g n k \displaystyle g_{dn}^{dk} = g^{dk(p-1) / dn} = g^{k(p-1) / n} = g_n^{k} g d n d k = g d k ( p − 1 ) / d n = g k ( p − 1 ) / n = g n k
( g n k ) 2 = g n / 2 k \displaystyle \left(g_n^k \right)^2 = g_{n/2}^k ( g n k ) 2 = g n / 2 k
这样 FFT 中所有 ω n \omega_n ω n 有的性质,g n g_n g n 都有
只不过所有的运算要换成模 p p p 意义下的运算 ,1 / n 1/n 1 / n 换成模 p p p 意义下的逆元
剩下的问题就是求原根,常见的模数的原根如下
p = 1004535809 = 479 ⋅ 2 21 + 1 , g = 3 p = 1004535809 = 479 \cdot 2^{21} + 1, \quad g = 3 p = 1 0 0 4 5 3 5 8 0 9 = 4 7 9 ⋅ 2 2 1 + 1 , g = 3
p = 998244353 = 7 ⋅ 17 ⋅ 2 23 + 1 , g = 3 p = 998244353 = 7 \cdot 17 \cdot 2^{23} + 1, \quad g = 3 p = 9 9 8 2 4 4 3 5 3 = 7 ⋅ 1 7 ⋅ 2 2 3 + 1 , g = 3
原根求解算法
假设 g g g 是关于模 P P P 的原根,首先 < 1 , g , g 2 , ⋯ , g φ ( P ) − 1 > \displaystyle \left<1, g, g^2, \cdots, g^{\varphi(P) - 1} \right> ⟨ 1 , g , g 2 , ⋯ , g φ ( P ) − 1 ⟩ 必须取遍 P P P 的简化剩余系
那么 g g g 的取值最坏情况可能是 < 2 , 3 , ⋯ , P − 1 > \displaystyle \left< 2, 3, \cdots, P-1 \right> ⟨ 2 , 3 , ⋯ , P − 1 ⟩ 这么几种
不难想到可以枚举 g g g
如果 g g g 不是 P P P 的原根,那么一定存在 0 ⩽ i < j ⩽ φ ( P ) − 1 0 \leqslant i < j \leqslant \varphi(P) - 1 0 ⩽ i < j ⩽ φ ( P ) − 1 ,我们有 g i ≡ g j ( m o d P ) g^i \equiv g^j (\bmod \ P) g i ≡ g j ( m o d P )
也就是说 g i − j ≡ 1 ( m o d P ) g^{i-j} \equiv 1 (\bmod \ P) g i − j ≡ 1 ( m o d P ) ,不妨令 d = j − i d = j - i d = j − i ,我们有
g d ≡ 1 ( m o d P ) , g φ ( P ) ≡ 1 ( m o d P ) \displaystyle g^{d} \equiv 1 (\bmod \ P), \quad g^{\varphi(P)} \equiv 1(\bmod \ P) g d ≡ 1 ( m o d P ) , g φ ( P ) ≡ 1 ( m o d P ) ,从而有 d ∣ φ ( P ) d \mid \varphi(P) d ∣ φ ( P )
于是我们可以想到对 φ ( P ) \varphi(P) φ ( P ) 进行素因子分解,假设含有的素因子为 < p 1 , p 2 , ⋯ , p k > \displaystyle \left<p_1, p_2, \cdots , p_k \right> ⟨ p 1 , p 2 , ⋯ , p k ⟩
接下来没有必要 检查 φ ( P ) \varphi(P) φ ( P ) 的所有素因子,因为如果我们找到比如说 d = k ⋅ p i d = k \cdot p_i d = k ⋅ p i ,使得 g d ≡ 1 ( m o d P ) \displaystyle g^{d} \equiv 1 (\bmod \ P) g d ≡ 1 ( m o d P )
那么一定有 < g d , g 2 d , g 3 d , ⋯ > ≡ 1 ( m o d P ) \left< g^{d}, g^{2d}, g^{3d}, \cdots \right> \equiv 1(\bmod \ P) ⟨ g d , g 2 d , g 3 d , ⋯ ⟩ ≡ 1 ( m o d P )
所以 g g g 是原根,那么剔除 φ ( P ) \displaystyle \varphi(P) φ ( P ) 的任意素因子,即 d = < φ ( P ) p 1 , φ ( P ) p 2 , ⋯ φ ( P ) p k > \displaystyle d = \left<\frac{\varphi(P)}{p_1}, \frac{\varphi(P)}{p_2}, \cdots \frac{\varphi(P)}{p_k} \right> d = ⟨ p 1 φ ( P ) , p 2 φ ( P ) , ⋯ p k φ ( P ) ⟩
都因该满足 g d ≢ 1 ( m o d P ) g^{d} \not \equiv 1(\bmod \ P) g d ≡ 1 ( m o d P )
实现上,可以先预处理对 φ ( P ) = P − 1 \varphi(P) = P-1 φ ( P ) = P − 1 进行唯一分解,然后枚举 g ∈ [ 2 , P − 1 ] g \in [2, P-1] g ∈ [ 2 , P − 1 ]
对于每一个素因子 < p 1 , p 2 , ⋯ , p k > \left<p_1, p_2, \cdots, p_k \right> ⟨ p 1 , p 2 , ⋯ , p k ⟩ ,如果发现 g φ ( P ) p i ≡ 1 ( m o d P ) \displaystyle g^{\frac{\varphi(P)}{p_i}} \equiv 1(\bmod \ P) g p i φ ( P ) ≡ 1 ( m o d P )
那么标记 g g g 不是原根,检查其他的 g g g ,否则我们就返回 g g g
NTT 算法实现
一些题目
题目1
染色
算法设计
有 n n n 个位置需要染色,可以考虑如下分组,先从 m m m 种颜色中任选 k k k 种,每一种元素是一组,然后剩下的颜色分一组
对于 [ 1 ⋯ k ] [1\cdots k] [ 1 ⋯ k ] 组,每一组放 s s s 个元素,表示 k k k 种颜色,每一种都染了 s s s 个位置
那么剩下的呢?剩下 ( n − k s ) (n - ks) ( n − k s ) 个位置呢?怎么染色?
实际上,剩下的位置可以染剩下的 ( m − k ) (m - k) ( m − k ) 种颜色中的任意一种,这样我们可以得到一个初步的表达式
( m k ) ⋅ ( m − k ) n − k s ⋅ n ! ( s ! ) k ⋅ ( n − k s ) ! \displaystyle \binom{m}{k} \cdot (m-k)^{n-ks} \cdot \frac{n!}{(s!)^{k} \cdot (n - ks)!} ( k m ) ⋅ ( m − k ) n − k s ⋅ ( s ! ) k ⋅ ( n − k s ) ! n !
其中 n ! ( s ! ) k ⋅ ( n − k s ) ! \displaystyle \frac{n!}{(s!)^k \cdot (n - ks)!} ( s ! ) k ⋅ ( n − k s ) ! n ! 表示分组排列的方案数
但是,这并不是我们需要的,题目中要求有 k k k 种颜色恰好出现了 s s s 次
上面的表达式可以保证一定有 k k k 种颜色出现了 s s s 次,但是对于剩下的 ( m − k ) (m - k) ( m − k ) 种颜色呢?
对于未染色的 ( n − k s ) (n - ks) ( n − k s ) 个位置,我们是从剩下的 ( m − k ) (m - k) ( m − k ) 种颜色中任意选取,所以
还可能存在 k k k 种颜色之外的其他颜色 ,也染了 s s s 个位置
上面的表达式计算,求出的出现 s s s 次的颜色种类 ⩾ k \geqslant k ⩾ k 种,不妨记为 f ( k ) f(k) f ( k )
f ( k ) = ( m k ) ⋅ ( m − k ) n − k s ⋅ n ! ( s ! ) k ⋅ ( n − k s ) ! \displaystyle f(k) = \binom{m}{k} \cdot (m-k)^{n-ks} \cdot \frac{n!}{(s!)^{k} \cdot (n - ks)!} f ( k ) = ( k m ) ⋅ ( m − k ) n − k s ⋅ ( s ! ) k ⋅ ( n − k s ) ! n !
f ( k ) f(k) f ( k ) 表示出现 s s s 次的颜色至少 有 k k k 种的方案数
我们需要的是出现 s s s 次的颜色恰好 有 k k k 种的方案,不妨记为 g ( k ) g(k) g ( k ) ,那么出现 s s s 次的颜色最多有多少种呢?
不难想到最多有 N = min ( m , ⌊ n s ⌋ ) \displaystyle N = \min \left(m, \left\lfloor \frac{n}{s} \right\rfloor \right) N = min ( m , ⌊ s n ⌋ )
f ( k ) = ∑ i = k N g ( i ) \displaystyle f(k) = \sum_{i = k}^{N} g(i) f ( k ) = i = k ∑ N g ( i ) ,只可惜这样还是不对
已知出现 s s s 次的颜色恰好有 i i i 种,对应的方案数为 g ( i ) g(i) g ( i ) ,但 f ( k ) f(k) f ( k ) 对应的是
有 k k k 种颜色一定要出现 s s s 次,剩下的没有限制 ,仅仅把 g ( i ) g(i) g ( i ) 加起来会漏解
实际上,对于恰好 有 i i i 种颜色出现 s s s 次,方案数为 g ( i ) g(i) g ( i ) ,那么推 f ( k ) f(k) f ( k ) 时应该这样考虑
i i i 种颜色中 ( i k ) \displaystyle \binom{i}{k} ( k i ) 任意选择 k k k 种颜色一定出现 s s s 次,剩下 ( i − k ) (i-k) ( i − k ) 种颜色出现次数没有限制
f ( k ) = ∑ i = k N g ( i ) ⋅ ( i k ) \displaystyle f(k) = \sum_{i = k}^{N} g(i) \cdot \binom{i}{k} f ( k ) = i = k ∑ N g ( i ) ⋅ ( k i )
接下来考虑如何计算,我们需要知道的是 g ( k ) g(k) g ( k ) ,最后的答案是 ∑ k = 0 N ( g ( k ) ⋅ w k ) \displaystyle \sum_{k = 0}^{N} (g(k) \cdot w_k) k = 0 ∑ N ( g ( k ) ⋅ w k )
f ( k ) = ∑ i = k N ( i k ) ⋅ g ( i ) \displaystyle f(k) = \sum_{i = k}^{N} \binom{i}{k} \cdot g(i) f ( k ) = i = k ∑ N ( k i ) ⋅ g ( i ) ,二项式反演 可以得到
g ( k ) = ∑ i = k N ( − 1 ) i − k ( i k ) ⋅ f ( i ) \displaystyle g(k) = \sum_{i = k}^{N} (-1)^{i - k} \binom{i}{k} \cdot f(i) g ( k ) = i = k ∑ N ( − 1 ) i − k ( k i ) ⋅ f ( i )
化简可得
k ! g ( k ) = ∑ i = k N ( ( − 1 ) i − k ( i − k ) ! ) ( i ! f ( i ) ) \displaystyle k! g(k) = \sum_{i = k}^{N} \left(\frac{(-1)^{i - k}}{(i-k)!} \right) \left(i! f(i) \right) k ! g ( k ) = i = k ∑ N ( ( i − k ) ! ( − 1 ) i − k ) ( i ! f ( i ) )
令 A ( i ) = i ! f ( i ) A(i) = i! f(i) A ( i ) = i ! f ( i ) ,B ( i ) = ( − 1 ) i i ! B(i) = \displaystyle \frac{(-1)^i}{i!} B ( i ) = i ! ( − 1 ) i
那么 g ( k ) = 1 k ! ∑ i = k N A ( i ) B ( i − k ) \displaystyle g(k) = \frac{1}{k!} \sum_{i = k}^{N} A(i)B(i-k) g ( k ) = k ! 1 i = k ∑ N A ( i ) B ( i − k )
下面考虑化简该式
g ( k ) = 1 k ! ∑ k ⩽ i ⩽ N A ( i ) B ( i − k ) g(k) = \displaystyle \frac{1}{k!} \sum_{k \leqslant i \leqslant N} A(i)B(i-k) g ( k ) = k ! 1 k ⩽ i ⩽ N ∑ A ( i ) B ( i − k ) 并不是标准的卷积形式
做变量替换,令 i ′ = i − k i' = i - k i ′ = i − k ,那么变量范围为 0 ⩽ i ′ ⩽ N − k 0 \leqslant i' \leqslant N-k 0 ⩽ i ′ ⩽ N − k
g ( k ) = 1 k ! ∑ 0 ⩽ i ⩽ N − k A ( i + k ) B ( i ) \displaystyle g(k) = \frac{1}{k!} \sum_{0 \leqslant i \leqslant N-k} A(i+k)B(i) g ( k ) = k ! 1 0 ⩽ i ⩽ N − k ∑ A ( i + k ) B ( i ) ,但可惜这也不是标准卷积形式
令 A ( i + k ) = A ′ ( N − k − i ) A(i+k) = A'(N-k-i) A ( i + k ) = A ′ ( N − k − i ) ,这样 g ( k ) = 1 k ! ∑ 0 ⩽ i ⩽ N − k A ′ ( N − k − i ) B ( i ) \displaystyle g(k) = \frac{1}{k!} \sum_{0 \leqslant i \leqslant N-k} A'(N-k-i)B(i) g ( k ) = k ! 1 0 ⩽ i ⩽ N − k ∑ A ′ ( N − k − i ) B ( i )
这就是标准的卷积形式,只要做变换 A ′ ( x ) = A ( N − x ) A'(x) = A(N-x) A ′ ( x ) = A ( N − x ) ,这样就可以得到
g ( k ) = 1 k ! ∑ 0 ⩽ i ⩽ N − k A ′ ( N − k − i ) B ( i ) \displaystyle g(k) = \frac{1}{k!} \sum_{0 \leqslant i \leqslant N-k} A'(N-k-i)B(i) g ( k ) = k ! 1 0 ⩽ i ⩽ N − k ∑ A ′ ( N − k − i ) B ( i ) ,用 NTT \text{NTT} NTT 求卷积
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 int N, n, m, s; int w[maxm]; void solve () { N = min(m, n/s); vector<mint> f(N+1, 0); for (int i = 0; i <= N; i++) f[i] = binom(m, i) * power(mint(m-i), n-s*i) * fac[n] * power(infac[s], i) * (infac[n-s*i]); vector<mint> _a(N+1, 0), _b(N+1, 0); for (int i = 0; i <= N; i++) _a[i] = fac[i] * f[i]; for (int i = 0; i <= N; i++) _b[i] = ( (i & 1) ? (-1) : 1 ) * infac[i]; for (int i = 0; i <= N; i++) if (i < N - i) swap(_a[i], _a[N-i]); Poly A(_a), B(_b); Poly C = A * B; mint ans = 0; for (int k = 0; k <= N; k++) ans = ans + mint(w[k]) * infac[k] * C[N-k]; printf ("%d\n" , ans.x); }
二项式反演的推导(1)
f n = ∑ i = 0 n ( n i ) g i ⇔ g n = ∑ i = 0 n ( − 1 ) n − i ( n i ) f i \displaystyle f_n = \sum_{i = 0}^n \binom{n}{i} g_i \Leftrightarrow g_n = \sum_{i = 0}^{n} (-1)^{n-i} \binom{n}{i} f_i f n = i = 0 ∑ n ( i n ) g i ⇔ g n = i = 0 ∑ n ( − 1 ) n − i ( i n ) f i
这个可以用生成函数来推
f n n ! = ∑ i = 0 n g i i ! 1 ( n − i ) ! \displaystyle \frac{f_n}{n!} = \sum_{i = 0}^{n} \frac{g_i}{i!} \frac{1}{(n - i)!} n ! f n = i = 0 ∑ n i ! g i ( n − i ) ! 1 ,可以知道
F = { f n n ! } \displaystyle F = \left\{\frac{f_n}{n!}\right\} F = { n ! f n } 的 EGF \text{EGF} EGF 为 F i = ∑ i = 0 ∞ f i x i \displaystyle F_i = \sum_{i = 0}^{\infty} f_ix^i F i = i = 0 ∑ ∞ f i x i
G = { g n n ! } \displaystyle G = \left\{\frac{g_n}{n!} \right\} G = { n ! g n } 的 EGF \text{EGF} EGF 为 G i = ∑ i = 0 ∞ g i x i \displaystyle G_i = \sum_{i = 0}^{\infty} g_ix^i G i = i = 0 ∑ ∞ g i x i
根据 e x = ∑ i = 0 ∞ x i i ! , e − x = ∑ i = 0 ∞ ( − 1 ) i x i i ! \displaystyle e^x = \sum_{i = 0}^{\infty} \frac{x^i}{i!}, \quad e^{-x} = \sum_{i = 0}^{\infty} (-1)^i \frac{x^i}{i!} e x = i = 0 ∑ ∞ i ! x i , e − x = i = 0 ∑ ∞ ( − 1 ) i i ! x i
我们有 F = G ⋅ e x F = G \cdot e^{x} F = G ⋅ e x ,由此 G = F ⋅ e − x G = F \cdot e^{-x} G = F ⋅ e − x ,根据生成函数的系数展开
g n n ! = ∑ i = 0 n f i i ! ⋅ ( − 1 ) n − i 1 ( n − i ) ! \displaystyle \frac{g_n}{n!} = \sum_{i = 0}^{n} \frac{f_i}{i!} \cdot (-1)^{n-i} \frac{1}{(n-i)!} n ! g n = i = 0 ∑ n i ! f i ⋅ ( − 1 ) n − i ( n − i ) ! 1 ,证毕
二项式反演的推导(2)
f n = ∑ i = 0 n ( − 1 ) i ( n i ) g i ⟺ g n = ∑ i = 0 n ( − 1 ) i ( n i ) f i \displaystyle f_n = \sum_{i = 0}^{n} (-1)^i \binom{n}{i}g_i \Longleftrightarrow g_n = \sum_{i = 0}^{n} (-1)^i\binom{n}{i}f_i f n = i = 0 ∑ n ( − 1 ) i ( i n ) g i ⟺ g n = i = 0 ∑ n ( − 1 ) i ( i n ) f i
这里使用待定系数的方法来构造
g n = ∑ i = 0 n t ( n , i ) ⋅ f i , t ( n , i ) \displaystyle g_n = \sum_{i = 0}^{n} t(n, i)\cdot f_i, \quad t(n, i) g n = i = 0 ∑ n t ( n , i ) ⋅ f i , t ( n , i ) 为待定系数
f n = ∑ i = 0 n ( − 1 ) i ( n i ) ∑ j = 0 i t ( i , j ) ⋅ f j = ∑ j = 0 n f j ∑ i = j n ( − 1 ) i ( n i ) t ( i , j ) \displaystyle f_n = \sum_{i = 0}^{n} (-1)^i \binom{n}{i} \sum_{j = 0}^{i} t(i, j)\cdot f_j = \sum_{j = 0}^{n} f_j \sum_{i = j}^{n} (-1)^i \binom{n}{i}t(i, j) f n = i = 0 ∑ n ( − 1 ) i ( i n ) j = 0 ∑ i t ( i , j ) ⋅ f j = j = 0 ∑ n f j i = j ∑ n ( − 1 ) i ( i n ) t ( i , j )
令 i ′ = i − j , i = i ′ + j i' = i - j, \quad i = i'+j i ′ = i − j , i = i ′ + j 代换
f n = ∑ j = 0 n f j ∑ i = 0 n − j ( − 1 ) i + j ( n i + j ) t ( i + j , j ) \displaystyle f_n = \sum_{j = 0}^{n} f_j \sum_{i = 0}^{n - j} (-1)^{i+j} \binom{n}{i+j} t(i+j, j) f n = j = 0 ∑ n f j i = 0 ∑ n − j ( − 1 ) i + j ( i + j n ) t ( i + j , j )
注意到 j = n j = n j = n 的时候,f n = f n f_n = f_n f n = f n ,于是我们必须构造出
[ j = n ] = ∑ i = 0 n − j ( − 1 ) i + j ( n i + j ) t ( i + j , j ) (1) \displaystyle [j = n] = \sum_{i = 0}^{n - j} (-1)^{i+j} \binom{n}{i+j} t(i+j, j) \quad \textbf{(1)} [ j = n ] = i = 0 ∑ n − j ( − 1 ) i + j ( i + j n ) t ( i + j , j ) (1)
注意到 ( 1 − 1 ) n (1-1)^n ( 1 − 1 ) n 的展开,有 [ n = 0 ] = ∑ i = 0 n ( − 1 ) i ( n i ) \displaystyle [n = 0] = \sum_{i = 0}^{n}(-1)^i \binom{n}{i} [ n = 0 ] = i = 0 ∑ n ( − 1 ) i ( i n )
[ j = n ] = ∑ i = 0 n − j ( − 1 ) i ( n − j i ) (2) \displaystyle [j = n] = \sum_{i = 0}^{n - j} (-1)^i \binom{n - j}{i} \quad \textbf{(2)} [ j = n ] = i = 0 ∑ n − j ( − 1 ) i ( i n − j ) (2)
(1), (2) \textbf{(1), (2)} (1), (2) 对比,可以知道 t ( i + j , j ) t(i+j, j) t ( i + j , j ) 中有组合数形式,我们配凑
[ j = n ] = ∑ i = 0 n − j ( − 1 ) i ( n − j i ) ( n j ) = ∑ i = 0 n − j ( − 1 ) i ( n n − j ) ( n − j i ) \displaystyle [j = n] = \sum_{i = 0}^{n - j} (-1)^i \binom{n-j}{i} \binom{n}{j} = \sum_{i = 0}^{n - j} (-1)^i \binom{n}{n - j} \binom{n-j}{i} [ j = n ] = i = 0 ∑ n − j ( − 1 ) i ( i n − j ) ( j n ) = i = 0 ∑ n − j ( − 1 ) i ( n − j n ) ( i n − j )
= ∑ i = 0 n − j ( − 1 ) i ( n i ) ( n − i n − j − i ) = ∑ i = 0 n − j ( − 1 ) i ( n i ) ( n − i j ) \displaystyle \quad \quad = \sum_{i = 0}^{n - j} (-1)^i \binom{n}{i} \binom{n-i}{n-j-i} = \sum_{i = 0}^{n - j} (-1)^i \binom{n}{i} \binom{n-i}{j} = i = 0 ∑ n − j ( − 1 ) i ( i n ) ( n − j − i n − i ) = i = 0 ∑ n − j ( − 1 ) i ( i n ) ( j n − i )
其中,我们根据恒等式 ( n i + j ) ( i + j i ) = ( n i ) ( n − i j ) \displaystyle \binom{n}{i+j} \binom{i+j}{i} = \binom{n}{i} \binom{n-i}{j} ( i + j n ) ( i i + j ) = ( i n ) ( j n − i )
所以有 [ j = n ] = ∑ i = 0 n − j ( − 1 ) i ( n i + j ) ( i + j i ) \displaystyle [j = n] = \sum_{i = 0}^{n-j} (-1)^i \binom{n}{i+j} \binom{i+j}{i} [ j = n ] = i = 0 ∑ n − j ( − 1 ) i ( i + j n ) ( i i + j ) ,从而有
t ( i + j , j ) = ( − 1 ) j ⋅ ( i + j i ) = ( − 1 ) j ( i + j j ) \displaystyle t(i+j, j) = (-1)^j \cdot \binom{i+j}{i} = (-1)^j \binom{i+j}{j} t ( i + j , j ) = ( − 1 ) j ⋅ ( i i + j ) = ( − 1 ) j ( j i + j ) ,从而有
t ( i , j ) = ( − 1 ) j ( i j ) \displaystyle t(i, j) = (-1)^j \binom{i}{j} t ( i , j ) = ( − 1 ) j ( j i )
题目2
城市规划