# POJ 1830 高斯消元

##### 题解

X O R j ∈ [ 1 , N ] ( x j   &   a i , j ) = s i ⊕ t i XOR_{j\in [1,N]}(x_j\ \&\ a_{i,j})=s_i\oplus t_i 联立异或线性方程组，高斯消元求解，实现上将代表灯 i i 状态的方程，即增广矩阵的每一行进行状态压缩，此时高斯消元的复杂度为 O ( N 2 ) O(N^2)

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 30
int N, A[maxn], S[maxn], T[maxn];

int gauss_jordan()
{
for (int i = 1; i <= N; ++i)
{
for (int j = i + 1; j <= N; ++j)
if (A[i] < A[j])
swap(A[i], A[j]);
if (A[i] == 0)
return 1 << (N - i + 1);
if (A[i] == 1)
return 0;
for (int j = N; j >= 1; --j)
if (A[i] >> j & 1)
{
for (int k = 1; k <= N; ++k)
if (i != k && (A[k] >> j & 1))
A[k] ^= A[i];
break;
}
}
return 1;
}

int main()
{
int t;
scanf("%d", &t);
while (t--)
{
memset(A, 0, sizeof(A));
scanf("%d", &N);
for (int i = 1; i <= N; ++i)
scanf("%d", S + i);
for (int i = 1; i <= N; ++i)
scanf("%d", T + i);
for (int i = 1; i <= N; ++i)
A[i] |= 1 << i, A[i] |= S[i] ^ T[i];
int x, y;
while (~scanf("%d%d", &x, &y) && (x | y))
A[y] |= 1 << x;
int res = gauss_jordan();
if (!res)
puts("Oh,it's impossible~!!");
else
printf("%d\n", res);
}
return 0;
}


02-25 619
04-11 2053

11-16 212
01-29 291
10-11 415
03-27 397
11-03 146
04-24 284
05-15 20
08-10 535
07-10 23
08-20