矩阵快速幂与矩阵设计入门
ZMQ_Ink65536
·
·
个人记录
矩阵快速幂与矩阵设计入门
矩阵快速幂
I have 矩阵!
I have 快速幂!
I have 矩阵快速幂!
\mathcal O(n^3 \log k)
矩阵设计
- 确定状态向量
- 写出一阶线性更新方程组
- 提取系数填入矩阵
典例 1:f(x) = f(x - 1) + f(x - 2)
状态 v_t = \begin{bmatrix}f_t \\ f_{t - 1}\end{bmatrix},目标 v_{t + 1} = \begin{bmatrix} f_{t + 1} \\ f_t \end{bmatrix}。
方程组:
\begin{cases} f_{t + 1} = {\color{Red} 1} \times f_t + {\color{Red} 1} \times f_{t - 1} \\ f_{t} = {\color{Red} 1} \times f_{t} + {\color{Red} 0} \times{f_{t - 1}} \end{cases}
填入矩阵:
\begin{bmatrix} f_{t + 1} \\ f_t \end{bmatrix} = \begin{bmatrix} 1 \ 1 \\ 1 \ 0 \end{bmatrix} \begin{bmatrix} f_t \\ f_{t - 1} \end{bmatrix}
进阶 f(x) = f(x - 1) + f(x - 2) + 3,求 \sum f(x)
加入 s_t 表示前缀和,1_t 表示常数。
状态向量 v_t = \begin{bmatrix} s_t \\ f_t \\ f_{t - 1} \\ 1\end{bmatrix},目标向量 v_{t + 1} = \begin{bmatrix} s_{t + 1} \\ f_{t + 1} \\ f_t \\ 1 \end{bmatrix}。
方程组:
\begin{cases} s_{t + 1} = {\color{Red} 1} \times s_t + {\color{Red} 1} \times f_t + {\color{Red} 1} \times f_{t - 1} + {\color{Red} 3} \times 1 \\ f_{t + 1} = {\color{Red} 0} \times s_t + {\color{Red} 1} \times f_t + {\color{Red} 1} \times f_{t - 1} + {\color{Red} 3} \times 1 \\ f_t = {\color{Red} 0} \times s_t + {\color{Red} 1} \times f_t + {\color{Red} 0} \times f_{t - 1} + {\color{Red} 0} \times 1 \\ 1 = {\color{Red} 0} \times s_t + {\color{Red} 0} \times f_t + {\color{Red} 0} \times f_{t - 1} + {\color{Red} 1} \times 1 \\ \end{cases}
填入矩阵:
M = \begin{bmatrix}1 \ 1 \ 1 \ 3 \\ 0 \ 1 \ 1 \ 3 \\ 0 \ 1 \ 0 \ 0 \\ 0 \ 0 \ 0 \ 1 \end{bmatrix}
进阶 带时间 t:f(x) = 2f(x - 1) + x^2 + x + 1
状态向量 v_t = \begin{bmatrix} f_t \\ t^2 \\ t \\ 1 \end{bmatrix},目标向量 v_{t + 1} = \begin{bmatrix} f_{t + 1} \\ (t + 1)^2 \\ (t + 1) \\ 1 \end{bmatrix}。
填入矩阵:
M = \begin{bmatrix} 2 \ 1 \ 3 \ 3 \\ 0 \ 1 \ 2 \ 1 \\ 0 \ 0 \ 1 \ 1 \\ 0 \ 0 \ 0 \ 1\end{bmatrix}