跳到正文

个人记录

2025.03.28

OIerLCY约 16 分钟
原文
文章信息与版本

文章信息

发布
2025年4月2日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 16 分钟

版本

  1. v1当前

数论——矩阵快速幂

数学中的矩阵可以近似的看做计算机学中的二维数组。

前言

目前我们能碰到的矩阵主要有三种用途:

  • 解方程(省选)
  • DP线性优化(CSP提高级)
  • 优化特殊场景下的 floyd (CSP提高级)

矩阵有自己的运算法则:

  1. 矩阵××数字

设矩阵A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},将AA与任意数字xx相加,可以得到:

A×x=[1x2x3x4x]A × x = \begin{bmatrix} 1x & 2x \\ 3x & 4x \end{bmatrix}

  1. 矩阵++矩阵

设矩阵A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},矩阵B=[5678]B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix},将AA加上BB,有:

A+B=[1+52+63+74+8]A + B = \begin{bmatrix} 1+5 & 2+6 \\ 3+7 & 4+8 \end{bmatrix}

注意,矩阵++矩阵时需确保相加的两个矩阵大小一致,即行数和列数相同。

3.矩阵××矩阵

设矩阵A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix},矩阵B=[5678]B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix},将AA乘上BB,有:

A×B=[5+146+1615+2818+32]A × B = \begin{bmatrix} 5+14 & 6+16 \\ 15+28 & 18+32 \end{bmatrix}

设A×B=CA × B = C,那么关于CC,则有AA的第ii行每一个元素×B×B的第jj行每一个元素等于CC的第ii行第jj列。注意,需保证AA的列数必须等于BB的行数,否则无法相乘。

仅矩阵的左对角线上元素为11,其余为00的矩阵被称之为单位矩阵。

正题

矩阵有啥用,干嘛要学它?

我们不妨看一个例子——斐波那契数列。

斐波那契数列的定义:Fi=Fi−1+Fi−2F_i = F_{i - 1} + F_{i - 2}。

那么,定义一个矩阵f=[F1F2]f = \begin{bmatrix} F_1 & F_2\end{bmatrix},如何让它变成矩阵[F2F3]\begin{bmatrix} F_2 & F_3\end{bmatrix}呢?

不难求出,F2=F2,F3=F1+F2F_2 = F_2,F_3 = F_1+F_2,导出关键矩阵C=[0111]C = \begin{bmatrix} 0 & 1 \\ 1 & 1\end{bmatrix}

f×C=[F2F3]、f×C2=[F3F4]、......、f×Cn−2=[Fn−1Fn]f × C = \begin{bmatrix} F_2 & F_3\end{bmatrix}、f × C^2 = \begin{bmatrix} F_3 & F_4\end{bmatrix}、......、f × C ^{n - 2} = \begin{bmatrix} F_{n - 1} & F_n\end{bmatrix}

诶!套个快速幂,线性DPO(n)O(n)的斐波那契数列求解项就可以变成O(4∗log(n))O(4*log(n))(44是矩阵乘法常数)了。

例题B2105 矩阵乘法

矩阵乘法板题,我学OI三个月都会做。

Code

C++
#include <bits/stdc++.h>
using namespace std;
const int N = 1e2 + 10;
int n, m, k, a[N][N], b[N][N], ans[N][N];
struct Martix{
	int n, m;
	int a[N][N];
	void input(){
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= m; j ++){
				cin >>a[i][j];
			}
		}
		return ;
	}
	void  output(){
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j ++){
				cout << a[i][j] << ' ';
			}
			cout << '\n';
		}
		return ;
	}
	Martix(int x, int y){
		n = x, m = y;
		memset(a, 0, sizeof a);
	}
	Martix operator *(Martix b){
		Martix c = Martix(n, b.m);
		c.n = n, c.m = b.m;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= b.m; j ++){
				for(int k = 1; k <= m; k ++){
					c.a[i][j] += (a[i][k] * b.a[k][j]);
				}
			}
		}
		return c;
	}
};
int main() {
	cin >> n >> m >> k;
	Martix A = Martix(n, m), B = Martix(m, k), ans = Martix(n, k);
	A.input(), B.input();
	ans = A * B;
	ans.output();
	return 0;
}

