AcWing 884. 高斯消元解异或线性方程组 题解
题目分析
前置芝士:高斯消元
首先考虑暴力,枚举每一元可能情况并验证,时间复杂度 \(O( 2^n \times n^2)\) 显然无法通过。
有了高斯消元的知识,这道题可以转化为求异或线性方程组。
\[\begin{cases}
a_{11}x_1 \oplus a_{12}x_2 \oplus \dots\oplus a_{1n}x_n = b_1\\
a_{21}x_1 \oplus a_{22} x_2 \oplus \dots \oplus a_{2n}x_n= b_2\\
\vdots \\
a_{n1}x_1 \oplus a_{n2}x_2 \oplus \dots \oplus a_{nn}x_n = b_n
\end{cases}
\]
因为异或具有性质
如果有 \(a \oplus b=c\),那么 \(a = c \oplus b\)。
且异或可以看成不进位的加法,则将高斯消元的加法改成异或即可。
代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <ctime>
using namespace std;
const int maxn = 105;
int a[maxn][maxn];
int ans[maxn];
int n;
void print()
{
puts("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n + 1; j++)
{
printf("%5d ", a[i][j]);
}
puts("");
}
puts("");
}
void print_ans()
{
puts("");
for (int i = 1; i <= n; i++)
{
cout << ans[i] << " ";
}
puts("");
}
void gauss()
{
// row 行 col 列
int row = 1, col = 1;
for (col = 1; col <= n; col++)
{
int t = row;
for (int i = t; i <= n; i++)
{
if (a[i][col] == 1)
{
t = i;
break;
}
}
if (a[t][col] == 0) continue;
swap(a[t], a[row]);
for (int i = row + 1; i <= n; i++)
{
if (!a[i][col]) continue;
for (int j = n + 1; j >= col; j--)
{
a[i][j] ^= a[row][j];
}
}
row++;
}
// print();
if (row <= n)
{
for (int i = row; i <= n; i++)
{
if (a[i][n + 1] == 1)
{
puts("No solution");
exit(0);
}
}
puts("Multiple sets of solutions");
exit(0);
}
// print();
for (int i = n; i >= 1; i--)
{
// print_ans();
ans[i] = a[i][n + 1];
for (int j = i + 1; j <= n; j++)
{
if (a[i][j]) ans[i] ^= ans[j];
}
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << endl;
}
return;
}
int main()
{
#ifndef ONLINE_JUDGE
#define LOCAL
//freopen("in.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n + 1; j++)
{
cin >> a[i][j];
}
}
gauss();
#ifdef LOCAL
fprintf(stderr, "%f\n", 1.0 * clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
因为操作是异或,故可以使用 bitset 优化。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <ctime>
#include <bitset>
using namespace std;
const int maxn = 105;
bitset<maxn> a[maxn];
int ans[maxn];
int n;
void print()
{
puts("");
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n + 1; j++)
{
printf("%5d ", a[i][j]);
}
puts("");
}
puts("");
}
void print_ans()
{
puts("");
for (int i = 1; i <= n; i++)
{
cout << ans[i] << " ";
}
puts("");
}
void gauss()
{
// row 行 col 列
int row = 1, col = 1;
for (col = 1; col <= n; col++)
{
int t = row;
for (int i = t; i <= n; i++)
{
if (a[i][col] == 1)
{
t = i;
break;
}
}
if (a[t][col] == 0) continue;
swap(a[t], a[row]);
for (int i = row + 1; i <= n; i++)
{
if (!a[i][col]) continue;
a[i] ^= a[row];
}
row++;
}
// print();
if (row <= n)
{
for (int i = row; i <= n; i++)
{
if (a[i][n + 1] == 1)
{
puts("No solution");
exit(0);
}
}
puts("Multiple sets of solutions");
exit(0);
}
// print();
for (int i = n; i >= 1; i--)
{
// print_ans();
ans[i] = a[i][n + 1];
for (int j = i + 1; j <= n; j++)
{
if (a[i][j]) ans[i] ^= ans[j];
}
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << endl;
}
return;
}
int main()
{
#ifndef ONLINE_JUDGE
#define LOCAL
//freopen("in.txt","r",stdin);
#endif
cin >> n;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n + 1; j++)
{
int k;
cin >> k;
a[i][j] = k;
}
}
gauss();
#ifdef LOCAL
fprintf(stderr, "%f\n", 1.0 * clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
原本的 \(38ms\) 可以优化到 \(28ms\)。

浙公网安备 33010602011771号