#C2666. Prime Matrix Construction
Prime Matrix Construction
Prime Matrix Construction
Alex is fascinated by prime numbers. In his latest project, he wants to construct an m × m matrix of positive integers with the following property:
- The sum of the elements in each row is a prime number.
- The sum of the elements in each column is a prime number.
- The sum of the elements in both of the main diagonals (the primary diagonal and the secondary diagonal) is a prime number.
Your task is to determine whether such a matrix exists given an integer m. If it exists, you should output "YES" followed by one valid matrix; otherwise, output "NO".
Note: For this problem, it turns out that a valid matrix exists if and only if either m = 1 or m is a prime number greater than 2. In particular, even though 2 is a prime, no valid 2×2 matrix exists, and similarly for composite values of m (for example, m = 4). If m satisfies the existence condition then one simple solution is to output a matrix where every entry is 1 (except when m = 1, output a 1×1 matrix with a prime number, e.g. 2). This works because when m > 2 is prime, each row, each column, and both diagonals sum to m, which is prime.
inputFormat
The input consists of a single integer m
read from standard input.
outputFormat
If a valid matrix exists, output YES
on the first line, then output m lines each containing m space-separated positive integers representing the matrix. If no valid matrix exists, output NO
.
3
YES
1 1 1
1 1 1
1 1 1
</p>