Editorial for Wesley's Anger Contest 3 Problem 7 - Mirrors
Submitting an official solution before solving the problem yourself is a bannable offence.
For , there is always a solution. We can select any two arbitrary points. For or , we need to determine if there is a path of at least points. We can try all possible triplets of nodes and determine if the direction vector between the first and second points is equal to the direction vector between the second and third points after reflecting on the second mirror.
For the second subtask, we can select a pair of points to use as an initial direction vector, and repeatedly find the next mirror on the path taken by the laser and reflect on that mirror. This can be done by simply iterating through each mirror, and determining if the vector from the last mirror hit to the mirror in consideration is in the same direction as the direction vector, which takes time for each reflection. Although it seems that this takes time in total, it is almost impossible for each possible pair of initial direction vectors to create a path of length due to the random location of mirrors. Care should be taken to avoid -bit integer overflow; if the direction vector cannot be reduced to a value where the absolute value of both coordinates is less than , then it is impossible for there to be another mirror that it can hit.
For the third subtask, we can observe that if there is a path that passes through of the mirrors, then if a random mirror is selected, it's very likely that it is on this path. Thus, we do not need to try all pairs of points for the initial direction vector, and instead select a few random points, and try all direction vectors for each of those random points. Since each trial has a chance of success, if we select random points, there is a chance our solution is correct if such a path exists. If we do not find a path, then we can assume that a path does not exist.
For the final subtask, we need to quickly find the next mirror on the path given the last mirror the laser hit, and a direction vector (a ray). There are multiple methods to accomplish this.
The first method involves dividing the coordinate plane (bounded by ) into a grid of squares of the same size. Since the mirrors are randomly scattered, the expected number of points in each square is . To determine if a point is on the ray, we can start with the current square, and move to an adjacent square that the ray also passes through. It can be seen that the ray passes through at most squares.
Alternatively, one can observe that on average, the number of integer coordinates (with ) on the ray is fairly small. We can iterate through all of them by taking constant multiples of the reduced direction vector.
Edit: Turns out that there are cases that can break this solution that take around 1.5 times the time limit, however they were not included in the data.