原题链接:
题解:
与求解线性方程组的+运算不同,求解异或线性方程组是xor运算,需要使用异或运算的性质来简化方程组,并最终找到变量的值。
参考讲解链接:AcWing 884. 高斯消元解异或线性方程组 - AcWing
代码:
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N];
int n;
int gauss()
{
//第一:消成同解的上三角矩阵
int r, c;
//1.枚举列,可以跳过行,但是不能跳过列
for (r = 0, c = 0; c < n; c++) {
//2.找到剩余行中的非零行
int t = r;
for (int i = r; i < n; i++)
if (a[i][c]) {
t = i;
break;
}
if (a[t][c] == 0) continue;
//3.非零行换到剩余行中的最上面
for (int i = c; i <= n; i++) swap(a[r][i], a[t][i]);
//4.剩余行中的第c列下面消成0
for (int i = r + 1; i < n; i++) {
if (a[i][c]) {
for (int j = c; j <= n; j++)
a[i][j] ^= a[r][j];
}
}
r++;
}
//第二:判断解的情况
if (r < n) {
for (int i = r; i < n; i++)
if (a[i][n])
return 2;
return 1;
}
else {
for (int i = n - 1; i >= 0; i--) {
for (int j = i + 1; j < n; j++) {
a[i][n] ^= a[i][j] * a[j][n];
}
}
return 0;
}
}
int main()
{
//int n;
cin >> n;
for (int i = 0; i < n; i++)
for (int j = 0; j <= n; j++)
cin >> a[i][j];
int res = gauss();
if (res == 0) {
for (int i = 0; i < n; i++)
cout << a[i][n] << endl;
}
else if (res == 1)
cout << "Multiple sets of solutions" << endl;
else
cout << "No solution" << endl;
}