#P5512. [NOIP 1997 提高组] 棋盘问题 加强版

[NOIP 1997 提高组] 棋盘问题 加强版

Description

On an N×NN \times N chessboard (1N101 \le N \le 10), fill in N2N ^ 2 numbers 1,2,,N21, 2, \dots, N ^ 2, such that the sum of any two adjacent numbers is a prime number.

For example, when N=2N = 2, one solution is:

11 22
44 33

The adjacent pairs whose sums are prime are:

1+2,1+4,4+3,2+31+2,1+4,4+3,2+3

When N=4N = 4, one possible filling is:

11 22 1111 1212
1616 1515 88 55
1313 44 99 1414
66 77 1010 33

Here we require that the top-left cell must contain the number 11.

Input Format

One line with an integer NN.

Output Format

If there are multiple solutions, output the arrangement with the smallest sum of the first row and the first column. If there is no solution, output NO.

2
1 2
4 3
1
NO

Hint

N10N\leq10

For N=1,2,,10N=1,2,\dots,10, each has one test point. For some reasons, NN is not necessarily the same as the test point ID.


The testdata was newly fixed on 2020.1.20.

Translated by ChatGPT 5