C. Magic Grid 构造矩阵
C. Magic Grid
1 second
256 megabytes
standard input
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.
The only line of input contains an integer nn (4≤n≤10004≤n≤1000). It is guaranteed that nn is a multiple of 44.
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.
4
8 9 1 13
3 12 7 5
0 2 4 11
6 10 15 14
8
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
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 构造矩阵的更多相关文章
- 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. ...
- Number Sequence(HDU 1005 构造矩阵 )
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- hdu 5015 233 Matrix(构造矩阵)
http://acm.hdu.edu.cn/showproblem.php?pid=5015 由于是个二维的递推式,当时没有想到能够这样构造矩阵.从列上看,当前这一列都是由前一列递推得到.依据这一点来 ...
- poj 2888 Magic Bracelet(Polya+矩阵快速幂)
Magic Bracelet Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 4990 Accepted: 1610 D ...
- AMR11A - Magic Grid
Thanks a lot for helping Harry Potter in finding the Sorcerer's Stone of Immortality in October. Did ...
- [数学-构造矩阵]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的公式!!! ...
- poj 3735 Training little cats(构造矩阵)
http://poj.org/problem?id=3735 大致题意: 有n仅仅猫,開始时每仅仅猫有花生0颗,现有一组操作,由以下三个中的k个操作组成: 1. g i 给i仅仅猫一颗花生米 2. e ...
- HDU 3306 Another kind of Fibonacci ---构造矩阵***
Another kind of Fibonacci Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Jav ...
- 构造矩阵解决这个问题 【nyoj299 Matrix Power Series】
矩阵的又一个新使用方法,构造矩阵进行高速幂. 比方拿 nyoj299 Matrix Power Series 来说 给出这样一个递推式: S = A + A2 + A3 + - + Ak. 让你求s. ...
随机推荐
- LVS+Keepalived -----> 效率最高的负载均衡器
一. 简介 1. 负载均衡的类型 负载均衡可以采用硬件设备(例如常常听见的 F5),也可以采用软件负载 商用硬件负载设备成本通常较高(一台几十万甚至上百万),所以一般 情况下会采用软件负载 软件负 ...
- C语言:将3*4矩阵中找出行最大,列最小的那个元素。-将低于平均值的人数作为函数返回值,将低于平均分的分数放入below数组中。
//将3*4矩阵中找出行最大,列最小的那个元素. #include <stdio.h> #define M 3 #define N 4 void fun(int (*a)[N]) { ,j ...
- Centos7 将应用添加快捷方式到applications 中以pycham为例[ubuntu]适用
安装版本pycharm-2019.1.3 安装路径:/opt/pycharm-2019.1.3/ vim /usr/share/applications/pycharm.desktop #!/usr/ ...
- Ubuntu各个版本的镜像下载地址
http://mirrors.melbourne.co.uk/ubuntu-releases/
- Linux 下忘记mysql 密码
ERROR (): Access denied for user 'root'@'localhost' (using password: YES). 一.停止 mysql 数据库 /etc/init. ...
- JPA 级联保存的问题
前提:系统有学校-学生关系,学校可以包含多个学生,学生只能属于一个学校 在使用 spring-data-jpa 的时候,保存学校的同时保存学生信息,不需要先逐个保存学生信息,再将学生信息放在学校中保存 ...
- PCSearch
1.hinstance:GetModuleHandle(NULL) 2.窗口直角: 方法1:在Oncreate函数中添加以下代码,然而这种方法会导致窗口阴影无效 LONG styleValue = : ...
- P4710 平抛运动
题目:https://www.luogu.org/problemnew/show/P4710 $$ v_x = v_{x_0} = v \ sin \ \theta, \ v_y = v_{y_0} ...
- 「USACO5.5」矩形周长Picture
题目描述 墙上贴着许多形状相同的海报.照片.它们的边都是水平和垂直的.每个矩形图片可能部分或全部的覆盖了其他图片.所有矩形合并后的边长称为周长. 编写一个程序计算周长. 如图1所示7个矩形. 如图2所 ...
- uniGUI之uniEdit(23)
主要新加属性: a]EmptyText在没有文本里显示的背景文本. b]作为其他控件的编辑输入控件. c]ClearButton清空按钮 d]左边显示文本 e]回车触发事件CharEOL:=#13 f ...