#include #define REP(i,n) for(int i=0,i##_len=(n);i>N; vector> ans(N,vector(N,0)); if(N&1){ int row=0,col=N/2,n=0; while(++n){ if(row==-1) row+=N; if(col==N) col-=N; if(ans[row][col]){ row+=2;col--; row%=N; if(col==-1) col+=N; } if(ans[row][col]) break; ans[row][col]=n; row--;col++; } } else if(N%4==0){ int nl=0,nu=0; vector> t(N,vector(N,false)); while(nu> pre(N/2,vector(N/2,0)); int row=0,col=N/4,n=0; while(++n){ if(row==-1) row+=N/2; if(col==N/2) col-=N/2; if(pre[row][col]){ row+=2;col--; row%=N/2; if(col==-1) col+=N/2; } if(pre[row][col]) break; pre[row][col]=n; row--;col++; } REP(i,N/2) REP(j,N/2){ pre[i][j]--; pre[i][j]*=4; } REP(i,N/2) REP(j,N/2){ if(i<=N/4+1){ ans[2*i][2*j]=4+pre[i][j]; ans[2*i][2*j+1]=1+pre[i][j]; ans[2*i+1][2*j]=2+pre[i][j]; ans[2*i+1][2*j+1]=3+pre[i][j]; if(i==N/4&&j==N/4||(i==N/4+1&&j!=N/4)){ swap(ans[2*i][2*j],ans[2*i][2*j+1]); } }else{ ans[2*i][2*j]=1+pre[i][j]; ans[2*i][2*j+1]=4+pre[i][j]; ans[2*i+1][2*j]=3+pre[i][j]; ans[2*i+1][2*j+1]=2+pre[i][j]; } } } REP(i,N) REP(j,N){ cout<