P27465 [AtCoder] Magic Square of N
提高+
题目描述

Problem Description

For a grid of $N \times N$ squares, let $ c_{i,j} $ represent the number written in the square located in the $ i $-th row from the top and the $ j $-th column from the left.

Given a natural number $ N $ as input, determine if there exists a configuration that satisfies the following conditions, and if so, output one such configuration.

  • Each square contains one of the numbers $1, 2, \ldots, N^2$
  • All numbers written in the squares are distinct
  • The sum of the numbers in each row, modulo $ N $, are all different
  • The sum of the numbers in each column, modulo $ N $, are all different

Input Format

The input is given from the standard input in the following format:

$ N $

Output Format

If no configuration satisfying the conditions exists, output No in one line.

If a configuration satisfying the conditions exists, output one such configuration in the following format:

Yes $ c_{1,1} $ $ c_{1,2} $ $\ldots$ $ c_{1,N} $ $ c_{2,1} $ $ c_{2,2} $ $\ldots$ $ c_{2,N} $ $\vdots$ $ c_{N,1} $ $ c_{N,2} $ $\ldots$ $ c_{N,N} $

Sample Input and Output

Sample Input #1

3

Sample Output #1

Yes
1 7 9
2 8 5
3 4 6

Sample Input #2

2

Sample Output #2

No

Notes/Hints

Constraints

  • All inputs are integers
  • $1 \le N \le 1000$

Sample Explanation 1

The sums of each row modulo 3 are:
- $ 1 + 7 + 9 = 17 $ which is $ 2 $ modulo 3
- $ 2 + 8 + 5 = 15 $ which is $ 0 $ modulo 3
- $ 3 + 4 + 6 = 13 $ which is $ 1 $ modulo 3

The sums of each column modulo 3 are:
- $ 1 + 2 + 3 = 6 $ which is $ 0 $ modulo 3
- $ 7 + 8 + 4 = 19 $ which is $ 1 $ modulo 3
- $ 9 + 5 + 6 = 20 $ which is $ 2 $ modulo 3

Thus, this configuration satisfies the conditions.

输入格式

见题面 Input 部分

输出格式

见题面 Output 部分

样例输入
3
样例输出
Yes 1 7 9 2 8 5 3 4 6