Editorial
In this solution, rows and columns are indexed from . Thus the row index is one of , and the column index is one of .
First, use the sieve of Eratosthenes to compute all primes up to . For each , choose two primes such that , with . In the implementation, it is enough to check primes close to first. Under the constraints of this problem, such a pair exists for every required .
The main idea is to split the grid into a left block of width and a right block of width . We first describe the general case, where and . Initially define
The left block uses exactly the numbers , and the right block uses exactly the numbers . Hence the whole grid is a permutation of .
Inside the left block, horizontally adjacent numbers differ by , so all horizontal pairs are coprime. Vertically adjacent numbers differ by exactly . Therefore, if a vertical pair in the left block is not coprime, both numbers must be multiples of . This happens only in the last column , where the column is
The right block is analogous. Horizontally adjacent numbers differ by , so all horizontal pairs are coprime. Vertically adjacent numbers differ by exactly , so the only dangerous column is the column containing multiples of .
We now fix these two columns.
First, fix the column of multiples of in the left block. Originally, , , , and . Replace these values as follows:
Then the last column of the left block becomes
The adjacent pairs in this column are , , , and , and all of them are coprime because is an odd prime.
We also need to check the other edges affected by this replacement. The value is placed between and , and the value below it is . Since , all these pairs are coprime. Similarly, is placed between and , and the value below it is ; since , no bad pair is created. The values and placed in the last column are also adjacent only to odd numbers or to numbers not divisible by the relevant small factors, so they create no bad pair.
Next, find the column of multiples of in the right block. Let
and
Then the multiples of are gathered in column . More precisely, column contains
from top to bottom.
If is even, apply the following replacements:
In other words, we swap the odd-indexed multiples of with small numbers in the first row. Column then becomes
Small numbers and multiples of now alternate. Each small number is coprime to the neighboring coefficient and is smaller than , so all vertical pairs in this column are coprime.
If is odd, apply the following replacements instead:
Then column becomes
Again, small numbers and multiples of alternate, so all vertical pairs in this column are coprime.
The moved multiples of are placed in small columns of the first row. Their horizontal neighbors are consecutive small numbers, and the value below column is of the form . In the general case , these values do not introduce a common divisor. This is exactly why the small-prime cases are treated separately.
It remains to check the boundary between the two blocks. In row , the two boundary values are
Suppose they have a common prime divisor. If that divisor is , then must divide , which is impossible because and . Otherwise, the divisor must divide , so the only possible small primes are . The prime is ruled out by parity, and the cases where or could matter are precisely among the small-prime cases handled separately. Thus the boundary pairs are also coprime in the general case.
Now consider the exceptional cases.
If is prime, let . We use one large block of width and one small column. Define
Then column contains
Swap with , and swap with . The dangerous column becomes
so all vertical pairs in that column are coprime. The only other affected edges are around the small numbers and the moved values , and they are also coprime by direct checking.
If , we use a slightly different right block:
The horizontal edges are still mostly automatic, and the only dangerous places are columns containing multiples of . The implementation first swaps the values with the small values to separate the dangerous multiples in the right block. Then it fixes the last column of the left block depending on . If , it moves to the first row and places in the last column, as in the general case. Otherwise, it moves to the first row and places in the last column. In both cases, only constantly many edges are affected, and each can be checked directly.
For , the general repair can conflict with small prime divisors. Therefore, the solution uses a precomputed small block of size containing the numbers . The large block of width contains the numbers through in a regular pattern. The small block and the boundary are chosen so that they contain only good pairs, and the single dangerous column of multiples of in the large block is fixed by a few swaps. Since these cases occur only for , storing the small patterns directly in the code is sufficient.
Finally, very small values such as are handled by hardcoded grids. For these values, the grid is small enough that the permutation condition and all adjacency conditions can be checked directly.
The algorithm is as follows.
- Read all input values and compute .
- Use the sieve of Eratosthenes to compute all primes up to .
- For each , choose the appropriate case above and construct the grid.
- Print the grid.
The sieve takes time, where . Printing the grids takes time proportional to the total output size, namely . The memory usage is for the prime table and one constructed grid.
This construction makes every adjacent pair a good pair, so it achieves for every test case. Therefore it obtains the maximum possible score.
Solution written by GPT5.5