0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-19 01:25:25 +00:00

884. 高斯消元解异或线性方程组

https://www.acwing.com/problem/content/submission/code_detail/14082659/
This commit is contained in:
Baoshuo Ren 2022-05-10 15:17:59 +08:00
parent 805b9bd638
commit 7ee05130ad
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

89
AcWing/884/884.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <cmath>
#include <iomanip>
#include <iostream>
using std::cin;
using std::cout;
const char endl = '\n';
const int N = 105;
int n, a[N][N];
int gauss() {
int c, r;
for (c = r = 1; c <= n; c++) {
int t = r;
for (int i = r; i <= n; i++) {
if (a[i][c]) t = i;
}
if (!a[t][c]) continue;
std::swap(a[t], a[r]);
for (int i = r + 1; i <= n; i++) {
if (a[i][c]) {
for (int j = n + 1; j >= c; j--) {
a[i][j] ^= a[r][j];
}
}
}
r++;
}
if (r <= n) {
for (int i = r; i <= n; i++) {
if (a[i][n + 1]) {
return -1;
}
}
return 1;
}
for (int i = n; i; i--) {
for (int j = i + 1; j <= n; j++) {
a[i][n + 1] ^= a[i][j] & a[j][n + 1];
}
}
return 0;
}
int main() {
std::ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n + 1; j++) {
cin >> a[i][j];
}
}
switch (gauss()) {
case 0: {
for (int i = 1; i <= n; i++) {
cout << a[i][n + 1] << endl;
}
break;
}
case 1: {
cout << "Multiple sets of solutions" << endl;
break;
}
case -1: {
cout << "No solution" << endl;
break;
}
}
return 0;
}