POJ1830 开关问题

嘟嘟嘟


这题思路还是挺奇特的,神奇的利用了自由元的性质。


看到这种题,第一反应是设\(2 ^ n\)个状态,然后解方程组。但对于这题来说显然过不了。


我们还是用高斯消元,对于每一个开关\(i\),令\(f[i][n + 1] = a[i]\)^\(b[i]\)\(a[i]\)表示\(i\)的初始状态,\(b[i]\)为结束状态。然后对于\(i\)的这个方程,首先有\(f[i][i] = 1\),其次如果\(j\)能影响\(i\),那么\(f[i][j] = 1\)
这样构成的方程组,解出来的每一个变量要么有具体值\(0\)\(1\),表示是否按第\(i\)个开关,要么为自由元,表示他按不按都不会影响答案,所以答案就是\(2\)的自由元个数次幂。


这题同时让我更加理解了自由元和无解的情况,无解的情况是在最后回代的时候判断的,解方程的时候这一列系数为\(0\)说明他是自由元。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<queue>
#include<assert.h>
#include<ctime>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define In inline
#define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt)
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 35;
inline ll read()
{
	ll ans = 0;
	char ch = getchar(), last = ' ';
	while(!isdigit(ch)) last = ch, ch = getchar();
	while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
	if(last == '-') ans = -ans;
	return ans;
}
inline void write(ll x)
{
	if(x < 0) x = -x, putchar('-');
	if(x >= 10) write(x / 10);
	putchar(x % 10 + '0');
}
In void MYFILE()
{
#ifndef mrclr
	freopen(".in", "r", stdin);
	freopen(".out", "w", stdout);
#endif
}

int n, a[maxn], b[maxn];

int f[maxn][maxn];
In int Gauss()
{
	int ret = 0;
	for(int i = 1; i <= n; ++i)
	{
		int pos = i;
		while(!f[pos][i] && pos <= n) ++pos;
		if(pos > n) {++ret; continue;}
		if(pos ^ 1) swap(f[i], f[pos]);
		for(int j = i + 1; j <= n; ++j) if(f[j][i])
			for(int k = i; k <= n + 1; ++k) f[j][k] ^= f[i][k];
	}
	for(int i = n; i; --i)
	{
		if(!f[i][i] && f[i][n + 1]) return -1;
		for(int j = i - 1; j; --j) f[j][n + 1] ^= (f[j][i] & f[i][n + 1]);
	}
	return ret;
}

int main()
{
//	MYFILE();
	int T = read();
	while(T--)
	{
		Mem(f, 0);
		n = read();
		for(int i = 1; i <= n; ++i) a[i] = read();
		for(int i = 1; i <= n; ++i) b[i] = read();
		int x = read(), y = read();
		while(x | y) f[y][x] = 1, x = read(), y = read();
		for(int i = 1; i <= n; ++i) f[i][i] = 1, f[i][n + 1] = a[i] ^ b[i];
		ll tp = Gauss();
		if(tp == -1) puts("Oh,it's impossible~!!");
		else write(1 << tp), enter;
	}
	return 0;	
}
posted @ 2019-07-10 16:17  mrclr  阅读(188)  评论(0编辑  收藏  举报