跳到正文

算法·理论

【高斯消元】与【高斯异或消元】学习笔记

腼腆的立辉约 14 分钟
原文
文章信息与版本

文章信息

发布
2024年12月29日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 14 分钟
获赞
1

版本

  1. v2当前
  2. v1

高斯消元

理论

这个算法

可以求 nn 元一次方程组的解。

举个例子,现在已知有三元一次方程

{x+2y+3z=36−x+y+2z=17x+2z=17\begin{cases} x + 2y + 3z = 36 \\ -x + y + 2z = 17 \\ x + 2z = 17 \end{cases}

解得

{x=3y=6z=7\begin{cases} x = 3 \\ y = 6 \\ z = 7 \end{cases}

将问题扩展为 nn 个未知数的方程组

{a1,1x1+a1,2x2+⋯+a1,nxn=b1a2,1x1+a2,2x2+⋯+a2,nxn=b2⋮an,1x1+an,2x2+⋯+an,nxn=bn\begin{cases} a_{1,1}x_1 + a_{1,2}x_2 + \cdots + a_{1,n}x_n = b_1 \\ a_{2,1}x_1 + a_{2,2}x_2 + \cdots + a_{2,n}x_n = b_2 \\ \vdots \\ a_{n,1}x_1 + a_{n,2}x_2 + \cdots + a_{n,n}x_n = b_n \end{cases}

对应的矩阵表示为

[a1,1a1,2⋯a1,nb1a2,1a2,2⋯a2,nb2⋮⋮⋱⋮⋮an,1an,2⋯an,nbn]\begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} & b_1 \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} & b_2 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ a_{n,1} & a_{n,2} & \cdots & a_{n,n} & b_n \\ \end{bmatrix}

步骤分解

我们以刚才的例子。

初始矩阵

[12336−1121710217]\begin{bmatrix} 1 & 2 & 3 & 36 \\ -1 & 1 & 2 & 17 \\ 1 & 0 & 2 & 17 \\ \end{bmatrix}
  • 处理第一列

消去第二、三行的第一列

[12336035530−2−1−19] \begin{bmatrix} 1 & 2 & 3 & 36 \\ 0 & 3 & 5 & 53 \\ 0 & -2 & -1 & -19 \\ \end{bmatrix}
  • 处理第二列

第二行归一化并消去其他行

[10−1314301535330073493] \begin{bmatrix} 1 & 0 & -\frac{1}{3} & \frac{14}{3} \\ 0 & 1 & \frac{5}{3} & \frac{53}{3} \\ 0 & 0 & \frac{7}{3} & \frac{49}{3} \\ \end{bmatrix}
  • 处理第三列

第三行归一化并回代

[100301060017] \begin{bmatrix} 1 & 0 & 0 & 3 \\ 0 & 1 & 0 & 6 \\ 0 & 0 & 1 & 7 \\ \end{bmatrix}

操作步骤

  • 对每个 xix_i,选取系数绝对值最大的一行,交换至第 ii 行并且以 ii 作为主元,进行归一化。这里,绝对值最大可以减少精度的误差。如果 xix_i 都为 00,则是误解,跳过这一列。
  • 用主行消去其他行中 ii 的系数。
  • 回代:把他化简成最简形矩阵,从最后一行倒序求解变量值,公式为: xi=bi−∑j=i+1nai,jxj x_i = b_i - \sum_{j=i+1}^n a_{i,j}x_j

时间复杂度 O(n3)\text{O}(n^3)。

特殊情况分析

无解情况

形如这样的,方程一样但是答案不一样,比如

[3751037520] \begin{bmatrix} 3 & 7 & 5 & 10 \\ 3 & 7 & 5 & 20 \\ \end{bmatrix}

是不行的。


还有形如这样的

[0001−44113] \begin{bmatrix} 0 & 0 & 0 & 1 \\ -4 & 4 & 1 & 13 \\ \end{bmatrix}

存在矛盾方程 0x+0y+0z≠00x+0y+0z \ne 0,方程组无解。

一般情况:一整行的系数都为 00,答案不为 00。


无穷解情况
[0121806−34] \begin{bmatrix} 0 & 1 & 2 & 18 \\ 0 & 6 & -3 & 4 \\ \end{bmatrix}

第一列全零,自由变量存在,方程组有无穷多解。

形如这样的,一整列的系数都为 00,说明这个数没有参与任何方程。是多少都无所谓,与答案没有关联。

模板

P2455 [SDOI2006] 线性方程组

C++
#include<bits/stdc++.h>
using namespace std;
const int N=160;
const double eps=1e-6;
double a[N][N];
int n;
int gauss()
{
	int c,r;//C:当前处理X_c,R:处理了 r 个未知数 
	for(c=0,r=0;c<n;c++)
	{
		int t=r;
		for(int i=r;i<n;i++)
		{
			if(fabs(a[i][c])>fabs(a[t][c]))//如果这个系数更大 
			{
				t=i;//更新 
			}
		}
		if(fabs(a[t][c])<eps)
		{//如果最大的数还是 0,则代表所有方程这个项的系数都是 0。 
			continue;//就不管 
		}
		for(int i=c;i<n+1;i++)
		{
			swap(a[r][i],a[t][i]);//将其交换至第 c 行
		}
		for(int i=n;i>=c;i--)
		{
			a[r][i]/=a[r][c];//除以系数 
		}
		for(int i=0;i<n;i++)//消消乐 
		{
			if(fabs(a[i][c])>eps && i!=r)//如果这一项系数不为 0 且不是系数最大的一行 
			{
				for(int j=n;j>=c;j--)
				{
					a[i][j]-=a[i][c]*a[r][j];//消元 
				}
			}
		}
		r++;//新处理了一个未知数 
	}
	if(r<n)//如果没有处理完所有的未知数 
	{
		for(int i=r;i<n;i++)//检查剩下的方程 
		{
			if(fabs(a[i][n])>eps)
			{//如果有最终答案>=0说明无解 
				return -1;
			}
		}
		return 0;//反之无解 
	}
	return 1;//有唯一解 
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			cin>>a[i][j];
		}
	}
	int ans=gauss();
	if(ans==1)
	{
		for(int i=0;i<n;i++)
		{
			printf("x%d=%.2f\n",i+1,a[i][n]);
		}
	}
	else
	{
		cout<<ans;
	}
	return 0;
}

