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. 刷题15. 3Sum

    一.题目说明 题目非常简洁15. 3Sum,读懂题目后,理解不难. 但 实话说,我们提交代码后,Time Limit Exceeded,最主要的是给了非常长的测试用例,我本地运行后87秒,确实时间非常 ...

  2. npm 配置国内淘宝镜像

    淘宝NPM镜像官网:http://npm.taobao.org/ npm config set registry=http://registry.npm.taobao.org -g 安装cnpm: n ...

  3. 【代码总结】MYSQL数据库的常见操作

    ============================== MYSQL数据库的常见操作 ============================== 一.mysql的连接与关闭 -h:指定所连接的服 ...

  4. k8s 的pod进阶

    容器探测的具体实现方法:三种探针类型 ExecAction.TCPSocketAction.HTTPGetAction lifecycle <Object> Actions that th ...

  5. 25 JavaScript对象原型&ES5新的对象方法

    JavaScript对象原型 所有JavaScript对象都从原型继承对象和方法 日期对象继承自Date.prototype,数组继承自Array.prototype,对象构造器新建的对象Person ...

  6. 吴裕雄--天生自然TensorFlow2教程:函数优化实战

    import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D def himme ...

  7. 小程序云函数调用http或https请求外部数据

    参考网址 https://blog.csdn.net/qiushi_1990/article/details/101220920 小程序云函数调用http或https请求外部数据 原创编程小石头 发布 ...

  8. mssql-osql

    mssql导入单行字段值非常长,或者sql文件非常大,比如上百M或者更大,常规方法是导不进去的,所以推荐下面方式进行导入. osql -S . -U sa -P 123456 -d TS_TEST - ...

  9. 【内容摘录自 MDN】变量作用域

    有如下自定义函数:(此函数为全局函数,任何地方均可调用) function output(value) { var para = document.createElement('p'); docume ...

  10. Centos7 [ubuntu] 安装pycharm2019.1.3并永久破解教程

    一.安装pycharm2019专业版并激活步骤 1.拉取安装包 # wget   https://download.jetbrains.com/python/pycharm-professional- ...