QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 128 MB Total points: 100 Hackable ✓

#15291. Magic Square

Statistics

A magic square is a special $N \times N$ matrix: it is composed of the numbers $1, 2, 3, \dots, N \times N$, and the sum of the numbers in each row, each column, and both diagonals are the same.

When $N$ is odd, we can construct a magic square using the following method:

First, write $1$ in the middle of the first row.

After that, fill in each number $K$ ($K = 2, 3, \dots, N \times N$) in increasing order according to the following rules:

  1. If $(K-1)$ is in the first row but not in the last column, then $K$ is placed in the last row, in the column to the right of $(K-1)$.
  2. If $(K-1)$ is in the last column but not in the first row, then $K$ is placed in the first column, in the row above $(K-1)$.
  3. If $(K-1)$ is in the last row and the last column, then $K$ is placed directly below $(K-1)$.
  4. If $(K-1)$ is neither in the first row nor in the last column, and the cell to the upper-right of $(K-1)$ is not yet filled, then $K$ is placed in the upper-right of $(K-1)$; otherwise, $K$ is placed directly below $(K-1)$.

Given $N$, please construct the $N \times N$ magic square using the method described above.

Input

The input consists of a single line containing an integer $N$, representing the size of the magic square.

Output

The output contains $N$ lines, each containing $N$ integers, representing the $N \times N$ magic square constructed by the method above. Adjacent integers in a row are separated by a single space.

Examples

Input 1

3

Output 1

8 1 6
3 5 7
4 9 2

Input 2

5

Output 2

17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9

Constraints

For $100\%$ of the data, $1 \le N \le 39$ and $N$ is odd.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.