当前位置: 移动技术网 > IT编程>开发语言>C/C++ > 洛谷P2447 [SDOI2010]外星千足虫(异或方程组)

洛谷P2447 [SDOI2010]外星千足虫(异或方程组)

2019年02月10日  | 移动技术网IT编程  | 我要评论

育儿,理财通收益,小八义

题意

题目链接

sol

异或高斯消元的板子题。

bitset优化一下,复杂度\(o(\frac{nm}{32})\)

找最优解可以考虑高斯消元的过程,因为异或的特殊性质,每次向下找的时候找到第一个1然后交换就行,这样显然是最优的

#include<bits/stdc++.h>
using namespace std;
const int maxn = 2001;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m;
bitset<maxn> b[maxn];
void gauss() {
    int ans = 0;
    for(int i = 1; i <= n; i++) {
        int j = i;
        while(!b[j][i] && j < m + 1) 
            j++;
        if(j == m + 1) {puts("cannot determine"); return ;}
        ans = max(ans, j);
        swap(b[i], b[j]);
        for(int j = 1; j <= m; j++) {
            if(i == j || !b[j][i]) continue;
            b[j] ^= b[i];
        }
    }
    printf("%d\n", ans);
    for(int i = 1; i <= n; i++)
        puts(!b[i][n + 1] ? "earth" : "?y7m#");
}
int main() {
    n = read(); m = read();
    for(int i = 1; i <= m; i++) {
        string s; cin >> s; 
        b[i][n + 1] = read();
        for(int j = 1; j <= n; j++) b[i][j] = (s[j - 1] == '0' ? 0 : 1);
    }
    gauss();
    return 0;
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网