DOJ logoDOJ
LoginRegister
DOJ logoDOJ
Call for Tasks
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord account linking.

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
Problems
Recently added problemsSaved problemsWorkbooks
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Editorial

Area Inside a Cycle

Back to problem

Editorial

Algorithm

A route exists if and only if NNN is even and

K=N22−1.K=\frac{N^2}{2}-1.K=2N2​−1.

If this condition does not hold, print No. Otherwise, print Yes and make the following moves:

  • Make N−1N-1N−1 moves U, reaching (1,N)(1,N)(1,N).
  • For x=2,3,⋯ ,Nx=2,3,\cdots,Nx=2,3,⋯,, first make one move . If is even, make moves ; otherwise, make moves .

A feasible case requires printing N2N^2N2 characters and takes O(N2)O(N^2)O(N2) time. An infeasible case takes O(1)O(1)O(1) time. The total time complexity is .

Proof

An odd grid size is impossible.

Color each grid point according to the parity of x+yx+yx+y. Every move changes the color, so any route returning to its starting point has even length. A cycle visiting every point exactly once has length N2N^2N2. Therefore, NNN must be even.

Every valid cycle has the same area.

Two horizontal or vertical unit grid segments can intersect or overlap only by sharing a grid point. No point is revisited except for the final return to the starting point. Thus, a valid cycle forms a simple polygon.

The polygon lies inside the square . Every integer grid point strictly inside it must therefore belong to . However, the cycle visits every point in , placing all of them on its boundary. Consequently, the number of interior grid points is .

Subtask 1

For N≤4N\leq 4N≤4, exhaustively enumerate all routes while recording the visited points. Whenever a route visits every point and can return to the start in one more move, compute its area with the shoelace formula. Store a route for each area found, then look up the requested KKK.

After the first move, the immediately preceding point cannot be visited again, so each extension considers at most 333 directions. The search for one size NNN takes O(3N2)O(3^{N^2}) time. The only possible sizes are , so perform the search once per size and reuse the results.

N
R
xxx
N−2N-2N−2
D
N−2N-2N−2
U
  • Make one move D, followed by N−1N-1N−1 moves L.
  • O(T+∑N2)O(T+\sum N^2)
    O(T+∑N2)
    [1,N]×[1,N][1,N]\times[1,N][1,N]×[1,N]
    SSS
    SSS
    I=0I=0I=0

    The boundary consists of N2N^2N2 unit grid segments and contains exactly B=N2B=N^2B=N2 distinct grid points. By Pick's theorem, its area is

    I+B2−1=N22−1.I+\frac{B}{2}-1=\frac{N^2}{2}-1.I+2B​−1=2N2​−1.

    Hence, the requested area KKK must equal this value.

    The construction visits every point when NNN is even.

    The first stage visits the entire column x=1x=1x=1. The middle stage visits all points with 2≤y≤N2\leq y\leq N2≤y≤N in each column from x=2x=2x=2 through x=Nx=Nx=N, alternating between downward and upward traversal. These points are distinct and do not overlap with the first stage.

    Since NNN is even, the last column is traversed downward, ending at (N,2)(N,2)(N,2). The final stage visits the remaining points in the row y=1y=1y=1 and returns to the start. Thus, every grid point is visited exactly once, apart from the final return to the starting point.

    The total number of moves is

    (N−1)+(N−1)(N−1)+1+(N−1)=N2.(N-1)+(N-1)(N-1)+1+(N-1)=N^2.(N−1)+(N−1)(N−1)+1+(N−1)=N2.

    Its area equals the value proved above, so the stated condition is also sufficient.

    O(3N2)
    1,2,3,41,2,3,41,2,3,4

    Solution written by GPT6