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 (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.

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

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

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

C. Magic Grid 构造矩阵的更多相关文章

  1. POJ 3233 Matrix Power Series(构造矩阵求等比)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  2. Number Sequence(HDU 1005 构造矩阵 )

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. hdu 5015 233 Matrix(构造矩阵)

    http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...

  4. poj 2888 Magic Bracelet(Polya+矩阵快速幂)

    Magic Bracelet Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 4990   Accepted: 1610 D ...

  5. AMR11A - Magic Grid

    Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...

  6. [数学-构造矩阵]NEFU 1113

    依据题意.我已经推导出tn的公式.ti=ti.a+ti.b,ti.a=5*t(i-1).a+4*t(i-1).b,ti.b=t(i-1).a+t(i-1).b 然而以下居然不能继续推到sn的公式!!! ...

  7. poj 3735 Training little cats(构造矩阵)

    http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...

  8. HDU 3306 Another kind of Fibonacci ---构造矩阵***

    Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Jav ...

  9. 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】

    矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...

随机推荐

  1. hadoop中block副本的放置策略

    下面的这种是针对于塔式服务器的副本的放置策略

  2. centos610无桌面安装libreoffice

    Centos610系列配置 #安装文件 yum -y install libreoffice #安装中文包 yum -y install libreoffice-langpack-zh-Han* #安 ...

  3. PB 数据窗口点击标题不能排序的一个原因

    标题必须和数据行名称一致,如 数据行列名为:num ,标题行必须为 num_t 才可以

  4. juypter

    juypter 1.      jupyter 1.1.    它是什么? Jupyter Notebooks 是一款开源的网络应用,我们可以将其用于创建和共享代码与文档. 以前叫ipython no ...

  5. Docker安装部署ELK教程(Elasticsearch+Kibana+Logstash+Filebeat)

    Elasticsearch 是个开源分布式搜索引擎,它的特点有:分布式,零配置,自动发现,索引自动分片,索引副本机制,restful风格接口,多数据源,自动搜索负载等. Logstash 是一个完全开 ...

  6. 802.1X与Cisco基于身份的网络服务(IBNS)

    Cisco基于身份的网络服务(Identity-Based Networking Services,IBNS)是一种以IEEE802.1X标准为基础的安全架构,具有认证.用户策略.访问控制等多种功能, ...

  7. 【Android多线程】异步信息处理机制

    https://www.bilibili.com/video/av65170691?p=3 (本文为此视频听课笔记) 一.线程和线程之间为什么要进行通讯 各线程之间要传递数据 二.线程和线程之间如何通 ...

  8. Vue系列(六)之常用指令v-model

    v-model 基本使用 修饰符 .trim .number .lazy 前面讲到的插值,其实都是单向绑定,数据变-->视图变.有些元素可以与用户交互,比如input,select等,那么我们希 ...

  9. 删除Autorun.inf的方法

    你的电脑的每个分区根目录都有一个autorun.inf的文件夹,查看属性是只读+隐藏,且无法删除.无法取得权限!点进去,却显示的是控制面板的内容? 其实这个不是病毒,而是用来防病毒,一些系统封装工具本 ...

  10. Vacuum Pump Manufacturer - Vacuum Pump Range Use: Considerations

    The vacuum pump is a versatile bottle that holds your lotion, shampoo and conditioner. Keep away fro ...