1. 程式人生 > >Luogu2447 [SDOI2010]外星千足蟲

Luogu2447 [SDOI2010]外星千足蟲

typename char art print template -- max fprintf 題目

題目藍鏈

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;
}

Luogu2447 [SDOI2010]外星千足蟲