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. ...
随机推荐
- iframe onload事件触发两次
标准参考 关于 HTML 4.01 规范中 onload 内在事件说明:http://www.w3.org/TR/html401/interact/scripts.html#adef-onload 关 ...
- python 解析json格式
对于网页爬取结果为json格式的,可以直接使用python的json库解析,获取相应字段的值,比用正则匹配更简单规范. import json…… resp=requests.post(url,hea ...
- Bugku-CTF加密篇之散乱的密文(lf5{ag024c483549d7fd@@1} 一张纸条上凌乱的写着2 1 6 5 3 4)
散乱的密文 lf5{ag024c483549d7fd@@1} 一张纸条上凌乱的写着2 1 6 5 3 4
- PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)
1.树控件的基本使用方法QTreeWidget'''QTreeWidget树控件的使用方法添加图标,添加表格,添加复选框等'''from PyQt5.QtWidgets import *from Py ...
- C#上传数据到HTTP,HTTPS 代码示例
string param = string.Format("username={0}&password={1}", account, pwd); string result ...
- !important面试汇总啊
https://www.jianshu.com/p/feab89b88d6b https://juejin.im/post/5c64d15d6fb9a049d37f9c20#heading-57
- 吴裕雄 python 神经网络——TensorFlow 花瓣分类与迁移学习(1)
import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...
- ES-elasticsearch安装-linux
(1)安装JDK(ES是使用java开发的) (2)安装ES(虚拟机内存大于一个g) 1)创建普通用户启动 2)非常占用内存(默认1个g的内存) (3)创建一个普通用户(用于启动ES) groupad ...
- MySQL复制方法
MySQL的二进制日志,MySQL复制原理,MySQL主从模式搭建,MySQL双主模式搭建,MySQL级联模式搭建,MySQL半同步模式复制 一.二进制日志 1.概念 MySQL的二进制日志(bina ...
- 树莓派4B踩坑指南 - (10)安装坚果云(更新:暂不支持)
191209更新: 根据坚果云用户支持(helpdesk@nutstore.net)的官方回复,客户端不支持arm,所以本篇后续内容可以不用看了.. 原文如下: "您好,客户端似乎不支持ar ...