BZOJ1923 SDOI2010 外星千足虫 高斯消元

题意:给定一个K个N元mod 2方程,若有解,求用前几个方程即可出解和各个变量为奇数还是偶数,若无解输出:Cannot Determine题解:有关01异或方程的解法可以看莫涛的论文。这个题就可以看作是异或方程——虽然是加法,不过只考虑二进制最后一位的话和异或是一样的。

#include <bitset>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAXN=1000+2;
const int MAXM=2000+2;
int N,M,ans;
bitset<MAXM> a[MAXN];

bool Gauss(){
    int now=0;
    for(int i=1,j;i<=N;i++){
        j=now+1;

        while(j<=M && !a[j][i]) j++;
        if(j>M) return 0;
        else ans=max(ans,j);

        now++,swap(a[j],a[now]);
        for(int j=1;j<=M;j++)
            if(j!=now && a[j][i])
                a[j]^=a[now];
    }
    return 1;
}

int main(){
    scanf("%d %d",&N,&M);
    for(int i=1,x;i<=M;i++){
        for(int j=1;j<=N;j++){
            scanf("%1d",&x);
            a[i][j]=x;
        }
        scanf("%d",&x);
        a[i][N+1]=x;
    }

    if(!Gauss()) cout << "Cannot Determine" << endl;
    else{
        cout << ans << endl;
        for(int i=1;i<=N;i++)
            if(a[i][N+1]) cout << "?y7M#" << endl;
            else cout << "Earth" << endl;
    }

    return 0;
}
View Code

 

posted @ 2017-02-26 11:05  WDZRMPCBIT  阅读(155)  评论(0编辑  收藏  举报