Editorial for COCI '23 Contest 5 #5 Trokut


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For 13 points, it was sufficient to recursively determine whether a game state is winning or losing. However, it is necessary to optimize the solution using memoization technique.

To solve the second subtask worth 36 points, we need to notice the following: the player who uses a point that is already connected to another point by a line segment in their move will lose in the next move. Note that this is also a necessary condition for a player to lose. Therefore, we can play a similar and equivalent game to the previous one: a player on their turn connects two line segments so that no two line segments intersect (or touch), and the loser is the player who can no longer make a move.

Let's consider the initial state of the game for some N. When we connect two points with a line segment, we divide the game into two independent "boards" of sizes l and r such that l+r = N-2. Now we can calculate the Grundy number of each state, i.e., for each N using the previous ones using the following equation:

\displaystyle g[n] = mex\{g[l] \oplus g[r] : l+r = n-2\}

where mex\{\dots\} denotes minimum excludant value. It is now obvious that Lucija will win for some N if and only if g[N] > 0. The complexity of this algorithm is \mathcal{O}(N^2).

To achieve the maximum number of points on the task, it was necessary to notice that the sequence g[n] enters a period of length 34 after a relatively small pre-period, which allows us to efficiently determine the Grundy numbers for any N \le 10^9. We did not prove some of the claims and leave them as the exercise to the reader.


Comments

There are no comments at the moment.