Official

D - Loong and Takahashi Editorial by en_translator


By filling values on a spiral from the outer to the inner, one can always construct an answer independent of \(N\). We consider what value fills each square based on this approach.

Sample \((N=7)\)

 1  2  3  4  5  6  7
24 25 26 27 28 29  8
23 40 41 42 43 30  9
22 39 48  T 44 31 10
21 38 47 46 45 32 11
20 37 36 35 34 33 12
19 18 17 16 15 14 13

For implementation, one can do as follows.

  1. Prepare an empty grid
  2. Put \(1\) to the top-left square. Let the current square be the top-left square, and the direction be right.
  3. While there is an empty square, repeat the following.
    • If the square adjacent to the current square in the current direction is empty, advance to that square and fill it with the next value.
    • Otherwise, rotate the direction clockwise by \(90\) degrees.

writer’s solution (C)

Bonus:
When \(N=45\), there are \(45^2-1=2024\) squares to fill in. あけましておめでとうございます!

posted:
last update: