BZOJ 3503 [CQOI2014]和谐矩阵

题目链接

BZOJ 3503

题解

没想到……直接用暴力的\(O((nm)^3)\)算法,居然能过?!

高斯消元解异或方程组。

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#define space putchar(' ')
#define enter putchar('\n')
typedef long long ll;
using namespace std;
template <class T>
void read(T &x){
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
    if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
    x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x){
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}

const int N = 1605;
int n, m, x, g[N][N], ans[N];
const int dx[5] = {0, -1, 1, 0, 0};
const int dy[5] = {0, 0, 0, -1, 1};

void gauss(){
    for(int i = 1; i <= x; i++){
    if(!g[i][i])
        for(int j = i + 1; j <= x; j++)
        if(g[j][i]){
            for(int k = 1; k <= x + 1; k++)
            swap(g[i][k], g[j][k]);
            break;
        }
    for(int j = i + 1; j <= x; j++)
        if(g[j][i])
        for(int k = i; k <= x + 1; k++)
            g[j][k] ^= g[i][k];
    }
    for(int i = x; i; i--){
    if(!g[i][i]) ans[i] = 1;
    else{
        for(int j = i + 1; j <= x; j++)
        g[i][x + 1] ^= ans[j] & g[i][j];
        ans[i] = g[i][x + 1];
    }
    }
}

int main(){

    read(n), read(m);
    for(int i = 1; i <= n; i++)
    for(int j = 1; j <= m; j++){
        x++;
        for(int d = 0; d <= 4; d++){
        int ti = i + dx[d], tj = j + dy[d];
        if(ti <= n && tj <= m && ti && tj)
            g[x][(ti - 1) * m + tj] = 1;
        }
    }
    
    gauss();
    for(int i = 1; i <= x; i++)
    write(ans[i]), i % m ? space: enter;
    
    return 0;
}

转载于:https://www.cnblogs.com/RabbitHu/p/BZOJ3503.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值