跳到正文

算法·理论

线性代数

_xxxxx_约 21 分钟
原文

这是 2026年8月21日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年8月20日
收录
2026年8月21日
更新
2026年11月10日
篇幅
约 21 分钟
获赞
1

版本

  1. v3当前
  2. v2
  3. v1

vjudge link

向量

向量线性相关:两条向量在同一直线上,此时变换完的空间会被压少若干维。反之则不是线性相关。

线性变换:经过一个 AA 的线性变换后,得到的坐标系仍然平行且等距。

秩(rank):行列式在空间上形成的维度,或是高斯消元时的主元(非 0)个数,即他们线性无关。

貌似下面有一些地方把 基向量 写成 向量基 了,请仔细甄别。

矩阵乘法

若现在有两个矩阵 A,BA,B,定义他们的矩阵乘为:

C++
for(int i = 1; i <= 2; i++)
	for(int j = 1; j <= 2; j++)
		for(int k = 1; k <= 2; k++)
			res.a[i][j] += a[i][k] * b.a[k][j];

请自行意会。具体就是 AA 每一行和 BB 每一列相乘再加在一起。


几何意义:

先定义向量基是一个 mm 维空间中的一个类似 单位长度 的向量。如二维平面就有

i=[10],j=[01]i=\begin{bmatrix} 1 \\ 0 \end{bmatrix},j=\begin{bmatrix} 0 \\ 1 \end{bmatrix}

拓展到 mm 维就是一个 mm 行 11 列的向量。

对于原空间上的一个向量 AA,他有着 mm 个数字来确定这个向量的终点,我们想让他变换,一个普遍的方式是平移,旋转。那其实可以通过操控这 mm 个向量基来实现。具体的就是 AA 乘上这个向量基,可以在这个向量基控制的这一维度上进行移动。

现在一个向量集合 BB 中是 nn 个 mm 维向量,现在他们要做变换,而 AA 中是 mm 个对应维上的 mm 维向量基。那么 ABAB 就代表 BB 中的每个向量根据这些向量基做完变换后的向量集的情况。这恰好就对应了上文的 BB 每一行(一个向量)和 AA 每一列(一个向量基)相乘的原理。

其实了解这个几何意义貌似没有什么用处,大抵是可以和向量一起理解,以及这是了解矩阵乘法是如何推出的。

实际应用上,大多都是用一个 mm 行 11 列的向量进行变换。


应用

这个有啥用呢,可以快速处理递推的第 kk 项。

比如我们现在有一个斐波那契数列 f(n)=f(n−1)+f(n−2)f(n)=f(n-1)+f(n-2)。

那么我们尝试把它丢到矩阵上,就是我们现在有的向量是

[f(n−1)f(n−2)]\begin{bmatrix} f(n-1) \\ f(n-2) \end{bmatrix}

我们想推出

[f(n)f(n−1)]\begin{bmatrix} f(n) \\ f(n-1) \end{bmatrix}

那么我们现在的向量基就是 22 维的:

[????]\begin{bmatrix} ? &? \\ ? &? \end{bmatrix}

因为 f(n)=f(n−1)+f(n−2)f(n)=f(n-1)+f(n-2),那么我们显然 f(n−1)f(n-1) 和 f(n−2)f(n-2) 对新的向量中的 f(n)f(n) 贡献,具体的,我们在向量基上填上:

[11??]\begin{bmatrix} 1 &1 \\ ? &? \end{bmatrix}

然后要补上新向量的 f(n−1)f(n-1),其实就是直接等于旧向量的 f(n−1)f(n-1) 部分,即:

[1110]\begin{bmatrix} 1 & 1 \\ 1 &0 \end{bmatrix}

那么就补完了,将 f(1)=1,f(2)=1f(1)=1,f(2)=1 填进初始的向量 AA 中,答案就是

ans=[1110]n−1[11]=[1110]n−1Aans= \begin{bmatrix} 1 & 1 \\ 1 &0 \end{bmatrix}^{n-1} \begin{bmatrix} 1 \\ 1 \end{bmatrix}= \begin{bmatrix} 1 & 1 \\ 1 &0 \end{bmatrix}^{n-1} A

那么 ans.a[1][1] 就是 f(n)f(n) 了。

summary: 其实就是第 mm 行向量基的取值决定了 ansans 第 mm 行的值,根据需要推出向量基这一行的取值。

