Luogu2447 [SDOI2010]外星千足虫

题目蓝链

Description

\(n\)个未知数\(x_1, x_2, \cdots, x_n\),给出\(m\)条消息,每条消息选出一些未知数并告诉你他们的和的奇偶性。你的目标是判断每个未知数的奇偶性

如果前\(k\)条消息就可以确定所有未知数的奇偶性,输出\(k\)以及所有未知数的奇偶性,否则输出这是不可能的

\(n \leq 1000, m \leq 2000\)

Solution

抑或方程组的板子题,本质就是用\(bitset\)异或来优化一个方程加上另一个方程的这个过程

另外在找第\(i\)行非零方程是尽量找靠前的,同时更新一下最大值即可

时间复杂度\(\mathcal{O}(\frac{n ^ 3}{64})\)

Code

#include <bits/stdc++.h>

using namespace std;

#define fst first
#define snd second
#define mp make_pair
#define squ(x) ((LL)(x) * (x))
#define debug(...) fprintf(stderr, __VA_ARGS__)

typedef long long LL;
typedef pair<int, int> pii;

template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }

inline int read() {
    int sum = 0, fg = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') fg = -1;
    for (; isdigit(c); c = getchar()) sum = (sum << 3) + (sum << 1) + (c ^ 0x30);
    return fg * sum;
}

const int maxn = 1e3 + 10;
const int maxm = 2e3 + 10;

bitset<maxn> a[maxm], ans;

int n, m;

int main() {
#ifdef xunzhen
    freopen("alien.in", "r", stdin);
    freopen("alien.out", "w", stdout);
#endif

    n = read(), m = read();
    for (int i = 1; i <= m; i++) {
        static char s[maxn];
        scanf("%s", s);
        for (int j = 0; j < n; j++) a[i][j + 1] = s[j] - 0x30;
        a[i][n + 1] = (bool)read();
    }

    int Max = n;
    for (int i = 1; i <= n; i++) {
        int p = i;
        for (int j = i + 1; j <= m; j++)
            if (a[j][i]) { p = j, chkmax(Max, j); break; }
        if (!a[p][i]) { printf("Cannot Determine\n"); return 0; }
        if (p != i) swap(a[p], a[i]);
        for (int j = i + 1; j <= m; j++)
            if (a[j][i]) a[j] ^= a[i];
    }

    for (int i = n; i; i--) {
        for (int j = i + 1; j <= n; j++)
            if (a[i][j] & ans[j]) a[i].flip(n + 1);
        ans[i] = a[i][n + 1];
    }

    printf("%d\n", Max);
    for (int i = 1; i <= n; i++) printf(ans[i] ? "?y7M#\n" : "Earth\n");

    return 0;
}
posted @ 2019-02-06 13:50 xunzhen 阅读(...) 评论(...) 编辑 收藏