Averages in Place
Time limit1sMemory limit64 MB
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 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 numbers in the table are different.
Find a table that makes Slavko happy.
Input
The first line contains the integer ().
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 and smaller than .
- If , print on the first line. No table satisfies the conditions for .
- If is odd, print through in row order, so the value in row , column is .
- If is even and , define by for every with , and . The value in row , column is .
Print the table on lines, each line holding integers separated by one space.
Hint
In the odd table the average of row is , which sits in the middle cell of row . The average of column is , which sits in the middle cell of column . For the row averages are , , and the column averages are , , .
For even the average of is , and . That is why the row average lands in its row and the column average lands in its column. Since is larger than the gap between the largest and the smallest , the values are all different.
For 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.