形式幂级数
设数列 a 0 , a 1 , a 2 , ⋯ , a n , ⋯ a_0,a_1, a_2,\cdots,a_n,\cdots a0,a1,a2,⋯,an,⋯ 那么他的形式幂级数就为
∑ i = 0 ∞ a i x i \sum_{i = 0}^{\infty}a_ix^i i=0∑∞aixi
运算:
设 f ( x ) = ∑ i = 0 ∞ a i x i , g ( x ) = ∑ i = 0 ∞ b i x i f(x) = \sum_{i = 0}^{\infty}a_ix^i,g(x) = \sum_{i = 0}^{\infty}b_ix^i f(x)=∑i=0∞aixi,g(x)=∑i=0∞bixi
加法:
f ( x ) + g ( x ) = ∑ i = 0 ∞ ( a i + b i ) x i f(x) + g(x) = \sum_{i = 0}^{\infty}(a_i + b_i)x^i f(x)+g(x)=i=0∑∞(ai+bi)xi
减法:
f ( x ) − g ( x ) = ∑ i = 0 ∞ ( a i − b i ) x i f(x) - g(x) = \sum_{i = 0}^{\infty}(a_i - b_i)x^i f(x)−g(x)=i=0∑∞(ai−bi)xi
乘法:
f ( x ) ∗ g ( x ) = ∑ i = 0 ∞ ∑ k = 0 i a k ∗ b i − k x i f(x) * g(x) = \sum_{i = 0}^{\infty}\sum_{k = 0}^{i}a_k * b_{i - k}x^i f(x)∗g(x)=i=0∑∞k=0∑iak∗bi−kxi
记号:
记形式幂级数 f ( x ) f(x) f(x) 的 x n x^n xn 项系数为 [ x n ] f ( x ) [x^n] f(x) [xn]f(x)
常生成函数
定理:
设 S = a 1 , a 2 , ⋯ , a k S = {a_1,a_2,\cdots,a_k} S=a1,a2,⋯,ak ,且 a i a_i ai 可以取的次数的集合为 M i M_i Mi ,记 F i ( x ) = ∑ u ∈ M i x u F_i(x) = \sum_{u \in M_i}x^u Fi(x)=∑u∈Mixu ,则从 S S S 中取 n n n 个元素组成集合的方案数 g ( n ) g(n) g(n) 的常生成函数 G ( x ) = ∑ i = 0 ∞ g ( i ) x i G(x) = \sum_{i =0}^{\infty}g(i)x^i G(x)=∑i=0∞g(i)xi 满足:
G ( x ) = F 1 ( x ) F 2 ( x ) ⋯ F k ( x ) G(x) = F_1(x)F_2(x) \cdots F_k(x) G(x)=F1(x)F2(x)⋯Fk(x)
形式幂级数的逆元:
A ( x ) B ( x ) = 1 A(x)B(x) = 1 A(x)B(x)=1
逆元存在的条件: [ x 0 ] A ( x ) ≠ 0 [x^0]A(x) \ne 0 [x0]A(x)=0
暴力计算的方法:递推
常见的逆:
∑ i = 0 ∞ x i = 1 1 − x \sum_{i = 0}^{\infty}x^i = \frac{1}{1 - x} i=0∑∞xi=1−x1
∑ i = 0 ∞ a i x i = 1 1 − a x \sum_{i = 0}^{\infty} a^ix^i = \frac{1}{1 - ax} i=0∑∞aixi=1−ax1
∑ i = 0 ∞ C i + k − 1 i x i = 1 ( 1 − x ) k \sum_{ i = 0}^{\infty}C_{i + k - 1}^{i}x^i=\frac{1}{(1 - x)^k} i=0∑∞Ci+k−1ixi=(1−x)k1
例题:
食物
在一个自助水果店,有苹果、香蕉、草莓三种水果,你可以取 n n n 个水果,但是水果店要求,取的苹果数必须是偶数,取的香蕉数必须是 3 3 3 的倍数,取的草莓数不能超过 5 5 5 个。求有多少种取 n n n 个水果的方案。
苹果: A ( x ) = 1 + x 2 + x 4 + ⋯ A(x) = 1+x^2+x^4+\cdots A(x)=1+x2+x4+⋯
香蕉: B ( x ) = 1 + x 3 + x 6 + ⋯ B(x) = 1 + x^3 + x^6+\cdots B(x)=1+x3+x6+⋯
草莓: C ( x ) = 1 + x + x 2 + x 3 + x 4 + x 5 C(x) = 1 + x + x ^2 + x ^3 + x ^ 4 + x ^ 5 C(x)=1+x+x2+x3+x4+x5
那么答案就是 [ x n ] A ( x ) B ( x ) C ( x ) [x^n]A(x)B(x)C(x) [xn]A(x)B(x)C(x)
Devu and Flowers
n n n 种花,分别有 f 1 , f 2 , ⋯ , f n f_1,f_2,\cdots,f_n f1,f2,⋯,fn 个,求取 s s s 朵花的方案数
( 1 ≤ n ≤ 20 , 0 ≤ f i ≤ 1 0 12 , 0 ≤ s ≤ 1 0 14 ) (1 \le n \le 20, 0 \le f_i \le 10^{12}, 0 \le s \le 10^{14}) (1≤n≤20,0≤fi≤1012,0≤s≤1014)
每一朵花的生成函数:
F i ( x ) = 1 + x + x 2 + ⋯ + x f i = 1 − x f i + 1 1 − x F_i(x) = 1 + x + x ^2 + \cdots + x^{f_i} = \frac{1 - x^{f_i + 1}}{1 - x} Fi(x)=1+x+x2+⋯+xfi=1−x1−xfi+1
方案数:
F ( x ) = F 1 ( x ) F 2 ( x ) ⋯ F n ( x ) = ∏ i = 1 n ( 1 − x f i + 1 ) ( 1 − x ) n F(x) = F_1(x)F_2(x)\cdots F_n(x) = \frac{\prod_{i = 1}^{n}(1-x^{f_i + 1})}{(1-x)^n} F(x)=F1(x)F2(x)⋯Fn(x)=(1−x)n∏i=1n(1−xfi+1)
设 A ( x ) = ∏ i = 1 n ( 1 − x f i + 1 ) A(x) = \prod_{i = 1}^{n}(1-x^{f_i + 1}) A(x)=∏i=1n(1−xfi+1)
那么答案就为
[ x s ] F ( x ) = ∑ i = 0 s [ x i ] A ( x ) [ x s − i ] 1 ( 1 − x ) n [x^s]F(x) = \sum_{i = 0}^{s}[x^i]A(x) [x^{ s- i}] \frac{1}{(1 - x)^n} [xs]F(x)=i=0∑s[xi]A(x)[xs−i](1−x)n1
等价于
[ x s ] F ( x ) = ∑ i = 0 s [ x i ] A ( x ) C s − i + n − 1 n − 1 [x^s]F(x) = \sum_{i = 0}^{s}[x^i]A(x) C_{s - i + n - 1}^{n - 1} [xs]F(x)=i=0∑s[xi]A(x)Cs−i+n−1n−1
[CEOI2004] Sweets
n n n 种糖果,别分有 m 1 , m 2 , ⋯ , m n m_1,m_2,\cdots,m_n m1,m2,⋯,mn 个,求取不少于 a a a 不多于 b b b 颗糖果的方案数。
( 1 ≤ n ≤ 10 , 0 ≤ a ≤ b ≤ 1 0 7 , 0 ≤ m i ≤ 1 0 6 ) (1 \le n \le 10, 0 \le a \le b \le 10^7,0 \le m_i \le 10^6) (1≤n≤10,0≤a≤b≤107,0≤mi≤106)
每一个糖果的生成函数:
F i ( x ) = 1 + x + x 2 + ⋯ + x m i = 1 − x m i + 1 1 − x F_i(x) = 1 + x + x ^2 + \cdots + x^{m_i} = \frac{1 - x^{m_i + 1}}{1 - x} Fi(x)=1+x+x2+⋯+xmi=1−x1−xmi+1
方案数:
F ( x ) = F 1 ( x ) F 2 ( x ) ⋯ F n ( x ) = ∏ i = 1 n ( 1 − x m i + 1 ) ( 1 − x ) n F(x) = F_1(x)F_2(x)\cdots F_n(x) = \frac{\prod_{i = 1}^{n}(1-x^{m_i + 1})}{(1-x)^n} F(x)=F1(x)F2(x)⋯Fn(x)=(1−x)n∏i=1n(1−xmi+1)
那么答案就为
∑ s = a b [ x s ] F ( x ) = ∑ s = a b ∑ i = 0 s [ x i ] A ( x ) C s − i + n − 1 n − 1 \sum_{s = a}^{b}[x^s]F(x) = \sum_{s = a}^{b}\sum_{i = 0}^{s}[x^i]A(x) C_{s - i + n - 1}^{n - 1} s=a∑b[xs]F(x)=s=a∑bi=0∑s[xi]A(x)Cs−i+n−1n−1
根据组合数公式
C a j + C a + 1 j + ⋯ + C b j = C b + 1 j + 1 − C a j + 1 C_{a}^{j}+C_{a + 1}^{j} + \cdots+C_{b}^{j} = C_{b + 1}^{j + 1} - C_{a}^{j + 1} Caj+Ca+1j+⋯+Cbj=Cb+1j+1−Caj+1
得到
∑ i = 0 s [ x i ] A ( x ) ( C a − i + n n − C b − i + n − 1 n ) \sum_{i = 0}^{s}[x^i]A(x) (C_{a - i + n}^{n} - C_{b - i+ n - 1}^{n}) i=0∑s[xi]A(x)(Ca−i+nn−Cb−i+n−1n)
指数生成函数
一个数列 { a n } \{a_n\} {an} 对应的指数生成函数为 f ( x ) = ∑ i = 0 ∞ a i x i i ! f(x) = \sum_{i = 0}^{\infty}a_i\dfrac{x_i}{i!} f(x)=∑i=0∞aii!xi
定理:
设 S = a 1 , a 2 , ⋯ , a k S = {a_1,a_2,\cdots,a_k} S=a1,a2,⋯,ak ,且 a i a_i ai 可以取的次数的集合为 M i M_i Mi ,记 F i ( x ) = ∑ u ∈ M i x u u ! F_i(x) = \sum_{u \in M_i}\dfrac{x^u}{u!} Fi(x)=∑u∈Miu!xu ,则从 S S S 中取 n n n 个元素排成一列的方案数 g ( n ) g(n) g(n) 的指数生成函数 G ( x ) = ∑ i = 0 ∞ g ( i ) x i i ! G(x) = \sum_{i =0}^{\infty}g(i)\dfrac{x^i}{i!} G(x)=∑i=0∞g(i)i!xi 满足:
G ( x ) = F 1 ( x ) F 2 ( x ) ⋯ F k ( x ) G(x) = F_1(x)F_2(x) \cdots F_k(x) G(x)=F1(x)F2(x)⋯Fk(x)
常见公式:
∑ i = 0 ∞ x i i ! = e x \sum_{i = 0}^{\infty}\frac{x^i}{i!} = e^x i=0∑∞i!xi=ex
∑ i = 0 ∞ a i x i i ! = e a x \sum_{i = 0}^{\infty}a^i\frac{x^i}{i!} = e^{ax} i=0∑∞aii!xi=eax
例题:
blocks
一段长度为 n n n 的序列,你有红黄蓝绿 4 4 4 种颜色的砖块,一块砖长度为 1 1 1,问你铺砖的方案数,其中红色砖和绿色砖的数量必须为偶数
答案可能很大,请输出 m o d 10007 \bmod 10007 mod10007 后的结果
设
F ( x ) = 1 + x 2 2 ! + x 4 4 ! + ⋯ = e x + e − x 2 F(x) = 1 + \dfrac{x^2}{2!} + \dfrac{x^4}{4!} +\cdots = \dfrac{e^{x}+e^{-x}}{2} F(x)=1+2!x2+4!x4+⋯=2ex+e−x
G ( x ) = 1 + x + x 2 2 ! + x 3 3 ! + ⋯ = e x G(x) = 1 + x+\dfrac{x^2}{2!} + \dfrac{x^3}{3!} +\cdots = e^x G(x)=1+x+2!x2+3!x3+⋯=ex
则答案为
n ! × [ x n ] F 2 ( x ) G 2 ( x ) = n ! × [ x n ] e 4 x + 2 e 2 x + 1 4 = 4 n + 2 × 2 n 4 = 4 n − 1 + 2 n − 1 n!\times[x^n]F^2(x)G^2(x) = n!\times[x^n]\frac{e^{4x}+2e^{2x} + 1}{4}=\frac{4^n+2\times2^n}{4} = 4^{n - 1} + 2^{n - 1} n!×[xn]F2(x)G2(x)=n!×[xn]4e4x+2e2x+1=44n+2×2n=4n−1+2n−1