Under construction

Still being built. Things may change

Occult Square

Time limit1sMemory limit512 MB

Summary
Given a prime P, build a P by P magic square with entries 0 to P, each i used at most i times, and two distinct positive values, or print -1.
Level

Medium7 of 10

Topics
Math, Matrix, Implementation
Solved
No attempts yet

Problem

A magic square is a square array of non-negative integers in which the sums of the numbers in each row, each column, and both main diagonals are equal. An N×NN \times N occult square is a magic square with NN rows and NN columns that also satisfies these conditions:

  • Every integer in the square is between 0 and NN, inclusive.
  • For each 1≤i≤N1 \le i \le N, the number ii appears at most ii times in the square.
  • The square contains at least two distinct positive integers.

For example, the following is a 5 × 5 occult square. The sums of each row, each column, and both main diagonals are 7.

0 0 0 3 4
2 4 0 0 1
0 0 3 4 0
5 0 0 0 2
0 3 4 0 0

For a given prime PP, construct a P×PP \times P occult square, or determine that none exists.

Input

The input contains one prime number PP (2≤P≤10002 \le P \le 1000), the number of rows and columns of the occult square.

Output

If no P×PP \times P occult square exists, print -1 on a line by itself. Otherwise, print PP lines with PP integers each. The jjth integer on the iith line is the number in row ii, column jj of the square. Any valid P×PP \times P occult square is accepted.

Examples2

  1. Example 1

    Input
    5
    
    Expected output
    0 0 0 3 4
    2 4 0 0 1
    0 0 3 4 0
    5 0 0 0 2
    0 3 4 0 0
    
  2. Example 2

    Input
    2
    
    Expected output
    -1