#Q26. 「一本通 1.3 练习 3」质数方阵
「一本通 1.3 练习 3」质数方阵
Description
Original Source: USACO 4.3.2
A prime square is a matrix where every row, column, and both diagonals form a 5-digit prime number when read from left to right (for rows and diagonals) or top to bottom (for columns). Each of these primes must have digits that sum to the same given value. The top-left corner number is predetermined. A prime number may appear multiple times in the matrix. Note that 5-digit primes with leading zeros (e.g., ) are not considered valid.

Given the sum of the digits for each prime and the top-left corner number, output all possible valid configurations of the matrix. If there are multiple solutions, output all of them (sorted by the 25-digit number formed by concatenating all 25 digits in the matrix).
Input Format
A single line containing two space-separated integers: the sum of the digits and the top-left corner number.
Output Format
For each solution found, output 5 lines, each containing 5 characters representing a 5-digit prime. Separate different solutions with an empty line. If no solution exists, output a single line NONE.
Sample 1
11 1
11351
14033
30323
53201
13313
11351
33203
30323
14033
33311
13313
13043
32303
50231
13331