Always keep a b|

vanueber

园龄:2年6个月粉丝:0关注:2

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

题目分析

前置芝士:高斯消元

首先考虑暴力,枚举每一元可能情况并验证,时间复杂度 O(2n×n2) 显然无法通过。

有了高斯消元的知识,这道题可以转化为求异或线性方程组。

{a11x1a12x2a1nxn=b1a21x1a22x2a2nxn=b2an1x1an2x2annxn=bn

因为异或具有性质

如果有 ab=c,那么 a=cb

且异或可以看成不进位的加法,则将高斯消元的加法改成异或即可。

代码

#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

本文作者:vanueber

本文链接:https://www.cnblogs.com/vanueber/p/18668579

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   vanueber  阅读(13)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起