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

题目分析

前置芝士:高斯消元

不懂的可以看看这篇文章:数学-高斯消元

首先考虑暴力,枚举每一元可能情况并验证,时间复杂度 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} a11x1a12x2a1nxn=b1a21x1a22x2a2nxn=b2an1x1an2x2annxn=bn

因为异或具有性质

如果有 a ⊕ b = c a \oplus b=c ab=c,那么 a = c ⊕ b a = c \oplus b 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;
}

原本的 38 m s 38ms 38ms 可以优化到 28 m s 28ms 28ms

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值