code

注意这里的 AA 才是向量基。

C++
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define fi first
#define se second
#define pb push_back
const int N = 5e3 + 10, M = 2e2 + 10, inf = 1e18, mod = 1e9 + 7; 
using namespace std;
typedef pair<int, int> pii;
struct Matr
{
	int a[3][3];
	Matr() { memset(a, 0, sizeof a); }
	Matr operator * (const Matr &b) const
	{
		Matr res;
		for(int i = 1; i <= 2; i++)
			for(int j = 1; j <= 2; j++)
				for(int k = 1; k <= 2; k++)
					(res.a[i][j] += a[i][k] * b.a[k][j]) %= mod;
		return res;
	}
};
Matr mpower(Matr a, int b)
{
	Matr ans; ans.a[1][1] = ans.a[2][2] = 1;
	while(b)
	{
		if(b & 1) ans = ans * a;
		a = a * a, b >>= 1;
	}
	return ans;
}
int n;
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	if(n <= 2) return cout << "1\n", 0;
	Matr A; A.a[1][1] = A.a[1][2] = A.a[2][1] = 1;
	cout << mpower(A, n - 1).a[1][1] << "\n"; 
	return 0;
}
/*
*/

AT_dp_r

设 dpi,j,tdp_{i,j,t} 表示 i→ji \to j 走了 tt 步的方案数,那么类似 floyd 的,我们有转移:

dpi,j,t=∑k=1ndpi,k,t−1×dpk,j,1dp_{i,j,t}=\sum_{k=1}^{n} dp_{i,k,t-1} \times dp_{k,j,1}

欸你发现,这个 dp 如果去掉 tt 维时恰好与矩阵乘法的形式一模一样。显然的,我们有答案 ans=∑dpi,j,tans=\sum dp_{i,j,t}。又因为现在的形式是 ft=ft−1×f1f_t=f_{t-1} \times f_1,所以 ans=fK=GKans=f_K=G^K。

code
C++
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define fi first
#define se second
#define pb push_back
const int N = 1e2 + 10, M = 2e2 + 10, inf = 1e18, mod = 1e9 + 7; 
using namespace std;
typedef pair<int, int> pii;
int n, K;
struct Matr
{
	int a[N][N];
	Matr() { memset(a, 0, sizeof a); }
	Matr operator * (Matr &b) const
	{
		Matr res;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				for(int k = 1; k <= n; k++)
					(res.a[i][j] += a[i][k] * b.a[k][j] % mod) %= mod;
		return res;
	}
};
Matr A;
Matr mpower(Matr a, int b)
{
	Matr res;
	for(int i = 1; i <= n; i++) res.a[i][i] = 1;
	while(b)
	{
		if(b & 1) res = res * a;
		a = a * a, b >>= 1;
	}
	return res;
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> K;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) cin >> A.a[i][j];
	Matr ans = mpower(A, K);
	int sum = 0;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) (sum += ans.a[i][j]) %= mod;
	cout << sum << "\n";
	return 0;
}
/*
dp_{i,j,t}=\sum_{k=1}^{n} dp_{i,k,t-1} \times dp_{k,j,1}
*/

P6569

蓝的码字了,直接贴 Reynard 的:

code
C++
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define fi first
#define se second
#define pb push_back
const int N = 1e2 + 10, M = 2e2 + 10, inf = 1e18, mod = 2009; 
using namespace std;
typedef pair<int, int> pii;
int n, K;
struct Matr
{
	int a[N][N];
	Matr() { memset(a, 0, sizeof a); }
	Matr operator * (Matr &b) const
	{
		Matr res;
		for(int i = 1; i <= n; i++)
			for(int j = 1; j <= n; j++)
				for(int k = 1; k <= n; k++)
					(res.a[i][j] ^= a[i][k] * b.a[k][j]);
		return res;
	}
};
struct Vec
{
	int a[N];
	Vec() { memset(a, 0, sizeof a); }
};
Vec operator * (Matr &b, Vec &a)
{
	Vec res;
	for(int j = 1; j <= n; j++)
		for(int k = 1; k <= n; k++)
			(res.a[j] ^= a.a[k] * b.a[k][j]);
	return res;
}
Matr A;
Matr mpower(Matr a, int b)
{
	Matr res;
	for(int i = 1; i <= n; i++) res.a[i][i] = 1;
	while(b)
	{
		if(b & 1) res = res * a;
		a = a * a, b >>= 1;
	}
	return res;
}
int m, q, a[N];
Matr g[33];
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m >> q;
	for(int i = 1; i <= n; i++) cin >> a[i];
	for(int i = 1, x, y; i <= m; i++) cin >> x >> y, g[0].a[x][y] = g[0].a[y][x] = 1;
	for(int i = 1; i < 32; i++) g[i] = g[i - 1] * g[i - 1];
	for(int t = 1, x; t <= q; t++)
	{
		cin >> x;
		Vec f;
		for(int i = 1; i <= n; i++) f.a[i] = a[i];
		for(int i = 0; i < 32; i++) if((x >> i) & 1) f = g[i] * f;
		cout << f.a[1] << "\n";
	}
	return 0;
}
/*
*/

