Poj--1830(数学,高斯消元)

2014-09-18 14:17:19

思路:和1222很像,高斯消元,N个主元。

 1 /*************************************************************************
 2     > File Name: 1830.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com 
 5     > Created Time: Thu 18 Sep 2014 01:43:45 PM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <vector>
13 #include <queue>
14 #include <iostream>
15 #include <algorithm>
16 using namespace std;
17 typedef long long ll;
18 const int INF = 1 << 29;
19 
20 int T,N;
21 int st[30],ed[30];
22 int A[30][30];
23 
24 void Debug(){
25     for(int i = 1; i <= N; ++i){
26         for(int j = 1; j <= N + 1; ++j){
27             printf("%d ",A[i][j]);
28         }
29         puts("");
30     }
31     puts("");
32 }
33 
34 int Gauss(){
35     int cnt = 0;
36     int equ = N,var = N;
37     int i,j,k,r,col = 1;
38     for(i = 1; i <= equ && col <= var; ++i,++col){
39         r = i;
40         for(j = i + 1; j <= equ; ++j)
41             if(A[j][col]){
42                 r = j;
43                 break;
44             }
45         if(r != i)
46             for(j = col; j <= var + 1; ++j) swap(A[i][j],A[r][j]);
47         if(A[i][col] == 0){
48             --i;
49             cnt++;
50             continue;
51         }
52         for(k = i + 1; k <= equ; ++k)
53             if(A[k][col]){
54                 for(j = col; j <= var + 1; ++j) A[k][j] ^= A[i][j];
55             }
56         //Debug();
57     }
58     for(k = i; k <= equ; ++k)
59         if(A[k][var + 1] != 0)
60             return -1;
61     return cnt;
62 }
63 
64 int main(){
65     int a,b;
66     scanf("%d",&T);
67     while(T--){
68         scanf("%d",&N);
69         memset(A,0,sizeof(A));
70         for(int i = 1; i <= N; ++i) scanf("%d",st + i);
71         for(int i = 1; i <= N; ++i) scanf("%d",ed + i);
72         for(int i = 1; i <= N; ++i){
73             A[i][i] = 1;
74             A[i][N + 1] = st[i] ^ ed[i];
75         }
76         while(scanf("%d%d",&a,&b) != EOF && (a || b)){
77             A[b][a] = 1;
78         }
79         //Debug();
80         int t = Gauss();
81         if(t == -1) printf("Oh,it's impossible~!!\n");
82         else printf("%d\n",1 << t);
83     }
84     return 0;
85 }

 

posted @ 2014-09-18 20:11  Naturain  阅读(167)  评论(0)    收藏  举报