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

· · 算法·理论

高斯消元

理论

这个算法

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

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

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

解得

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

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

\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}

对应的矩阵表示为

\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}

步骤分解

我们以刚才的例子。

初始矩阵

\begin{bmatrix} 1 & 2 & 3 & 36 \\ -1 & 1 & 2 & 17 \\ 1 & 0 & 2 & 17 \\ \end{bmatrix}

消去第二、三行的第一列

\begin{bmatrix} 1 & 2 & 3 & 36 \\ 0 & 3 & 5 & 53 \\ 0 & -2 & -1 & -19 \\ \end{bmatrix}

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

\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}

第三行归一化并回代

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

操作步骤

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

特殊情况分析

无解情况

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

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

是不行的。

还有形如这样的

\begin{bmatrix} 0 & 0 & 0 & 1 \\ -4 & 4 & 1 & 13 \\ \end{bmatrix}

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

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

无穷解情况

\begin{bmatrix} 0 & 1 & 2 & 18 \\ 0 & 6 & -3 & 4 \\ \end{bmatrix}

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

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

模板

P2455 [SDOI2006] 线性方程组

#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 【模板】高斯消元法

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

#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;
}

高斯异或消元

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

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

\begin{cases} x_1 \oplus x_2 = 1 \\ x_1 \oplus x_3 = 0 \\ x_2 \oplus x_3 = 0 \end{cases}

解得

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

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

\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 表示异或运算,系数 a_{i,j} \in \{0,1\},常数项 b_i \in \{0,1\}。

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

\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}

步骤分解

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

初始矩阵

\begin{bmatrix} 1 & 0 & 1 & 1 \\ 1 & 1 & 1 & 0 \\ 0 & 1 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} 1 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 \\ 0 & 1 & 0 & 1 \\ \end{bmatrix} \begin{bmatrix} 1 & 0 & 1 & 1 \\ 0 & 1 & 0 & 1 \\ 0 & 0 & 0 & 0 \\ % 第三行 = 第三行 ⊕ 第二行 \end{bmatrix}

操作步骤

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

特殊情况分析

无解情况

形如这样的矩阵:

\begin{bmatrix} 1 & 1 & 0 & 1 \\ 1 & 1 & 0 & 0 \\ \end{bmatrix}

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

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

无穷解情况

\begin{bmatrix} 1 & 1 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 1 & 1 & 1 & 1 \\ \end{bmatrix}

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

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

模板

P2447 [SDOI2010] 外星千足虫

#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;
}