广义的矩阵乘法

综上我们可以定义广义矩阵乘法为:

对于矩阵 An×mA_{n×m} 和 Bm×pB_{m×p},定义 Ci,j=⊕k=1m(Ai,k⊗Bk,j)C_{i,j}= \oplus^m_ {k=1}(A_{i,k}\otimes B_{k,j}),我们将其称为 (⊕,⊗)(\oplus,\otimes) 的矩阵乘法。

其中需要满足:

  • ⊕\oplus 满足交换律,结合律。
  • ⊗\otimes 满足结合律。
  • ⊗\otimes 对于 ⊕\oplus 满足分配律。

常见的 (⊕,⊗)(\oplus,\otimes) 组合有 (+,×)(+,\times),(min⁡,+)(\min,+),(max⁡,+)(\max,+),(∨,∧)(\vee,\wedge),(⊗,∧)(\otimes,\wedge)。注意不同运算下的零元(转移不存在)、单位元(原地不动)和单位矩阵也需要重新定义。

高斯消元

逆矩阵:定义 nn 阶方阵 AA,若存在方阵 BB 使得:

AB=BA=InAB=BA=I_n

其实没太听懂。本质上是模拟做一个加减消元求解线性方程组的过程。

高斯消元从左到右处理每一列,每次尝试在当前列找到一个主元。

主元:当前列中选出的一个非零元素,用它消掉其他行在这一列的系数。

设有m个方程、n个未知数,构造增广矩阵 [a∣b⃗][a|\vec{b}]。就是把 b⃗\vec{b} 和 aa 放在一起。

  1. 从左到右枚举变量列;
  2. 在当前还没有处理的行中,寻找这一列的非零元素作为主元;
  3. 如果找到了主元,将主元所在行交换到当前行;
  4. 将主元归一化,并用主元消去其他行在这一列的系数;
  5. 进入下一行,继续处理后面的列;
  6. 消元结束后判断是否存在矛盾行,并根据主元个数判断解的类型;
  7. 若需要求具体解,则根据主元列和自由变量进行回代。

其实就是在每一列中确定一个主元让它的系数控制为 11,那么它需要变为 11 的话要把它同行的元素同比进行放缩,同列的元素进行加减。就是在它这一列只保留下它自己为 11。

若这一列能找到主元,那么这个维度上的向量就与其他向量线性无关,换句话说就是方程组的秩(rank)可以 +1+1。

那么这个就是原始的高斯消元,最后可以形成一个右上角有数的一个矩阵,如:

[1???01??001?0001]\begin{bmatrix} 1 &? &? &? \\ 0 &1 &? &? \\ 0 &0 &1 &? \\ 0 &0 &0 &1 \end{bmatrix}

此时右下角的 11 可以直接解出当前这个元的解,回代就是带进上面的数。

还存在更高级的高斯-约旦消元。

不仅消去主元下面的元素,也消去主元上面的元素。最后左边接近单位矩阵,可以直接读出答案。

意思就是不只是消下面的行,上面的行也一起消,最后可以形成:

[c0000b0000a00001]\begin{bmatrix} c &0 &0 &0 \\ 0 &b &0 &0 \\ 0 &0 &a &0 \\ 0 &0 &0 &1 \end{bmatrix}

其中 a,b,ca,b,c 是上面通过计算已知数。这样不需要回代。

