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. notepad++,vim驼峰命名与下划线的互相转换

    notepad++,vim驼峰命名与下划线的互相转换   IDDAY_TIMEDAY_FULL_NAMEDAY_OF_WEEKYEAR_MONTHYREA_NAME 下滑线转驼峰 大写转小写 有这么些 ...

  2. maplotlib python 玩具绘图 横向纵向条状图

    from matplotlib import font_manager#解决zh-han图形汉字乱码 my_font = font_manager.FontProperties(fname=" ...

  3. windows maven配置

    <?xml version="1.0" encoding="UTF-8"?> <!-- Licensed to the Apache Soft ...

  4. stl_string复习

    #include <iostream>#include <string>#include <algorithm>using namespace std; void ...

  5. .NET中的字符串(1):字符串 - 特殊的引用类型

    C# string 特殊的引用类型 .Net 框架程序设计(修订版)中有这样一段描述:String类型直接继承自Object,这使得它成为一个引用类型,也就是说线程上的堆栈上不会驻留有任何字符串.(译 ...

  6. npm 配置国内淘宝镜像

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

  7. kibana 开发工具介绍

    kibana上查看es集群节点信息 get /_cat/nodes?v ip heap.percent ram.percent cpu load_1m load_5m load_15m node.ro ...

  8. cf 76 div2

    传送门 题意 t组样例 n个学生, x最多交换次数 a交换的第一个位置 b交换的第二个位置 最多是交换n-1次,不懂的话可以找个数列自己模拟一下: 然后其他的就是abs(a-b)+x;两个位置之间的距 ...

  9. Uncaught SyntaxError: Unexpected identifier 报错 import Vue from 'vue';

    一般情况是因为Webpack与webpack-dev-server版本不兼容导致. package.json { "name": "vue-loader-demo&quo ...

  10. 02-16Android学习进度报告十六

    今天主要学习了GridView(网格视图)的基本使用和一些基本概念. 下面是GridView中的一些属性: android:columnWidth:设置列的宽度 android:gravity:组件对 ...