8. Advanced Counting Techniques
(这也不怎么advanced啊(笑))
本课程作业需要写出具体数字,但在考试中只要用C(m,n)表示即可
Recurrence Relations
Definition
- recurrence an=f(a1,…,an−1)
- initial conditions. a0,a1…. to fixed the sequence
- degree: number of initial conditions. an=an−1+an−8(degree=8)
Example Problems
Let an be the number of valid string of decimal digits containing an even number of 0 digits.
-
sn is not zero, then s[1,n−1] must be a valid string, 9sn−1
-
sn is zero, then s[1,n−1] must contain odd number of 0. from the opposite side, 10n−1−an−1
$$
a_n=8a_{n-1}+10^{n-1}
$$
The number of bit strings of length n that contain 3 consecutive 0s
这些题本来用几个数组的dp挺好做的,但强行只能用一个数列就需要一些技巧。
技巧1:这些题往往要枚举一段符合条件的序列的长度,后面任选
考虑最后位数
- 000,前面任选2n−3
- 1, an−1
- 10,an−2
- 100,an−3
the number of ternary strings that do not contain two consecutive 0’s or and consecutive 1’s
- sn=2: an−1
- 枚举0101的长度k, 前面要不是1010,要不是0101,2种,且长度至少为2。 所以∑k=0n−22ak
所以an=an−1+2an−2+…2a0
再写第n-1项作差: an−1=an−2+2an−3+…2a0
得到an=2an−1+an−2
初始条件a0=1,a1=3
Solving Linear Recurrence Relations
Linear Homogeneous Recurrence Relations with Constant Coefficients
characteristic equation
characteristic roots
etc. r1=r2, p1r1n+p2r2n
r1=r2, (p1+p2n)r1n
总结 m重根r的待定系数(p1+p2n+…pmnm−1)rn

Linear Nonhomogeneous Recurrence Relations with Constant Coefficients
an=c1an−1+…ckan−k+f(n)
associated homogeneous recurrence relation
an=c1an−1+…ckan−k
Every solution is the sum of a particular solution and a solution of the associated linear homogeneous recurrence relation.
证明类似线性代数里解非齐次线性方程组的思想。AX0=c叠加上AX=0,得到A(X+X0)=c.
However,finding the particular solution is difficult. But for some certain type of f(n), there is a easy solution.

总结:解非齐次线性递推式的过程
-
写出对应的齐次递推数列,并求其通解
∑(c0+c1n+cmi−1nmi−1)rin (ri是mi重特征根)
-
写出一个特解的待定系数。形式与f(n)类似. 设f(n)=q(n)sn (若f(n)是多项式,则s=1)
- 若s不是特征根,那么特解是p(n)sn. 其中p(n)=p0+p1n+…和q(n)的次数相同
- 若s是特征根(r重), 那么特解是nrp(n)sn
-
把特解代入原递推式并求出系数pt,pt−1…p0.
-
把通解和特解加在一起(所以最后的结果里面特解是常系数的,只有通解带上k1,k2的系数). 如果有初始条件,再确定通解的系数。
etc
一个易错的情况是F(n)=多项式. 如an=an−1+n. 对应特征方程的解是r=c是任意实数. 1显然是特征方程的根, 所以待定系数是n(p1n+p2)

c2n+n(pn+q)
Generating Functions
Definition and operation
Sequences->Functions(of power series)
Ordinary generating function of {an}: G(x)=∑k=0∞akxk (0-index)
(根据数列求生成函数,就是对幂级数求和函数,微积分里已经讲的很清楚)
an=1, G(x)=∑k≥0xk=1−x1
an=n, G(x)=k≥0∑kxk=xk≥0∑kxk−1=x(1−x1)′=(1−x)2x
an=n+1,G(x)=∑k≥0(k+1)xk=(∑k≥1xk)′=(1−xx)′=(1−x)21
Application of Products:
Eg. {ak} has the generating function A(x), find the generating function of bn=∑k=1nak
bn=∑k=1nak=∑k=1nakcn−k(ck≡1)
B(x)=1−xA(x)
b0=a02,b1=2a1a0,b2=a12+2a0a2…,B(x)=A2(x)
注意有限项的生成函数,如(1+x)n
k=0∑m−1xk=1−x1−xm
Eg. an=C(10,n+1). ∑n=0∞C(10,n+1)xn=x1∑n=1∞C(10,n)xn=x(1+x)10−1
根据数列求生成函数
- 记住常见生成函数,如等比。
- 利用泰勒展开
- 逐项积分/求导
- 生成函数的组合意义
根据生成函数求数列: 泰勒展开,常见的结论,还要对式子做因式分解等。如1−4x21=1+2x1+1−2x1
- 本质是把函数展开成幂级数后xn的系数
- 提公因数,拆成xmf(x),其中f(x)是比较好求的形式,如(1−x)m1
- 指数比较小的直接枚举. 注意如果指数大于n的项就直接舍去
an=n+1, f(x)=(1−x)21
法一: ∑n≥0(n+1)xn=(∑n≥0xn+1)′=(1−x)21
法二(卷积): bn=1, an=∑k=0nbkbn−k.
an=C(m+n−1,m−1),f(x)=(1−x)m1
Extended binomial Theorem(本质是(1+x)a的泰勒级数) 根据
(1+x)a=k=0∑∞k!a(a−1)…(a−k+1)xk=k=0∑∞(ka)xk(a∈R)
展开(1+x)−n即可
广义二项式定理的变式: an=C(n,2)
f(x)=∑n≥2C(n,2)xn−=x2∑n≥0C(n+2,2)xn=(1−x)3x2
Eg. 

