C. Magic Grid

https://codeforces.com/problemset/problem/1208/C

Let us define a magic grid to be a square matrix of integers of size n×nn×n, satisfying the following conditions.

  • All integers from 00 to (n2−1)(n2−1) inclusive appear in the matrix exactly once.
  • Bitwise XOR of all elements in a row or a column must be the same for each row and column.

You are given an integer nn which is a multiple of 44. Construct a magic grid of size n×nn×n.

Input

The only line of input contains an integer nn (4≤n≤10004≤n≤1000). It is guaranteed that nn is a multiple of 44.

Output

Print a magic grid, i.e. nn lines, the ii-th of which contains nn space-separated integers, representing the ii-th row of the grid.

If there are multiple answers, print any. We can show that an answer always exists.

Examples

input

Copy

4

output

Copy

8 9 1 13
3 12 7 5
0 2 4 11
6 10 15 14

input

Copy

8

output

Copy

19 55 11 39 32 36 4 52
51 7 35 31 12 48 28 20
43 23 59 15 0 8 16 44
3 47 27 63 24 40 60 56
34 38 6 54 17 53 9 37
14 50 30 22 49 5 33 29
2 10 18 46 41 21 57 13
26 42 62 58 1 45 25 61

Note

In the first example, XOR of each row and each column is 1313.

In the second example, XOR of each row and each column is 6060.


好嘛最开始猜就是 

0 1 2 3

4 5 6 7

8 9 10 11

12 13 14 15

这样的矩阵,然后没去算.至于怎么发现这个矩阵的....构造题很玄学。

考虑到题目只出4的倍数,猜测是一种暗示,暗示分成4x4的块,后面的数在前面的数上加16,32....

因为现在这个1号矩阵为位是2^0,2^1,2^2,2^3。隔壁2号矩阵加上16的话,在2^4加,而偶次2^4进行异或答案还是0

这道题获得的小规律:以偶数开始连续两个数如4^5=1;以偶数开始连续四个数如2^3^4^5=0;

要真是赛场上的话这种没啥特别性的构造还是往特殊了去构造

#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<cstdio>
#include<algorithm>
#define debug(a) cout<<#a<<"="<<a<<endl;
using namespace std;
const int maxn=1e3+100;
typedef long long LL;
LL a[maxn][maxn];
LL cnt=0;
void solve(LL x,LL y)
{
	for(LL i=0;i<4;i++)
		for(LL j=0;j<4;j++){
			a[x+i][y+j]=cnt++;
		} 
}
int main(void)
{
  cin.tie(0);std::ios::sync_with_stdio(false);
  LL n;
  cin>>n;
  for(LL i=1;i<=n;i+=4)
  	for(LL j=1;j<=n;j+=4)
  	{
  		solve(i,j);	
	}
  for(LL i=1;i<=n;i++)
  {
  	for(LL j=1;j<=n;j++)
	  {
		 cout<<a[i][j]<<" ";	  	
	  }
	cout<<endl;  
  }
return 0;
}

猜你喜欢

转载自blog.csdn.net/zstuyyyyccccbbbb/article/details/108331275