例题P3390 【模板】矩阵快速幂

矩阵乘法套快速幂板子即可。不会有人不会ksm吧

Code

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9 + 7;
int n, k;
struct Martix {
	int n, m;
	int a[110][110];
	Martix() {
		n = 0;
		m = 0;
		memset(a, 0, sizeof(a));
	}
	void input() {
		for(int i = 1; i <= n; i ++) {
			for(int j = 1; j <= m; j ++) {
				cin >>a[i][j];
			}
		}
		return ;
	}
	void  output() {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j ++) {
				cout << a[i][j] << ' ';
			}
			cout << '\n';
		}
		return ;
	}
	Martix(int k) {
		n = k;
		m = k;
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= k; i++) {
			a[i][i] = 1;
		}
	}
	Martix(int x, int y) {
		n = x;
		m = y;
		memset(a, 0, sizeof(a));
	}
	Martix operator*(Martix b) {
		Martix c(n, b.m);
		for (int i = 1; i <= c.n; i++) {
			for (int j = 1; j <= c.m; j++) {
				for (int k = 1; k <= m; k++) {
					c.a[i][j] = (c.a[i][j] + (a[i][k] * b.a[k][j])) % mod;
				}
			}
		}
		return c;
	}
};
Martix quick_pow(Martix A, int b) {
	Martix ans(n);
	while (b) {
		if (b & 1) {
			ans = ans * A;
		}
		b >>= 1;
		A = A * A;
	}
	return ans;
}
signed main() {
	cin >> n >> k;
	Martix A(n, n);
	A.input();
	Martix ans = quick_pow(A, k);
	ans.output();
	return 0;
}

例题P1349 广义斐波那契数列

把之前求的C=[0q1p]C = \begin{bmatrix} 0 & q \\ 1 & p\end{bmatrix}即可,推导过程略。

Code

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
int p, q, a1, a2, n, mod;
struct Martix {
	int n, m;
	int a[3][3];
	Martix() {
		n = 0;
		m = 0;
		memset(a, 0, sizeof(a));
	}
	void input() {
		for(int i = 1; i <= n; i ++) {
			for(int j = 1; j <= m; j ++) {
				cin >>a[i][j];
			}
		}
		return ;
	}
	void  output() {
		for(int i = 1; i <= n; i++) {
			for(int j = 1; j <= m; j ++) {
				cout << a[i][j] << ' ';
			}
			cout << '\n';
		}
		return ;
	}
	Martix(int k) {
		n = k;
		m = k;
		memset(a, 0, sizeof(a));
		for (int i = 1; i <= k; i++) {
			a[i][i] = 1;
		}
	}
	Martix(int x, int y) {
		n = x;
		m = y;
		memset(a, 0, sizeof(a));
	}
	Martix operator*(Martix b) {
		Martix c(n, b.m);
		for (int i = 1; i <= c.n; i++) {
			for (int j = 1; j <= c.m; j++) {
				for (int k = 1; k <= m; k++) {
					c.a[i][j] = (c.a[i][j] + (a[i][k] * b.a[k][j])) % mod;
				}
			}
		}
		return c;
	}
};
Martix A(2, 2), B(2, 2);
void quick_pow(int b) {
	while (b) {
		if (b & 1) {
			A = A * B;
		}
		b >>= 1;
		B = B * B;
	}
}
signed main() {
	cin >> p >> q >> a1 >> a2 >> n >> mod;
	if(n == 1){
		cout << a1 % mod;
		return 0;
	}
	if(n == 2){
		cout << a2 % mod;
		return 0;
	}
	A.a[1][1] = a2, A.a[1][2] = a1;
	B.a[1][1] = p, B.a[2][1] = q, B.a[1][2] = 1;
	quick_pow(n - 2);
	cout << A.a[1][1] % mod;
	return 0;
}

例题U546616 骨牌

