高斯消元解异或方程组。学了bitset。对比如下
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){int x=0;char c=getchar();while(!isdigit(c)) c=getchar();while(isdigit(c)) x=x*10+c-'0',c=getchar();return x;
}
const int nmax=50;
const int inf=0x7f7f7f7f;
int xx[5]={0,0,0,1,-1};
int yy[5]={0,1,-1,0,0};
int a[nmax*nmax][nmax*nmax],ans[nmax*nmax],n,m;
int id(int x,int y){return (x-1)*m+y;
}
void Gauss(int N,int M){REP(i,1,N){int j;for(j=i;j<=N&&!a[j][i];j++)if(j>N) continue;if(j!=i) REP(k,i,M) swap(a[i][k],a[j][k]);REP(j,i+1,N) if(a[j][i]) REP(k,i,M) a[j][k]^=a[i][k];}dwn(i,N,1){if(!a[i][i]) ans[i]=1;else{REP(j,i+1,N) a[i][M]^=(ans[j]*a[i][j]);ans[i]=a[i][M];}}
}
int main(){n=read(),m=read();REP(i,1,n) REP(j,1,m) REP(k,0,4) {int tx=i+xx[k],ty=j+yy[k];if(tx&&ty&&tx<=n&&ty<=m) a[id(i,j)][id(tx,ty)]=1;}Gauss(n*m,n*m+1);REP(i,1,n) {REP(j,1,m) printf("%d ",ans[id(i,j)]);printf("\n");}return 0;
}
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<bitset>
using namespace std;
#define REP(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define clr(x,c) memset(x,c,sizeof(x))
int read(){int x=0;char c=getchar();while(!isdigit(c)) c=getchar();while(isdigit(c)) x=x*10+c-'0',c=getchar();return x;
}
const int nmax=2505;;
const int inf=0x7f7f7f7f;
int xx[5]={0,0,0,1,-1};
int yy[5]={0,1,-1,0,0};
int ans[nmax],n,m;
bitset<nmax>a[nmax];
int id(int x,int y){return (x-1)*m+y;
}
void Gauss(int N,int M){REP(i,1,N){int j;for(j=i;j<=N&&!a[j][i];j++)if(j>N) continue;if(j!=i) swap(a[j],a[i]);REP(j,i+1,N) if(a[j][i]) a[j]^=a[i];}dwn(i,N,1){if(!a[i][i]) ans[i]=1;else{REP(j,i+1,N) if(a[i][j]) ans[i]^=ans[j];//a[i][M]^=ans[j];//a[i][M]^=(ans[j]*a[i][j]);}}
}
int main(){n=read(),m=read();REP(i,1,n*m) a[i].reset();REP(i,1,n) REP(j,1,m) REP(k,0,4) {int tx=i+xx[k],ty=j+yy[k];if(tx&&ty&&tx<=n&&ty<=m) a[id(i,j)][id(tx,ty)]=1;}Gauss(n*m,n*m+1);REP(i,1,n) {REP(j,1,m) printf("%d ",ans[id(i,j)]);printf("\n");}return 0;
}
3503: [Cqoi2014]和谐矩阵
Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 877 Solved: 397
[Submit][Status][Discuss]
Description
我们称一个由0和1组成的矩阵是和谐的,当且仅当每个元素都有偶数个相邻的1。一个元素相邻的元素包括它本
身,及他上下左右的4个元素(如果存在)。
给定矩阵的行数和列数,请计算并输出一个和谐的矩阵。注意:所有元素为0的矩阵是不允许的。
Input
输入一行,包含两个空格分隔的整数m和n,分别表示矩阵的行数和列数。
Output
输出包含m行,每行n个空格分隔整数(0或1),为所求矩阵。测试数据保证有解。
Sample Input
4 4
Sample Output
0 1 0 0
1 1 1 0
0 0 0 1
1 1 0 1
数据范围
1 <=m, n <=40
1 1 1 0
0 0 0 1
1 1 0 1
数据范围
1 <=m, n <=40
HINT
Source
鸣谢Ljcc提供Spj