P3389 【模板】高斯消元法

这个 gauss⁡\operatorname{gauss} 是经典void类型的。

C++
#include<bits/stdc++.h>
using namespace std;
const int MAXN=100;
int n;
double a[MAXN+5][MAXN+5];
void gauss(int x)
{
	for(int i=0;i<x;i++)
	{
		int row=i;
		for(int j=i;j<x;j++)
		{
			if(fabs(a[row][i])<fabs(a[j][i]))
			{
				row=j;
			}
		}
		if(row!=i)
		{
			swap(a[row],a[i]);
		}
		double div1=a[i][i];
		for(int j=0;j<=x;j++)
		{
			a[i][j]/=div1;
		}
		for(int j=i+1;j<x;j++)
		{
			double div2=a[j][i];
			for(int k=0;k<=x;k++)
			{
				a[j][k]-=a[i][k]*div2;
			}
		}
	}
	//-----------------------------复原 
	for(int i=x-1;i>=0;i--)
	{
		for(int j=i-1;j>=0;j--)
		{
			a[j][x]-=a[j][i]*a[i][x];
			a[j][i]=0;
		}
	}
	//------------------------------
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=0;i<n;i++)
	{
		for(int j=0;j<=n;j++)
		{
			cin>>a[i][j];
		}
	}
	gauss(n);
	for(int i=0;i<n;i++)
	{
		bool flag=1;
		for(int j=0;j<n;j++)
		{
			if(fabs(a[i][j])>1e-8)
			{
				flag=0;
			}
		}
		if(flag)
		{
			cout<<"No Solution";
			return 0;
		}
	}
	for(int i=0;i<n;i++)
	{
		cout<<fixed<<setprecision(2)<<a[i][n]<<'\n';
	}
	return 0;
}

高斯异或消元

这个算法可以求解 nn 元异或方程组的解。

举个例子,现在已知有三元异或方程

