版本比较
校用课件-DP补充
正文
增加 22 行删除 22 行
39 行未改动4041424344444546464748495040 行未改动9192939495959697989940 行未改动14014114214314414414514614714850 行未改动1992002012022032032042052062079 行未改动21721821922022122122222322422540 行未改动26626726826927027027127227327427427527627727827928028028128228328445 行未改动33033133233333433433533633733833934034134234334434434534634734734834935035137 行未改动38939039139239339339439539639739839940040140240240340440540540640740840940 行未改动45045145245345445445545645745845845946046146146246346446541 行未改动50750850951051151151251351351451551651751751851951939 行未改动由于矩阵乘法满足结合律,故一个矩阵自乘若干次可以通过倍增法实现快速幂。::::
### [P3390 【模板】矩阵快速幂](luogu.com.cn/problem/P3390)### [P3390 【模板】矩阵快速幂](https://www.luogu.com.cn/problem/P3390)::::info[参考代码][record](luogu.com.cn/record/231230303)[record](https://www.luogu.com.cn/record/231230303)```cpp#include<bits/stdc++.h>#define ep emplace#define eb emplace_back40 行未改动 return 0;}```::::### [P1962 斐波那契数列](luogu.com.cn/problem/P1962)### [P1962 斐波那契数列](https://www.luogu.com.cn/problem/P1962)
::::info[矩阵乘法的一种特殊形式的意义]{open}我们考虑一个 $1\times n$ 的矩阵 $A$ 乘一个 $n\times n$ 的矩阵 $B$ 得到一个 $1\times n$ 的矩阵 $C$ 的意义。为方便书写,只有一行或一列的矩阵的元素只用一个下标表示。
40 行未改动利用矩阵快速幂加速**各种**递推的方法,被称为**矩阵加速**。
实现的时候为了方便,我们可以考虑把左操作数和结果的 $1\times n$ 的矩阵扩充为 $n\times n$ 的矩阵,只使用其第一行。::::info[参考代码][record](luogu.com.cn/record/241767132)[record](https://www.luogu.com.cn/record/241767132)```cpp#include<bits/stdc++.h>#define ep emplace#define eb emplace_back50 行未改动
进行矩阵乘法时,枚举左/右操作数的每个元素,计算其对答案每个元素的贡献,会比枚举答案的每个元素,找出对应左右操作数,计算它们的乘积会快得多。我给出的参考代码统一采用这种实现方式。
有 $A^0=I$。$A$ 为任意行列数相同的矩阵,$I$ 为行列数与 $A$ 相同的单位矩阵。### [P3758 [TJOI2017] 可乐](luogu.com.cn/problem/P3758)### [P3758 [TJOI2017] 可乐](https://www.luogu.com.cn/problem/P3758)
本题可以通过时间复杂度劣于矩阵加速的做法通过。
我们新建一个点,所有点向这个点连边,将自爆视为走到这个点,再对所有点连一个自环,把停留操作转换为经过这个自环。这样就相当于是机器人每轮都必须移动,从 $1$ 开始,问 $t$ 轮内不同的移动方案个数。9 行未改动若视 $f[i]$ 为矩阵,则有 $f[i]=f[i-1]\times B$。其中 $B[i][j]$ 为 $i\to j$ 的边数。矩阵加速成立。时间复杂度 $O(N^3\log t)$。
进一步总结矩阵加速的一种效用,当进行 DP 时每一层的结果只和前几层有关,且相邻两层的关系总相同,且每一层的状态数量不大,层数较大时,我们考虑使用矩阵加速。::::info[参考代码][record](luogu.com.cn/record/241780710)[record](https://www.luogu.com.cn/record/241780710)```cpp#include<bits/stdc++.h>#define ep emplace#define eb emplace_back40 行未改动 ans = mul(I,ans); ll rs = 0; fr(i,1,s) fr(j,1,s) rs += ans.a[i][j]; printf("%lld\n",rs % MOD); return 0; return 0;}```::::### [P8624 [蓝桥杯 2015 省 AB] 垒骰子](luogu.com.cn/problem/P8624)### [P8624 [蓝桥杯 2015 省 AB] 垒骰子](https://www.luogu.com.cn/problem/P8624)
考虑 DP:令 $i$ 对侧数字为 $o[i]$。令 $f[i][j]$ 表示叠了 $i$ 个骰子,最上面骰子的顶面数字是 $j$ 的方案数,则有每个 $f[i-1][k]$ 对 $f[i][j]$ 作出 $4$ 倍的贡献,当且仅当 $k$ 和 $o[j]$ 可以相邻。做矩阵加速即可。
令 $d=6$,时间复杂度 $O(d^3\log n)$。::::info[参考代码][record](luogu.com.cn/record/241849035)[record](https://www.luogu.com.cn/record/241849035)```#include<bits/stdc++.h>#define ep emplace#define eb emplace_back45 行未改动 return 0;}```::::### [P9777 [HUSTFC 2023] Fujisaki 讨厌数学](luogu.com.cn/problem/P9777)### [P9777 [HUSTFC 2023] Fujisaki 讨厌数学](https://www.luogu.com.cn/problem/P9777)
我们有 $\left(x^i+\frac{1}{x^i}\right)\left(x+\frac{1}{x}\right)=x^{i+1}+\frac{1}{x^{i+1}}+x^{i-1}+\frac{1}{x^{i-1}}$。
令 $F[i]=x^i+\frac{1}{x^i}$,则有 $F[1]F[i-1]=F[i]+F[i-2]$,即 $F[i]=kF[i-1]-F[i-2]$。矩阵加速递推即可。
时间复杂度 $O(\log n)$。
矩阵加速模板建议参考此题代码。::::info[参考代码][record](luogu.com.cn/record/241886199)[record](https://www.luogu.com.cn/record/241886199)```cpp#include<bits/stdc++.h>#define ep emplace #define ep emplace#define eb emplace_back#define ll long long#define ull unsigned ll#define fr(a,b,c) for(int a = b;a <= c;a ++)37 行未改动 return 0;}```::::### [P10503 233 Matrix](luogu.com.cn/problem/P10503)### [P10503 233 Matrix](https://www.luogu.com.cn/problem/P10503)把第一第二下标置换一下。给出 $a_{0,1\cdots n}$,求 $a_{m,n}$。
$a_{i,j}=a_{i-1,j}+a_{i,j-1}$,即 $a_{i,j}=a_{i-1,j}+a_{i-1,j-1}+\cdots+a_{i-1,1}+a_{i,0}$。这里 $a_{i,0}$ 和 $a_{i,j}$ 是同一层级的,不好化,同时发现计算 $a_{i,0}$ 也有困难,因为 $a_{i,0}=10a_{i-1,0}+3$,出现了常数项。
这个时候我们需要在矩阵中加上一个元素 $a_{i,-1}=1$,这样就可以做了,$a_{i,0}=10a_{i-1,0}+3a_{i-1,-1},a_{i,-1}=a_{i-1,-1}$。当然实现的时候肯定不用 $-1$ 做它的下标。
时间复杂度 $O(\sum n^3m)$。::::info[参考代码][record](luogu.com.cn/record/241893046)[record](https://www.luogu.com.cn/record/241893046)```cpp#include<bits/stdc++.h>#define ep emplace #define ep emplace#define eb emplace_back#define ll long long#define ull unsigned ll#define fr(a,b,c) for(int a = b;a <= c;a ++)40 行未改动 return 0;}```::::### [P5337 [TJOI2019] 甲苯先生的字符串](luogu.com.cn/problem/P5337)### [P5337 [TJOI2019] 甲苯先生的字符串](https://www.luogu.com.cn/problem/P5337)
不细讲。和上面垒骰子是一样的。我们用 $f[i][j]$ 表示长度为 $i$ 的以 $j$ 结尾的字符串的个数,用和前题一样形式的矩阵加速即可。令 $\vert\sum\vert=26$,时间复杂度 $O(\vert s_1\vert+\vert\sum\vert^3\log n)$。::::info[参考代码][record](luogu.com.cn/record/241898036)[record](https://www.luogu.com.cn/record/241898036)```cpp#include<bits/stdc++.h>#define ep emplace #define ep emplace#define eb emplace_back#define ll long long#define ull unsigned ll#define fr(a,b,c) for(int a = b;a <= c;a ++)41 行未改动}```::::### 其他题目(经验包)[P2151 [SDOI2009] HH 去散步](luogu.com.cn/problem/P2151)[P2151 [SDOI2009] HH 去散步](https://www.luogu.com.cn/problem/P2151)
[P4159 [SCOI2009] 迷路](luogu.com.cn/problem/P4159)[P4159 [SCOI2009] 迷路](https://www.luogu.com.cn/problem/P4159)
[CF691E Xor-sequences](https://codeforces.com/problemset/problem/691/E)
[P3193 [HNOI2008] GT考试](luogu.com.cn/problem/P3193)[P3193 [HNOI2008] GT考试](https://www.luogu.com.cn/problem/P3193)
[P1357 花园](luogu.com.cn/problem/P1357)[P1357 花园](https://www.luogu.com.cn/problem/P1357)