终于会一点高斯消元了,认真学还是学的进去啊。。。。。。
搞明白解异或方程的原理,然后构造出矩阵就好做了。模板题
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 #include <stack> 5 #include <queue> 6 #include <map> 7 #include <algorithm> 8 #include <vector> 9 #include <cstdlib> 10 11 using namespace std; 12 13 const int maxn = 1000005; 14 15 typedef long long LL; 16 17 int A[35][35]; 18 19 int gauss(int m,int n) //m个方程,n个变量 20 { 21 int i=0,j=0,k,r,u; 22 while(i < m && j <n ){ //当前处理i个方程j个变量 23 r = i; 24 for(k = i;k < m;k++){ 25 if(A[k][j]){ 26 r = k; 27 break; 28 } 29 } 30 if(A[r][j]){ 31 if(r != i){ 32 for(k = 0;k <= n;k++) swap(A[r][k],A[i][k]); 33 } 34 for(u = i+1;u < m;u++){ 35 if(A[u][j]) for(k = i;k <= n;k++) A[u][k] ^= A[i][k]; 36 } 37 i++; 38 } 39 j++; 40 41 } 42 for(j = i;j<n;j++){ 43 if(A[j][n]) return -1; 44 } 45 return n-i; 46 } 47 48 int main() 49 { 50 int t; 51 scanf("%d",&t); 52 int s[35],e[35]; 53 while(t--){ 54 memset(A,0,sizeof(A)); 55 memset(s,0,sizeof(s)); 56 memset(e,0,sizeof(e)); 57 int n; 58 scanf("%d",&n); 59 for(int i=0;i<n;i++){ 60 scanf("%d",&s[i]); 61 } 62 for(int i=0;i<n;i++){ 63 scanf("%d",&e[i]); 64 } 65 for(int i=0;i<n;i++){ 66 if(s[i] != e[i]) 67 A[i][n] =1; 68 A[i][i] = 1; 69 } 70 int a,b; 71 while(scanf("%d%d",&a,&b)&&a+b){ 72 a--; 73 b--; 74 A[b][a] = 1; 75 } 76 int ans = gauss(n,n); 77 if(ans == -1) puts("Oh,it‘s impossible~!!"); 78 else printf("%d\n",1<<ans); 79 80 } 81 return 0; 82 }
时间: 2024-10-27 13:57:26