#B3940. Magic Square Construction

    ID: 11597 Type: Default 1000ms 256MiB

Magic Square Construction

Magic Square Construction

In an N×N grid, a magic square is constructed by filling each cell with the positive integers from 1 to N×N such that the sums of the numbers in each row, each column, and both main diagonals are all equal. In ancient China, these were called "Hetu", "Luoshu", or "Zonghengtu".

When N is odd, there is a simple method known as the Siamese method. The steps are as follows:

  1. Start with an empty grid. Place the number 1 in the center of the first row.
  2. For each subsequent number, move one cell up and one cell to the right. If moving up goes out of bounds (i.e. above the first row), wrap around to the last row; similarly, if moving right goes out of bounds (i.e. past the last column), wrap around to the first column. If the target cell is empty, place the next number there.
  3. If the cell is already occupied, instead move one cell directly down from the last filled cell. (It is guaranteed this cell is empty.)
  4. Repeat steps 2 and 3 until all cells are filled and the magic square is complete.

For example, a 3×3 magic square generated by this algorithm is:

8 1 6
3 5 7
4 9 2

Note that in the formulas below, use the standard LaTeX format. For example, the grid is of size $N\times N$, and the numbers range from $1$ to $N^2$.

inputFormat

The input consists of a single odd integer N ($1 \leq N \leq 99$, and N is guaranteed to be odd), representing the size of the magic square.

outputFormat

Output the constructed magic square. Each of the N lines should contain N integers separated by a single space.

sample

3
8 1 6

3 5 7 4 9 2

</p>