直接对于每一个数建立方程然后求解 消元后直接将自由变元设置为1 通过制约关系解出其他的变量就行了 N^3直接过了= =
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#define PF printf
#define SF scanf
using namespace std;
typedef long long LL;
const int MAXN = 40;
int idx[MAXN+10][MAXN+10], ID;
int pos[MAXN*MAXN+10], ans[MAXN*MAXN+10];
int dx[] = { 0, 0, 0, 1, -1};
int dy[] = { 0, 1, -1, 0, 0};
int n, m;
struct Matrix {
int equ, var;
int A[MAXN*MAXN+10][MAXN*MAXN+10];
void Gauss() {
int row, col;
for(row = 0, col = 0; row < equ && col < var; row++, col++) {
int r = row;
for(int i = r; i < equ; i++)
if(A[i][col]) {
r = i;
break;
}
if(r != row) for(int i = 0; i <= var; i++) swap(A[r][i], A[row][i]);
if(A[row][col] == 0) { row--; continue; }
pos[col] = row;
for(int i = row+1; i < equ; i++) {
if(!A[i][col]) continue;
for(int j = col; j <= var; j++) A[i][j] ^= A[row][j];
}
}
row--;
for(int i = equ-1; i >= 0; i--) {
if( i != pos[row] ) { ans[i] = 1; continue; }
int ret = A[row][var];
for(int j = var-1; j > i; j--) if(A[row][j]) ret ^= ans[j];
ans[i] = ret;
row--;
}
}
} G;
int main()
{
SF("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
idx[i][j] = ID++;
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
for(int k = 0; k <= 4; k++) {
int xx = i + dx[k], yy = j + dy[k];
if( !xx || !yy || xx > n || yy > m ) continue;
G.A[idx[i][j]][idx[xx][yy]] = 1;
}
G.equ = G.var = ID;
G.Gauss();
int cnt=0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) PF("%d ", ans[cnt++]);
puts("");
}
}