Occult Square
Time limit1sMemory limit512 MB
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 occult square is a magic square with rows and columns that also satisfies these conditions:
- Every integer in the square is between 0 and , inclusive.
- For each , the number appears at most 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 , construct a occult square, or determine that none exists.
Input
The input contains one prime number (), the number of rows and columns of the occult square.
Output
If no occult square exists, print -1 on a line by itself. Otherwise, print lines with integers each. The th integer on the th line is the number in row , column of the square. Any valid occult square is accepted.