同理,容易发现ansn=ansn−1+ansn−2+ansn−3ans_n = ans_{n - 1} + ans_{n - 2} + ans_{n - 3},将C=[001101011]C = \begin{bmatrix} 0 & 0 & 1 \\ 1 & 0 & 1 \\ 0 & 1 & 1 \end{bmatrix}即可。

但是注意,n≤3n ≤ 3时特判一下。

Code

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MOD = 1e9 + 7;
const int N = 5; 
int n, ans;
struct Martix {
	int n, m;
	int a[N][N];
	Martix(int x = 0, int y = 0) : n(x), m(y) {
		memset(a, 0, sizeof(a));
	}
	Martix operator *(const Martix& b) const {
		Martix c(n, b.m);
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= b.m; j++)
				for(int k = 1; k <= m; k++)
					c.a[i][j] = (c.a[i][j] + a[i][k] * b.a[k][j]) % MOD;
		return c;
	}
	void identity() {
		for(int i = 1; i <= n; i++)
			a[i][i] = 1;
	}
};
Martix matrix_pow(Martix a, int n) {
	Martix res(a.n, a.m);
	res.identity();
	while(n > 0) {
		if(n & 1) res = res * a;
		a = a * a;
		n >>= 1;
	}
	return res;
}
signed main() {
	cin >> n;
	if(n == 0) {
		cout << 0 << endl;
		return 0;
	}
	if(n == 1) {
		cout << 1 << endl;
		return 0;
	}
	if(n == 2) {
		cout << 2 << endl;
		return 0;
	}
	if(n == 3) {
		cout << 4 << endl;
		return 0;
	}
	Martix B(3, 3);
	B.a[1][1] = B.a[1][2] = B.a[1][3] = B.a[2][1] = B.a[3][2] = 1;;
	B.a[2][2] = B.a[2][3] = B.a[3][1] = B.a[3][3] = 0;
	Martix res = matrix_pow(B, n - 3);
	ans = (res.a[1][1] * 4 + res.a[1][2] * 2 + res.a[1][3] * 1) % MOD;
	cout << ans << endl;
	return 0;
}

例题P2044 [NOI2012] 随机数生成器

这题与斐波那契不同,Xi+1X_{i + 1}与Xi−1X_{i - 1}并无太大关系,我们可以更改原先的矩阵ff,考虑到新加入了一个cc会改变数列XX的元素大小,于是将f=[X01],C=[a0c1]f = \begin{bmatrix} X_0 & 1 \end{bmatrix},C = \begin{bmatrix} a & 0 \\ c & 1\end{bmatrix},套版即可。 调了114514h,果断用上大佬的快速乘优化。

Code

C++
#include <bits/stdc++.h>
#define int unsigned long long
using namespace std;
int mod, a, c, x0, n, g;
int qmul(int a, int b, int mod) {
	int res = 0;
	while (b) {
		if (b & 1) res = (res + a) % mod;
		a = (a << 1) % mod;
		b >>= 1;
	}
	return res;
}
struct Matrix {
	int m[2][2];
	Matrix() {
		memset(m, 0, sizeof(m));
	}
	Matrix operator*(const Matrix& B) const {
		Matrix res;
		for (int i = 0; i < 2; i++)
			for (int j = 0; j < 2; j++)
				for (int k = 0; k < 2; k++)
					res.m[i][j] = (res.m[i][j] + qmul(m[i][k], B.m[k][j], mod)) % mod;
		return res;
	}
};
Matrix matrix_pow(Matrix base, int power) {
	Matrix res;
	res.m[0][0] = res.m[1][1] = 1;
	while (power) {
		if (power & 1) res = res * base;
		base = base * base;
		power >>= 1;
	}
	return res;
}

signed main() {
	cin >> mod >> a >> c >> x0 >> n >> g;
	if (n == 0) {
		cout << x0 % g << endl;
		return 0;
	}
	Matrix trans;
	trans.m[0][0] = a % mod;
	trans.m[1][0] = c % mod;
	trans.m[1][1] = 1 % mod;
	Matrix pow_trans = matrix_pow(trans, n);
	int result = (qmul(pow_trans.m[0][0], x0, mod) + pow_trans.m[1][0]) % mod;
	cout << result % g << endl;
	return 0;
}
原文