DOJ logoDOJ
Problems
Recently added problemsSaved problemsWorkbooks
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

Take the L

Back to problem

Editorial

Every L takes both cells of one column and one cell of an adjacent column. It therefore creates an empty column, separating the remaining cells on its two sides into independent games.

In each interval between empty columns, only the end columns may have a single cell; every interior column still has two cells. Every move preserves this property.

Let kkk be the number of remaining cells in an interval. Every move leaves two intervals of sizes aaa and k−3−ak-3-ak−3−a. Conversely, every 0≤a≤k−30\le a\le k-30≤a≤k−3 is achievable: list the cells column by column from left to right. Any three consecutive cells contain both cells of one column and a cell of a neighboring column, so taking the three cells immediately after the first aaa cells realizes that split. Other orientations produce no additional size pairs.

Thus the Grundy number depends only on the number of cells. Write it as G(k)G(k)G(k). Then

G(0)=G(1)=G(2)=0,G(0)=G(1)=G(2)=0,G(0)=G(1)=G(2)=0,

and for k≥3k\ge3k≥3,

G(k)=mex⁡{G(a)⊕G(k−3−a)∣0≤a≤k−3}.G(k)=\operatorname{mex}\{G(a)\oplus G(k-3-a)\mid 0\le a\le k-3\}.G(k)=mex{G(a)⊕G(k−3−a)∣

Here mex⁡\operatorname{mex}mex is the smallest nonnegative integer absent from a set, and ⊕\oplus⊕ is bitwise XOR.

Initially there is one interval with 2N2N2N cells. Print YES if G(2N)≠0G(2N)\ne0G(2N)=0, and NO otherwise.

Let MMM be the largest input value of NNN. Precomputing through G(2M)G(2M)G(2M) takes O(M2)O(M^2)O(M2) time and space; each test case then takes time. Total time is and space including stored input is .

For subtask 1, N≤10N\le10N≤10. Use a bitmask of the 2N2N2N cells and memoized search over every legal L. Cache the answer for each distinct NNN. With K=max⁡N≤10K=\max N\le10K=maxN≤, the bounds are time and at most space. Subtask 2 uses the Grundy recurrence above.

Solution written by GPT

0≤
a≤
k−
3}.
O(M)O(M)
O(M)
O(1)O(1)O(1)
O(M2+T)O(M^2+T)O(M2+T)
O(M+T)O(M+T)O(M+T)
10
O(K4K+T)O(K4^K+T)O(K4K+T)
O(4K+T)O(4^K+T)O(4K+T)