Editorial for COCI '23 Contest 5 #5 Trokut
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 . When we connect two points with a line segment,
we divide the game into two independent "boards" of sizes
and
such that
. Now we can
calculate the Grundy number of each state, i.e., for each
using the previous ones using the following
equation:
where denotes minimum excludant value. It is now obvious that Lucija will win for some
if
and only if
. The complexity of this algorithm is
.
To achieve the maximum number of points on the task, it was necessary to notice that the sequence
enters a period of length
after a relatively small pre-period, which allows us to efficiently determine the
Grundy numbers for any
. We did not prove some of the claims and leave them as the exercise to
the reader.
Comments