zoukankan      html  css  js  c++  java
  • [CQOI2014] 和谐矩阵

    称一个由 (0)(1) 组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1。一个元素相邻的元素包括它本身,及他上下左右的 (4) 个元素。给定矩阵的行数和列数,请计算并输出一个和谐的矩阵。注意所有元素为0的矩阵是不允许的。

    Solution

    即对于矩阵中的每一个点 ((x,y)),需要求出一种方案使得

    [a[x][y] oplus a[x+1][y] oplus a[x-1][y] oplus a[x][y+1] oplus a[x][y-1] = 0 ]

    于是我们列出了 (nm) 个方程,共有 (nm) 个未知量,高斯消元即可

    #include <bits/stdc++.h>
    using namespace std;
    
    const int N = 45;
    const int dx[]={0,0,0,-1,1},dy[]={0,-1,1,0,0};
    
    int n,m,id[N][N],ans[N*N];
    bitset <N*N> a[N*N];
    
    void gauss() {
        for(int i=1;i<=n*m;i++) {
            for(int j=i;j<=n*m;j++) {
                if(a[j][i]>0) {
                    swap(a[i],a[j]);
                    break;
                }
            }
            if(!a[i][i]) ans[i]=1;
            for(int j=i+1;j<=n*m;j++) {
                if(a[j][i]) a[j]^=a[i];
            }
        }
        for(int i=n*m;i>=1;--i) {
            for(int j=i+1;j<=n*m;j++) {
                ans[i]^=(ans[j]*a[i][j]);
            }
        }
    }
    
    signed main() {
        cin>>n>>m;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                id[i][j]=(i-1)*m+j;
            }
        }
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                for(int k=0;k<=4;k++) {
                    int x=i+dx[k],y=j+dy[k];
                    if(x>=1 && y>=1 && x<=n && y<=m) {
                        a[id[i][j]][id[x][y]]=1;
                    }
                }
            }
        }
        gauss();
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=m;j++) {
                cout<<ans[id[i][j]]<<" ";
            }
            cout<<endl;
        }
    }
    
    
  • 相关阅读:
    LightOJ 1370 Bi-shoe and Phi-shoe
    CF410div2 B. Mike and strings
    CF410div2 A. Mike and palindrome
    CF410div2 D. Mike and distribution
    CF798 C. Mike and gcd problem
    LightOJ1282 Leading and Trailing
    入门-2
    入门-1
    铅笔,用用,舍不得放下
    《快乐读书 轻松理财》书摘
  • 原文地址:https://www.cnblogs.com/mollnn/p/12655839.html
Copyright © 2011-2022 走看看