[洛谷P2447][SDOI2010]外星千足虫

题目大意:有$n$个数,每个数为$0$或$1$,给你其中一些关系,一个关系形如其中几个数的异或和是多少,问最少知道前几个关系就可以得出每个数是什么,并输出每个数

题解:异或方程组,和高斯消元差不多,就是把加减改成了异或。

卡点:用$bitset$优化,输出时输反了

C++ Code:

#include <algorithm>
#include <iostream>
#include <bitset>
#define maxn 1010
#define maxm 2010
int n, m, ans;
std::bitset<maxn> s[maxm];
int main() {
	std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0);
	std::cin >> n >> m;
	for (int i = 1, x; i <= m; i++) {
		std::cin >> s[i] >> x;
		s[i] <<= 1; s[i][n + 1] = x;
	}
	for (int i = 1, pos; i <= n; i++) {
		for (pos = i; pos <= m; pos++) if (s[pos][i]) break;
		if (pos > m) {
			std::cout << "Cannot Determine\n";
			return 0;
		}
		ans = std::max(ans, pos);
		std::swap(s[i], s[pos]);
		for (int j = 1; j <= m; j++) if (i != j && s[j][i]) s[j] ^= s[i];
	}
	std::cout << ans << '\n';
	for (int i = n; i; i--) std::cout << (s[i][n + 1] ? "?y7M#\n" : "Earth\n");
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Memory-of-winter/p/10048649.html