code
C++
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define fi first
#define se second
#define pb push_back
const int N = 1e2 + 10, M = 2e2 + 10, inf = 1e18, mod = 2009;
const double eps = 1e-6; 
using namespace std;
typedef pair<int, int> pii;
int n;
double a[N][N];
int rnk; // 秩 
void gauss()
{
	vector<int> where(n + 1);
	for(int col = 1; col <= n; col++) // 枚举的是列 
	{
		int p = rnk + 1;
		while(p <= n && fabs(a[p][col]) < eps) p++; // 找当前列的非 0 元素 
		if(p > n) continue;
		swap(a[p], a[++rnk]); where[col] = rnk;
		double pivot = a[rnk][col]; // 先存下主元的系数,不然一会会被直接除成 0 
		for(int j = col; j <= n + 1; j++) a[rnk][j] /= pivot; // 这里枚举的是列 
		for(int i = 1; i <= n; i++) // Jordan 直接全部删完 现在枚举的是行 
		{
			if(i == rnk || fabs(a[i][col]) < eps) continue;
			double t = a[i][col];
			for(int j = col; j <= n + 1; j++) a[i][j] -= t * a[rnk][j];
		}
	}
	for(int i = 1; i <= n; i++)
	{
		bool zro = 1;
		for(int j = 1; j <= n; j++) if(fabs(a[i][j]) > eps) { zro = 0; break; }
		if(zro && fabs(a[i][n + 1]) > eps) return cout << "-1", void();
	}
	if(rnk != n) return cout << "0\n", void();
	for(int i = 1; i <= n; i++)
	{
		cout << "x" << i << "=";
//		cout << where[i] << "\n";
		cout << fixed << setprecision(10) << a[where[i]][n + 1] << "\n";
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= n + 1; j++) cin >> a[i][j];
	gauss();
	return 0;
}
/*
*/

更高级的模板

要求输出方程组 Ax=0A_x=0 解空间的维数,一组特解和每个维度上的一个基。

我们之前求出来的秩是线性无关的主元个数,但是要输出的是维度,维度就是 解可以自由移动的空间。自由移动,那自然就是自由元造成的。自由元可以任意取值,能造成解的移动,那么第一问输出 m−rankm-rank。

一组特解就和上面那个是一样的。每一个维度上的基就是把这一维上的自由元设为 11 后求出其他解。几何意义就是这个空间上的一个基向量。

code
C++
#include <bits/stdc++.h>
#define int long long
#define mk make_pair
#define fi first
#define se second
#define pb push_back
const int N = 1e3 + 10, M = 2e2 + 10, inf = 1e18, mod = 998244353;
const double eps = 1e-6; 
using namespace std;
typedef pair<int, int> pii;
int n, m, a[N][N];
int power(int a, int b)
{
	int ans = 1;
	while(b)
	{
		if(b & 1) ans = ans * a % mod;
		a = a * a % mod, b >>= 1;
	}
	return ans;
}
int rnk;
void gauss()
{
	vector<int> where(m + 1);
	for(int col = 1; col <= m && rnk < n; col++)
	{
		int p = rnk + 1;
		while(p <= n && a[p][col] == 0) p++;
		if(p > n) continue;
		swap(a[p], a[++rnk]); where[col] = rnk;
		int inv = power(a[rnk][col], mod - 2);
		for(int j = col; j <= m + 1; j++) (a[rnk][j] *= inv) %= mod;
		for(int i = 1; i <= n; i++)
		{
			if(i == rnk || a[i][col] == 0) continue;
			int t = a[i][col];
			for(int j = col; j <= m + 1; j++) (a[i][j] = a[i][j] - t * a[rnk][j] % mod + mod) %= mod;
		}
	}
	for(int i = 1; i <= n; i++)
	{
		bool zro = 1;
		for(int j = 1; j <= m; j++) zro &= (a[i][j] == 0);
		if(zro && a[i][m + 1] != 0) return cout << "-1\n", void();
	}
	cout << m - rnk << "\n";
	for(int i = 1; i <= m; i++) cout << a[where[i]][m + 1] << " ";
	cout << "\n";
	for(int i = 1; i <= m; i++)
	{
		if(where[i]) continue;
		for(int j = 1; j <= m; j++)
		{
			if(i == j) cout << 1 << " ";
			else cout << (-a[where[j]][i] + mod) % mod << " ";
		}
	}
}
signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	cin >> n >> m;
	for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) cin >> a[i][j];
	for(int i = 1; i <= n; i++) cin >> a[i][m + 1];
	gauss();
	return 0;
}
/*
*/

线性基