P3164 [CQOI2014]和諧矩陣
阿新 • • 發佈:2018-12-06
位運算太強啦->題解
//minamoto #include<bits/stdc++.h> #define ll long long #define R register #define fp(i,a,b) for(R int i=a,I=b+1;i<I;++i) #define fd(i,a,b) for(R int i=a,I=b-1;i>I;--i) #define go(u) for(R int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v) using namespace std; const int N=45,dx[]={1,0,-1,0,0},dy[]={0,1,0,-1,0}; int n,m,h,a[N],b[N];ll lim,res,las,s;bool f[N][N],ans[N][N]; void dfs(int x){ if(x>h){ res=las=0; fp(i,1,h)b[i]=a[i]; for(R int i=(m&1)?h-1:h,k=h;i;--i)b[++k]=a[i]; fp(i,1,m)res=res<<1|(b[i-1]^b[i]^b[i+1]),las=las<<1|b[i]; fp(i,2,n)s=res,res=las^res^(res<<1&lim)^(res>>1),las=s; if(res)return; fp(i,1,m)if(ans[1][i]=b[i])fp(j,0,4)f[1+dx[j]][i+dy[j]]^=1; fp(i,2,n)fp(j,1,m)if(f[i-1][j]&1){ ans[i][j]=1; fp(k,0,4)f[i+dx[k]][j+dy[k]]^=1; }for(R int i=1;i<=n;++i,puts(""))fp(j,1,m)printf("%d ",ans[i][j]); exit(0); }a[x]=1,dfs(x+1),a[x]=0,dfs(x+1); } int main(){ // freopen("testdata.in","r",stdin); scanf("%d%d",&n,&m),h=(m+1)>>1,lim=(1ll<<m)-1; dfs(1);return 0; }