高斯消元模板, 判断是否有解以及变元的数量。
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include <set> 9 #include <string> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 using namespace std; 14 #define pb(x) push_back(x) 15 #define ll long long 16 #define mk(x, y) make_pair(x, y) 17 #define lson l, m, rt<<1 18 #define mem(a) memset(a, 0, sizeof(a)) 19 #define rson m+1, r, rt<<1|1 20 #define mem1(a) memset(a, -1, sizeof(a)) 21 #define mem2(a) memset(a, 0x3f, sizeof(a)) 22 #define rep(i, n, a) for(int i = a; i<n; i++) 23 #define fi first 24 #define se second 25 typedef pair<int, int> pll; 26 const double PI = acos(-1.0); 27 const double eps = 1e-8; 28 const int mod = 1e9+7; 29 const int inf = 1061109567; 30 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 31 double a[50][50], ans[50]; 32 int n, l[50]; 33 int gauss(){ 34 int i, j, k, r = 0; 35 double tmp; 36 mem(l); 37 for(i = 0; i<n; i++){ 38 for(j = r; j<n; j++) 39 if(fabs(a[j][i])>eps){ 40 for(k = i; k<=n; k++) 41 swap(a[j][k],a[r][k]); 42 break; 43 } 44 if(fabs(a[r][i])<eps) 45 continue; 46 for(j = 0; j<n; j++) 47 if(j != r && fabs(a[j][i])>eps){ 48 tmp = a[j][i]/a[r][i]; 49 for(k = i; k<=n; k++) 50 a[j][k] -= tmp*a[r][k]; 51 } 52 l[i] = 1; 53 r++; 54 } 55 for(i = 0; i<n; i++) 56 if(l[i]) { 57 for(j = 0; j<n; j++) 58 if(fabs(a[j][i])>eps) 59 ans[i] = a[j][n]/a[j][i]; 60 } 61 for(i = r; i<n; i++) 62 if(fabs(a[i][n])>eps) 63 return -1; 64 return 1<<n-r; 65 } 66 int u[30], v[30]; 67 int main() 68 { 69 ios::sync_with_stdio(0); 70 int t; 71 cin.tie(0); 72 cin>>t; 73 while(t--) { 74 cin>>n; 75 mem(a); 76 for(int i = 1; i<=n; i++) { 77 cin>>u[i]; 78 } 79 for(int i = 1; i<=n; i++) { 80 cin>>v[i]; 81 a[i-1][n] = u[i]^v[i]; 82 a[i-1][i-1] = 1; 83 } 84 int x, y; 85 while(cin>>x>>y) { 86 if(x+y==0) 87 break; 88 a[y-1][x-1] = 1; 89 } 90 int ret = gauss(); 91 if(ret == -1) { 92 cout<<"Oh,it's impossible~!!"<<endl; 93 } else { 94 cout<<ret<<endl; 95 } 96 } 97 return 0; 98 }