Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 1701 | Accepted: 537 |
Description
Input
Output
Sample Input
2 3 0 0 0 1 1 1 1 2 1 3 2 1 2 3 3 1 3 2 0 0 3 0 0 0 1 0 1 1 2 2 1 0 0
Sample Output
4 Oh,it's impossible~!!
Hint
Source
/* 第一个高斯消元 公式 (sum(func[i], for i = 0 to n - 1) + ss[j]) mod 2 = se[j] */ #include <iostream> #include <cmath> #define MAX_N 29 using namespace std; int func[MAX_N + 1][MAX_N + 1]; //第n位用来表示等式右边的常数 int ss[MAX_N + 1], se[MAX_N + 1]; //保存初始和结束状态 int n; void swap(int &v1, int &v2) { int temp = v1; v1 = v2; v2 = temp; } int gauss() { int row, col, rp, cp; for(row = 0, col = 0; row < n, col < n; row++, col++) { int maxRow = row; for(rp = row + 1; rp < n; rp++) if(func[rp][col] > func[maxRow][col]) maxRow = rp; if(maxRow != row) for(cp = col; cp <= n; cp++) swap(func[row][cp], func[maxRow][cp]); if(func[row][col] == 0) { row--; continue; } for(rp = row + 1; rp < n; rp++) { if(func[rp][col] == 0) continue; for(cp = col; cp <= n; cp++) func[rp][cp] = (func[rp][cp] - func[row][cp] + 2) % 2; } } for(rp = row; rp < n; rp++) if(func[rp][n] != 0) return -1; //无解 if(row == n) return 1; //唯一解 return pow(2.0, n - row); } int main() { int caseN, i; scanf("%d", &caseN); while(caseN--) { memset(func, 0, sizeof(func)); scanf("%d", &n); for(i = 0; i < n; i++) scanf("%d", &ss[i]); for(i = 0; i < n; i++) scanf("%d", &se[i]); int from, to; while(scanf("%d%d", &from, &to) && (from + to) != 0) func[to - 1][from - 1] = 1; for(i = 0; i < n; i++) { func[i][n] = (se[i] - ss[i] + 2) % 2; func[i][i] = 1; } int res = gauss(); if(res == -1) printf("Oh,it's impossible~!!/n"); else printf("%d/n", res); } return 0; }