[SDOI2010]外星千足蟲
阿新 • • 發佈:2021-08-04
傳送門
強化一下高斯消元。
學了線代以後就是不一樣,這題就是線性方程組的水題嘛。
題目已經將異或方程組直接告訴你了,然後問你最少需要幾個方程(按順序)才能有唯一解(保證有解)。
因為要按順序選取方程,所以只要正常的解\(m\)個方程就好了。高斯消元的時候,對於第\(i\)列,我們要找最靠前的一行\(x\),滿足這一行的第\(i\)列為\(1\)。如果沒有,就說明有無窮多解,否則記錄下來\(x\),表示至少需要到第\(x\)個方程,第\(i\)個未知數才能確定。那麼最後需要的最少方程數,就是\(\textrm{max} \{ x \}\).
因為\(O(n^2m)\)會超時,所以用bitset優化,就過了。
#include<cstdio> #include<iostream> #include<cmath> #include<algorithm> #include<cstring> #include<cstdlib> #include<cctype> #include<vector> #include<queue> #include<bitset> #include<assert.h> #include<ctime> using namespace std; #define enter puts("") #define space putchar(' ') #define Mem(a, x) memset(a, x, sizeof(a)) #define In inline #define forE(i, x, y) for(int i = head[x], y; ~i && (y = e[i].to); i = e[i].nxt) typedef long long ll; typedef double db; const int INF = 0x3f3f3f3f; const db eps = 1e-8; const int maxn = 1e3 + 5; In ll read() { ll ans = 0; char ch = getchar(), las = ' '; while(!isdigit(ch)) las = ch, ch = getchar(); while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar(); if(las == '-') ans = -ans; return ans; } In void write(ll x) { if(x < 0) x = -x, putchar('-'); if(x >= 10) write(x / 10); putchar(x % 10 + '0'); } char s[maxn]; int n, m; bitset<maxn> f[maxn << 1]; In int Gauss() { int ret = 0; for(int i = 1; i <= m; ++i) { int pos = i; while(pos <= m && !f[pos][i]) ++pos; if(pos > m) return -1; //無窮解 ret = max(ret, pos); if(pos ^ i) swap(f[i], f[pos]); for(int j = i + 1; j <= m; ++j) if(f[j][i]) f[j] ^= f[i]; if(i == n) break; //已經可以確定唯一解了 } for(int i = n; i; --i) for(int j = i - 1; j; --j) if(f[j][i]) f[j] ^= f[i]; //迴帶 return ret; } int main() { n = read(), m = read(); for(int i = 1, x; i <= m; ++i) { scanf("%s%d", s + 1, &x); for(int j = 1; j <= n; ++j) f[i][j] = s[j] == '1'; f[i][n + 1] = x; } int ans = Gauss(); if(ans == -1) puts("Cannot Determine"); else { write(ans), enter; for(int i = 1; i <= n; ++i) puts(f[i][n + 1] ? "?y7M#" : "Earth"); } return 0; }