bzoj 1923 [Sdoi2010]外星千足虫 高斯消元

题面

题目传送门

解法

学习了怎么用高斯消元解一个异或方程组

其实和普通的高斯消元是一样的

在多少个方程后就确定答案可以直接边做边取max即可

用bitset优化异或

时间复杂度:\(O(\frac{nm^2}{w})\)

代码

#include <bits/stdc++.h>
using namespace std;
template <typename node> void chkmax(node &x, node y) {x = max(x, y);}
template <typename node> void chkmin(node &x, node y) {x = min(x, y);}
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
int n, m;
bitset <1010> a[2010];
int gauss() {
    int now = 0, ret = 0;
    for (int i = 1; i <= n; i++) {
        int j = now + 1;
        while (!a[j][i] && j <= m) j++;
        if (j == m + 1) return -1;
        chkmax(ret, j);
        swap(a[++now], a[j]);
        for (int k = 1; k <= m; k++)
            if (k != now && a[k][i]) a[k] ^= a[now];
    }
    return ret;
}
int main() {
    read(n), read(m);
    for (int i = 1; i <= m; i++) {
        char c = getchar();
        while (!isdigit(c)) c = getchar();
        for (int j = 1; j <= n; j++)
            a[i][j] = c - '0', c = getchar();
        int t; read(t); a[i][n + 1] = t;
    }
    int t = gauss();
    if (t == -1) return cout << "Cannot Determine\n", 0;
    cout << t << "\n";
    for (int i = 1; i <= n; i++)
        if (a[i][n + 1]) cout << "?y7M#\n";
            else cout << "Earth\n";
    return 0;
}

转载于:https://www.cnblogs.com/copperoxide/p/9476733.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值