magic square is a square array of non-negative integers where the sums of the numbers on each row, each column, and both main diagonals are the same. An N × N occult square is a magic square with N rows and N columns with additional constraints: • The integers in the square are between 0 and N, inclusive. • For all 1 ≤ i ≤ N, the number i appears at most i times in the square. • There are at least two distinct positive integers in the square. For example, the following is a 5 × 5 occult square, where the sums of the numbers on 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 number P, you are asked to construct a P × P occult square, or determine whether no such occult square exists. Input Input contains a prime number: P (2 ≤ P ≤ 1000) representing the number of rows and columns in the occult square. Output If there is no P × P occult square, simply output -1 in a line. Otherwise, output P lines, each contains P integers representing an occult square. The j th integer on the i th line is the integer on the i th row and the j th column in the occult square. You may output any P × P occult square.
A magic square is a square array of non-negative integers where the sums of the numbers on each row,
each column, and both main diagonals are the same. An N × N occult square is a magic square with N
rows and N columns with additional constraints:
• The integers in the square are between 0 and N, inclusive.
• For all 1 ≤ i ≤ N, the number i appears at most i times in the square.
• There are at least two distinct positive integers in the square.
For example, the following is a 5 × 5 occult square, where the sums of the numbers on 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 number P, you are asked to construct a P × P occult square, or determine whether no
such occult square exists.
Input
Input contains a prime number: P (2 ≤ P ≤ 1000) representing the number of rows and columns in the
occult square.
Output
If there is no P × P occult square, simply output -1 in a line. Otherwise, output P lines, each contains P
integers representing an occult square. The j
th integer on the i
th line is the integer on the i
th row and the
j
th column in the occult square. You may output any P × P occult square.
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)