Under construction

Still being built. Things may change

Averages in Place

Time limit1sMemory limit64 MB

Summary
Given N, print the one fixed N x N table whose row and column averages each land inside their own row and column, with -1 for N=2.
Level

Easy3 of 10

Topics
Implementation, Math, Array, Simulation
Solved
No attempts yet

Problem

Slavko is bored, so he fills an N×NN \times N table with positive integers. He is happy when all three conditions hold.

  • The average of the numbers in each row is an integer, and that integer is in the same row.
  • The average of the numbers in each column is an integer, and that integer is in the same column.
  • All N2N^2 numbers in the table are different.

Find a table that makes Slavko happy.

Input

The first line contains the integer NN (1≤N≤1001 \le N \le 100).

Output

Many tables satisfy the conditions, so this problem fixes one answer. Print exactly the table defined below. Every number in it is greater than 00 and smaller than 10910^9.

  • If N=2N = 2, print −1-1 on the first line. No table satisfies the conditions for N=2N = 2.
  • If NN is odd, print 11 through N2N^2 in row order, so the value in row ii, column jj is N(i−1)+jN(i-1)+j.
  • If NN is even and N≥4N \ge 4, define s1,s2,…,sNs_1, s_2, \ldots, s_N by sk=ks_k = k for every kk with 1≤k≤N−11 \le k \le N-1, and sN=3N/2s_N = 3N/2. The value in row ii, column jj is 2N(si−1)+sj2N(s_i - 1) + s_j.

Print the table on NN lines, each line holding NN integers separated by one space.

Hint

In the odd table the average of row ii is N(i−1)+(N+1)/2N(i-1) + (N+1)/2, which sits in the middle cell of row ii. The average of column jj is N(N−1)/2+jN(N-1)/2 + j, which sits in the middle cell of column jj. For N=3N = 3 the row averages are 22, 55, 88 and the column averages are 44, 55, 66.

For even N≥4N \ge 4 the average of s1,s2,…,sNs_1, s_2, \ldots, s_N is N/2+1N/2 + 1, and sN/2+1=N/2+1s_{N/2+1} = N/2 + 1. That is why the row average lands in its row and the column average lands in its column. Since 2N2N is larger than the gap between the largest and the smallest sks_k, the N2N^2 values are all different.

For N=2N = 2 the average of two numbers equals one of them only when the two numbers are equal, and the conditions forbid equal numbers, so no table exists.

Examples2

  1. Example 1

    Input
    3
    
    Expected output
    1 2 3
    4 5 6
    7 8 9
    
  2. Example 2

    Input
    2
    
    Expected output
    -1