C. Magic Grid 构造矩阵

C. Magic Grid

time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 (n21)(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 (4n10004≤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.

 

 题意:构造一个这样的矩阵:第 i 行的异或和==第 i 列的异或和

 

题解:若已知一个满足题意的矩阵,对这个矩阵的每一个数加上一个4的倍数依然满足题意,又因为n是4的倍数,所以不断用这样的4*4矩阵凑成一个n*n的矩阵即可

 

#include<iostream>
#include<algorithm>
using namespace std;
int a[1024][1024];
int main()
{
    int n,cnt=0;
    cin>>n;
    for(int i=1;i<=n;i=i+4)
    {
        for(int j=1;j<=n;j=j+4)
        {
            for(int x=0;x<4;x++)
            {
                for(int y=0;y<4;y++)
                {
                    a[i+x][j+y]=cnt++;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(j==1)
                cout<<a[i][j];
            else
                cout<<' '<<a[i][j];
        }
        cout<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/-citywall123/p/11575052.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值