题目分析
前置芝士:高斯消元
不懂的可以看看这篇文章:数学-高斯消元
首先考虑暴力,枚举每一元可能情况并验证,时间复杂度 O ( 2 n × n 2 ) O( 2^n \times n^2) O(2n×n2) 显然无法通过。
有了高斯消元的知识,这道题可以转化为求异或线性方程组。
{ a 11 x 1 ⊕ a 12 x 2 ⊕ ⋯ ⊕ a 1 n x n = b 1 a 21 x 1 ⊕ a 22 x 2 ⊕ ⋯ ⊕ a 2 n x n = b 2 ⋮ a n 1 x 1 ⊕ a n 2 x 2 ⊕ ⋯ ⊕ a n n x n = b n \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} ⎩ ⎨ ⎧a11x1⊕a12x2⊕⋯⊕a1nxn=b1a21x1⊕a22x2⊕⋯⊕a2nxn=b2⋮an1x1⊕an2x2⊕⋯⊕annxn=bn
因为异或具有性质
如果有 a ⊕ b = c a \oplus b=c a⊕b=c,那么 a = c ⊕ b a = c \oplus b a=c⊕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;
}
原本的 38 m s 38ms 38ms 可以优化到 28 m s 28ms 28ms。