DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Coprime Grid

Back to problem

Editorial

In this solution, rows and columns are indexed from 000. Thus the row index is one of 0,1,2,3,40,1,2,3,40,1,2,3,4, and the column index is one of 0,1,⋯ ,2N−10,1,\cdots,2N-10,1,⋯,2N−1.

First, use the sieve of Eratosthenes to compute all primes up to 2max⁡Ni2\max N_i2maxNi​. For each NNN, choose two primes p,qp,qp,q such that 2N=p+q2N=p+q, 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 ppp and a right block of width qqq. We first describe the general case, where p<qp<qp<q and p∉{3,5,7}p\notin\{3,5,7\}p∈/. Initially define

Ar,c={1+c+rp(0≤c<p),6p−c+(5−r)q(p≤c<p+q).A_{r,c}= \begin{cases} 1+c+rp & (0\leq c<p),\\ 6p-c+(5-r)q & (p\leq c<p+q). \end{cases}Ar,c​=

The left block uses exactly the numbers 1,2,⋯ ,5p1,2,\cdots,5p1,2,⋯,5p, and the right block uses exactly the numbers 5p+1,5p+2,⋯ ,5p+5q5p+1,5p+2,\cdots,5p+5q5p+1,5p+2,⋯. Hence the whole grid is a permutation of .

Inside the left block, horizontally adjacent numbers differ by 111, so all horizontal pairs are coprime. Vertically adjacent numbers differ by exactly ppp. Therefore, if a vertical pair in the left block is not coprime, both numbers must be multiples of ppp. This happens only in the last column c=p−1c=p-1c=p−1, where the column is

p, 2p, 3p, 4p, 5p.p,\ 2p,\ 3p,\ 4p,\ 5p.p, 2p, 3p, 4p, 5p.

The right block is analogous. Horizontally adjacent numbers differ by 111, so all horizontal pairs are coprime. Vertically adjacent numbers differ by exactly qqq, so the only dangerous column is the column containing multiples of qqq.

We now fix these two columns.

First, fix the column of multiples of ppp in the left block. Originally, A0,1=2A_{0,1}=2A0,1​=2, A0,3=4A_{0,3}=4A, , and . Replace these values as follows:

A0,1=2p,A0,3=4p,A1,p−1=2,A3,p−1=4.A_{0,1}=2p,\quad A_{0,3}=4p,\quad A_{1,p-1}=2,\quad A_{3,p-1}=4.A0,1​=2p,A

Then the last column of the left block becomes

p, 2, 3p, 4, 5p.p,\ 2,\ 3p,\ 4,\ 5p.p, 2, 3p, 4, 5p.

The adjacent pairs in this column are (p,2)(p,2)(p,2), (2,3p)(2,3p)(2,3p), (3p,4)(3p,4)(3p,4), and (4,5p)(4,5p)(, 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 2p2p2p is placed between 111 and 333, and the value below it is p+2p+2p+2. Since p∉{2,3}p\notin\{2,3\}p, 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 qqq in the right block. Let

k=⌊5pq⌋k=\left\lfloor\frac{5p}{q}\right\rfloork=⌊q5p​⌋

and

l=6p−kq.l=6p-kq.l=6p−kq.

Then the multiples of qqq are gathered in column lll. More precisely, column lll contains

(k+5)q, (k+4)q, (k+3)q, (k+2)q, (k+1)q(k+5)q,\ (k+4)q,\ (k+3)q,\ (k+2)q,\ (k+1)q(k+5)q, (k+4)q, (k+3)

from top to bottom.

If kkk is even, apply the following replacements:

A0,k=(k+1)q,A0,k+2=(k+3)q,A0,k+4=(k+5)q,A_{0,k}=(k+1)q,\quad A_{0,k+2}=(k+3)q,\quad A_{0,k+4}=(k+5)q,A0,k​=(k+1)q A0,l=k+5,A2,l=k+3,A4,l=k+1.A_{0,l}=k+5,\quad A_{2,l}=k+3,\quad A_{4,l}=k+1.A0,l​=k+5,A

In other words, we swap the odd-indexed multiples of qqq with small numbers in the first row. Column lll then becomes

k+5, (k+4)q, k+3, (k+2)q, k+1.k+5,\ (k+4)q,\ k+3,\ (k+2)q,\ k+1.k+5, (k+4)q, k+3, (k+

Small numbers and multiples of qqq now alternate. Each small number is coprime to the neighboring coefficient and is smaller than qqq, so all vertical pairs in this column are coprime.

If kkk is odd, apply the following replacements instead:

A0,k+1=(k+2)q,A0,k+3=(k+4)q,A_{0,k+1}=(k+2)q,\quad A_{0,k+3}=(k+4)q,A0,k+1​=(k+2)q,A A1,l=k+4,A3,l=k+2.A_{1,l}=k+4,\quad A_{3,l}=k+2.A1,l​=k+4,A3,l

Then column lll becomes

(k+5)q, k+4, (k+3)q, k+2, (k+1)q.(k+5)q,\ k+4,\ (k+3)q,\ k+2,\ (k+1)q.(k+5)q, k+4, (k+3)q, k

Again, small numbers and multiples of qqq alternate, so all vertical pairs in this column are coprime.

The moved multiples of qqq are placed in small columns of the first row. Their horizontal neighbors are consecutive small numbers, and the value below column ccc is of the form p+c+1p+c+1p+c+1. In the general case p∉{3,5,7}p\notin\{3,5,7\}p∈, 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 rrr, the two boundary values are

(r+1)p,5p+(5−r)q.(r+1)p,\quad 5p+(5-r)q.(r+1)p,5p+(5−r)q.

Suppose they have a common prime divisor. If that divisor is ppp, then ppp must divide (5−r)q(5-r)q(5−r)q, which is impossible because p≠qp\neq qp 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 2N−12N-12N−1 is prime, let q=2N−1q=2N-1q=2N−1. We use one large block of width qqq and one small column. Define

Ar,c={6+c+rq(0≤c<q),5−r(c=q).A_{r,c}= \begin{cases} 6+c+rq & (0\leq c<q),\\ 5-r & (c=q). \end{cases}Ar,c​={

Then column q−6q-6q−6 contains

q, 2q, 3q, 4q, 5q.q,\ 2q,\ 3q,\ 4q,\ 5q.q, 2q, 3q, 4q, 5q.

Swap A1,q−6A_{1,q-6}A1,q−6​ with A1,qA_{1,q}A1,q​, and swap A3, with . The dangerous column becomes

q, 4, 3q, 2, 5q,q,\ 4,\ 3q,\ 2,\ 5q,q, 4, 3q, 2, 5q,

so all vertical pairs in that column are coprime. The only other affected edges are around the small numbers 2,42,42,4 and the moved values 2q,4q2q,4q2q,4q, and they are also coprime by direct checking.

If p=qp=qp=q, we use a slightly different right block:

Ar,c={1+c+rp(0≤c<p),(8−r)p+c+1(p≤c<2p).A_{r,c}= \begin{cases} 1+c+rp & (0\leq c<p),\\ (8-r)p+c+1 & (p\leq c<2p). \end{cases}Ar,c​=

The horizontal edges are still mostly automatic, and the only dangerous places are columns containing multiples of ppp. The implementation first swaps the values 7p,9p7p,9p7p,9p with the small values 7,97,97,9 to separate the dangerous multiples in the right block. Then it fixes the last column of the left block depending on p mod 3p\bmod 3pmod. 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 p=3,5,7p=3,5,7p=3,5,7, the general repair can conflict with small prime divisors. Therefore, the solution uses a precomputed small block of size 5×p5\times p5×p containing the numbers 1,2,⋯ ,5p1,2,\cdots,5p1,2,⋯. 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 N=1,2,3,5N=1,2,3,5N=1,2,3,5 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 max⁡Ni\max N_imaxNi​.
  • Use the sieve of Eratosthenes to compute all primes up to 2max⁡Ni2\max N_i2maxNi​.
  • For each NiN_i, choose the appropriate case above and construct the grid.

The sieve takes O(Mlog⁡log⁡M)O(M\log\log M)O(MloglogM) time, where M=2max⁡NiM=2\max N_iM=2maxNi​. 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 Bi=0B_i=0Bi​=0 for every test case. Therefore it obtains the maximum possible score.

Solution written by GPT5.5

2
N
=
p+
q
p≤qp\leq qp≤q
NNN
NNN
{3,5,7}
{
1+c+rp6p−c+(5−r)q​(0≤c<p),(p≤c<p+q).​
,
5
p
+
5q
1,2,⋯ ,10N1,2,\cdots,10N1,2,⋯,10N
0,3
​
=
4
A1,p−1=2pA_{1,p-1}=2pA1,p−1​=2p
A3,p−1=4pA_{3,p-1}=4pA3,p−1​=4p
0,3
​
=
4p,A1,p−1​=
2,A3,p−1​=
4.
4
,
5
p
)
ppp
∈
/
{2,3}
4p4p4p
333
555
p+4p+4p+4
p∉{2,5}p\notin\{2,5\}p∈/{2,5}
222
444
q
,
 
(
k
+
2)q, (k+
1)q
,
A0,k+2​
=
(k+
3)q,A0,k+4​=
(k+
5)q,
2,l
​
=
k+
3,A4,l​=
k+
1.
2
)
q
,
 
k
+
1.
0,k+3​
=
(k+
4)q,
​
=
k+
2.
+
2, (k+
1)q.
/
{3,5,7}
p=3,5,7p=3,5,7p=3,5,7
=
q
1≤5−r≤5<p1\leq 5-r\leq 5<p1≤5−r≤5<p
r+1r+1r+1
2,3,52,3,52,3,5
222
333
555
6+c+rq5−r
​
(0≤c<q),(c=q).​
q−6A_{3,q-6}
A3,q−6​
A3,qA_{3,q}A3,q​
{
1+c+rp(8−r)p+c+1​(0≤c<p),(p≤c<2p).​
3
p mod 3=1p\bmod 3=1pmod3=1
2p,4p2p,4p2p,4p
2,42,42,4
p,3p,5pp,3p,5pp,3p,5p
1,3,51,3,51,3,5
,
5
p
qqq
5p+15p+15p+1
5p+5q5p+5q5p+5q
qqq
p=3,5,7p=3,5,7p=3,5,7
Ni​
  • Print the grid.
  • O(∑iNi)O\left(\sum_i N_i\right)
    O(∑i​Ni​)
    O(max⁡Ni)O(\max N_i)O(maxNi​)