AcWing 884. 高斯消元解异或线性方程组 题解
题目分析
前置芝士:高斯消元
首先考虑暴力,枚举每一元可能情况并验证,时间复杂度
有了高斯消元的知识,这道题可以转化为求异或线性方程组。
因为异或具有性质
如果有
,那么 。
且异或可以看成不进位的加法,则将高斯消元的加法改成异或即可。
代码
#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;
}
原本的
本文作者:vanueber
本文链接:https://www.cnblogs.com/vanueber/p/18668579
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步