用bitset优化,要不然n^3肯定超时
消元过程中有几点需要注意,找到最大元后break,保证题目中所说的K最小
如果有自由元说明解很多,直接返回
#include <bitset> #include <cstdio> #define N 2050 #define max(x, y) ((x) > (y) ? (x) : (y)) int n, m, ans; std::bitset <N> s[N]; char S[N][N]; inline int read() { int x = 0, f = 1; char ch = getchar(); for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1; for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘; return x * f; } inline bool Gauss() { int i, j, k; for(j = 1; j <= n; j++) { k = j; for(i = j; i <= m; i++) if(s[i][j]) { k = i; ans = max(ans, i); break; } if(!s[k][j]) return 0; if(k != j) swap(s[k], s[j]); for(i = j + 1; i <= m; i++) if(s[i][j]) s[i] ^= s[j]; } for(i = n; i >= 1; i--) for(j = i + 1; j <= n; j++) s[i][n + 1] = s[i][n + 1] ^ (s[i][j] * s[j][n + 1]); } int main() { int i, j, x; n = read(); m = read(); for(i = 1; i <= m; i++) { scanf("%s", S[i] + 1); for(j = 1; j <= n; j++) s[i][j] = S[i][j] - ‘0‘; s[i][n + 1] = read(); } if(Gauss()) { printf("%d\n", ans); for(i = 1; i <= n; i++) if(s[i][n + 1]) puts("?y7M#"); else puts("Earth"); } else puts("Cannot Determine"); return 0; }
时间: 2024-10-29 19:10:56