const int maxn = 40; const double PI = acos(-1.0); int a[maxn][maxn]; int gauss(int N, int M) { int r, c, pvt; bool flag; for (r = 0, c = 0; r < N && c < M; ++ r, ++ c) { flag = false; for (int i = r; i < N; ++ i) if (a[i][c]) { flag = a[pvt=i][c]; break; } if (!flag) { r--; continue; } if (pvt != r) for (int j = r; j <= M; ++j) swap(a[r][j], a[pvt][j]); for (int i = r+1; i < N; ++i) if(a[i][c]) { a[i][c] = false; for (int j = c+1; j <= M; ++j) { if(a[r][j]) a[i][j] = !a[i][j]; } } } for (int i = r; i < N; ++i) if (a[i][M]) return -1; if (r < M) return M-r; for (int i = M-1; i >= 0; --i) { for (int j = i+1; j < M; ++j) a[i][M] ^= a[j][M]*a[i][j]; a[i][M] ^= a[i][i]; } return 0; } int main() { // freopen("in.txt", "r", stdin); int T, n, x, y; RI(T); FE(kase, 1, T) { RI(n); CLR(a, 0); REP(i, n) RI(a[i][n]); REP(i, n) { int t; RI(t); a[i][n] ^= t; a[i][i] = 1; } while (RII(x, y) && x) { a[y - 1][x - 1] = 1; } int ans = gauss(n, n); if (ans == -1) puts("Oh,it‘s impossible~!!"); else WI(1 << ans); } return 0; }
POJ1830开关问题——gauss消元,布布扣,bubuko.com
原文地址:http://blog.csdn.net/wty__/article/details/25367483