Solve Counting Problems
xk (k is the "size" of element)
Divide n indistinguishable objects into k distinguishable boxes(boxes can be empty)
(1+x+x2+…)(1+x+x2+…)⋯=(1−x1)k. Find the coefficient of xn
Divide n indistinguishable objects into k distinguishable boxes(boxes can not be empty)
(x+x2+…)k=(1−x)kxk. 没有xk的话xn的系数是C(n+k−1,k−1). 乘了xk,要把n换成n-k,所以是C(n−1,k−1)
生成函数的好处是可以更灵活的处理限制
- 如限制盒子内的物体数量 (xa+xa+1+…xb)
- 一定要熟悉[xn](1−x)m1=C(n+m−1,)
!!!2r red balls, 2r blue balls ,2r white balls. How many ways to select 3r balls?
(1+x+…x2r)3=(1−x)3(1−x2r+1)3
=(1−x)31−3x2r+1+(1−x)33x4r+2−x6r+3
后面两项次数至少4r+2>3r不考虑. 第一项中x3r系数C(3r+3−1,3−1). 第二项中因为系数平移−3C((3r−(2r+1))+2,2)
因此答案是C(3r+2,2)−3C(r+1,2)
$1,$2,$5
to pay $r
the order matters
(etc. 3=1+2=2+1) ∑k=0∞(x+x2+x5)=1−x−x2−x51
Solve recurrence relations
好处: 不用区分齐次/非齐次
- 乘上xn,对方程两边求和.并把右边整理成关于A(x)和x的式子. 注意求和的时候需要让an−k有意义,所以n至少要从k开始
- 解出A(x)此时应该是封闭形式
- 将封闭形式展开成幂级数形式,第n项系数即为答案


Prove combinatorial identities
Inclusion-Exclusion Principle

捆绑法。注意两个字符串不能有字母相同
Strling number of the second kind
n个不同元素分到m个不可区分的集合里,每个集合非空
S(n,m)=m!1k=0∑m(−1)kCmk(m−k)n
S(r,2)=22n−2S(r,r−1)=C(r,2)S(r+1,n)=S(r,n−1)+nS(r,n)
变式:
- 如果盒子是不同的,就不用除以m!
- 如果允许盒子为空,那么就是∑k=1mS(n,k)
In how many ways can seven different jobs be assigned
to four different employees so that each employee is assigned at least one job and the most difficult job is assigned to the best employee?
如果没有最后一个限制,答案就是4!S(7,4)
考虑most difficult job分别给到4个员工的方案数是相同的,因此答案就是4!S(7,4)/4
Derangements
(错排问题)
A derangement is a permutation of objects that leaves no object in its original position
Dn=n!k=0∑nk!(−1)k
枚举固定的k个元素,其他元素的排法就是C(n,k)(n−k)!=k!n!
性质:
limn→∞n!Dn=e1
Dn=(n−1)(Dn−1+Dn−2)
1不能在第一个位置,设排列第一个位置是数字k,有n-1种取法。
-
如果1到了第k个位置,那么剩下n-2个位置错排即可,Dn−2
-
如果1不在第k个位置,那么就可以看成位置2到n的数的错排(因为1不能放在位置k,所以就相当于1变成了k)
变式: 用错排的思想,固定位置
arran0,1,2,3,4,5, no odd digit is in its original position
枚举有几个奇数在原来的位置, 6!−C(3,1)5!+C(3,2)4!−C(3,3)3!