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

Good Triangles

Back to problem

Editorial

Suppose the three points have the same color. Let the vector from AAA to BBB be (a,b)(a,b)(a,b) and the vector from AAA to CCC be (c,d)(c,d)(c,d). By the same-color condition, both a+ba+ba+b and c+dc+dc+d are even. Hence a,ba,ba,b have the same parity, and c,dc,dc,d also have the same parity.

The parallelogram area is

W=∣ad−bc∣.W=|ad-bc|.W=∣ad−bc∣.

Because of the parity condition above, ad−bcad-bcad−bc is always even. Therefore, for WWW to be prime, we must have W=2W=2W=2.

We count ordered triples (A,B,C)(A,B,C)(A,B,C) first, then divide the answer by 666. Fix a vector u=(a,b)u=(a,b)u=(a,b) and count vectors v=(c,d)v=(c,d) satisfying

ad−bc=±2.ad-bc=\pm 2.ad−bc=±2.

Let g=gcd⁡(a,b)g=\gcd(a,b)g=gcd(a,b). The linear Diophantine equation ad−bc=±2ad-bc=\pm 2ad−bc=±2 has an integer solution only if ggg divides . Using the extended Euclidean algorithm, find one solution . Then all solutions are of the form

(c,d)=(c0+agt, d0+bgt).(c,d)=\left(c_0+\frac{a}{g}t,\ d_0+\frac{b}{g}t\right).(c,d)=(c0​+g

For each ttt, the following conditions must hold.

  • ccc and ddd must lie in the possible vector range inside the grid.
  • c+dc+dc+d must be even.

For each valid vector pair (u,v)(u,v)(u,v), count how many positions of AAA make all three points A,A+u,A+vA,A+u,A+vA,A+u,A+v lie inside the grid. This is obtained by computing the required span in the -coordinates and -coordinates.

After summing all ordered triples, divide by 666 to obtain the answer.

Solution written by GPT5.5

v=(c,d)
222
v0=(c0,d0)v_0=(c_0,d_0)v0​=(c0​,d0​)
a
​
t
,
 
d0​
+
gb​
t
)
.
xx
x
yyy