{x1⊕x2=1x1⊕x3=0x2⊕x3=0\begin{cases} x_1 \oplus x_2 = 1 \\ x_1 \oplus x_3 = 0 \\ x_2 \oplus x_3 = 0 \end{cases}

解得

{x1=1x2=0x3=1\begin{cases} x_1 = 1 \\ x_2 = 0 \\ x_3 = 1 \end{cases}

将问题扩展为 nn 个未知数的异或方程组

{a1,1x1⊕a1,2x2⊕⋯⊕a1,nxn=b1a2,1x1⊕a2,2x2⊕⋯⊕a2,nxn=b2⋮an,1x1⊕an,2x2⊕⋯⊕an,nxn=bn\begin{cases} a_{1,1}x_1 \oplus a_{1,2}x_2 \oplus \cdots \oplus a_{1,n}x_n = b_1 \\ a_{2,1}x_1 \oplus a_{2,2}x_2 \oplus \cdots \oplus a_{2,n}x_n = b_2 \\ \vdots \\ a_{n,1}x_1 \oplus a_{n,2}x_2 \oplus \cdots \oplus a_{n,n}x_n = b_n \end{cases}

其中 ⊕\oplus 表示异或运算,系数 ai,j∈{0,1}a_{i,j} \in \{0,1\},常数项 bi∈{0,1}b_i \in \{0,1\}。

按照普通高斯消元的方法,对应的矩阵表示为

[a1,1a1,2⋯a1,nb1a2,1a2,2⋯a2,nb2⋮⋮⋱⋮⋮an,1an,2⋯an,nbn]\begin{bmatrix} a_{1,1} & a_{1,2} & \cdots & a_{1,n} & b_1 \\ a_{2,1} & a_{2,2} & \cdots & a_{2,n} & b_2 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ a_{n,1} & a_{n,2} & \cdots & a_{n,n} & b_n \\ \end{bmatrix}

步骤分解

我们以一个三元异或方程组为例:

初始矩阵

[101111100101]\begin{bmatrix} 1 & 0 & 1 & 1 \\ 1 & 1 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ \end{bmatrix}
  • 处理第一列:消去第二行的第一列
[101101010101]\begin{bmatrix} 1 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 \\ 0 & 1 & 0 & 1 \\ \end{bmatrix}
  • 处理第二列:消去第三行的第二列
[101101010000]\begin{bmatrix} 1 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ % 第三行 = 第三行 ⊕ 第二行 \end{bmatrix}
  • 回代求解:得到解为 x3x_3 为自由变量,当 x3=0x_3=0 时,x1=1,x2=1x_1=1,x_2=1;当 x3=1x_3=1 时,x1=0,x2=1x_1=0,x_2=1,即无穷多解。

操作步骤

  • 对每个 xix_i,选取该列中系数为 11 的行,交换至第 ii 行作为主行。如果该列全为 00,则存在无数解,跳过这一列。
  • 用主行消去其他行中 xix_i 的系数(通过异或操作:若某行第 ii 列系数为 11,则该行与主行异或)。
  • 回代:从最后一行倒序求解变量值,若存在整列为 00 则有无穷多解。

时间复杂度 O(n3)\text{O}(n^3)。

特殊情况分析

无解情况

形如这样的矩阵:

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

消元后会得到 0=10=1 的矛盾方程,方程组无解。

一般情况:消元后出现一行系数全为 00 但常数项为 11,即 0⊕0⊕⋯⊕0=10 \oplus 0 \oplus \cdots \oplus 0 = 1,方程组无解。

无穷解情况

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

消元后会出现全零行,说明方程有无穷多解。

一般情况:消元后有全 00 列,方程组有无穷多解。

模板

P2447 [SDOI2010] 外星千足虫

C++
#include<bits/stdc++.h>
using namespace std;
const int N=2005;
string str;
bitset<10005> a[N];
int n,m;
int gauss()
{
	int ans=0;
	for(int i=1;i<=n;i++)
	{
		int c=i;
		while(c<=m && !a[c][i])
		{
			c++;
		}
		if(c>m)
		{
			return 0;
		}
		ans=max(ans,c);
		if(c!=i)
		{
			swap(a[c],a[i]);
		}
		for(int j=1;j<=m;j++)
		{
			if(i!=j && a[j][i])
			{
				a[j]^=a[i];
			}
		}
	}
	return ans;
}
int main()
{
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n>>m;
	for(int i=1;i<=m;i++)
	{
		cin>>str;
		for(int j=0;j<n;j++)
		{
			if(str[j]=='1')
			{
				a[i][j+1]=1;
			}
			else
			{
				a[i][j+1]=0;
			}
		}
		bool x;
		cin>>x;
		a[i][n+1]=x;
	}
	int ans=gauss();
	if(ans)
	{
		cout<<ans<<'\n';
		for(int i=1;i<=n;i++)
		{
			if(a[i][n+1])
			{
				cout<<"?y7M#\n";
			}
			else
			{
				cout<<"Earth\n";
			}
		}
	}
	else
	{
		cout<<"Cannot Determine";
	}
	return 0;
}
原文