ENIGMA-0x00 (2022) - S2 (Magic Squares)

View as PDF

Submit solution

Points: 15 (partial)
Time limit: 1.0s
Memory limit: 16M

Authors:
Problem types
Allowed languages
Blockly, C, C#, C++, Fortran, Go, Haskell, Java, Lua, Pascal, Perl, PHP, Python
Magic Squares

Totos needs to finally complete his term paper in computer science. The topic he got was magic squares. He's reading on Wikipedia:

"A magic square is an arrangement of numbers in an array of equal sets of rows and columns, where the arithmetic operation between numbers in the same row or column or diagonal of the square always returns the same result. This common result is called the magic constant of the magic square. The most common arithmetic operation in magic squares is addition between numbers, and there are other versions... "

Example of a 3x3 magic square where the sum of the numbers in each row, column and diagonal equals 15.

The algorithm for its construction is giving him a headache. It seems easier for him to use a random number generator and select the ones that form a magic square.

We want to write a program that reads from the input STDIN a positive integer N, followed by N^2 unique positive integers P_i, separated by a space character (' ') or a line break ('\n'), and checks whether the given square is a magic square. The program will print STDOUT output "NO " if the input square does not correspond to a magic square or will print "YES " on the first line of output followed by a number S on the next line if the given square corresponds to a magic square.

  • N (1 \le N \le 100) represents the size of the side of the square.
  • P_i (1 \le P_i \le N^2, 1 \le i \le N^2) denote each unique number of the square.
  • S denotes the sum of a row, column or diagonal of the magic square.

Attention! Each input or output line should end with the line change character '\n' and the words "YES" and "NO" are written in roman characters.

Examples
1st

STDIN

3
2 7 6
9 5 1
4 3 8

STDOUT

YES
15
2nd

STDIN

3
2 7 4
1 8 9
6 3 5

STDOUT

NO

Comments

There are no comments at the moment.