Editorial for LCC '25 Contest 1 S5 - Moonlight's Box Challenge


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.

Author: Moonlight

Notice that since each dimension is independent, all we need to determine is whether or not there's a time t such that for all dimensions, AndyMan68's position is on one of the endpoints of this dimension. This implies that for any dimension, the direction is oscillating between v_i and -v_i. It can be shown that it doesn't matter if we reverse directions, as we're just trying to determine whether or not AndyMan68's position is divisible by L_i.

Subtask 1

Notice that the positions p_i in modulo L_i repeats after lcm(L_1, L_2) units of time. This means that we can run a brute force algorithm to check each unit of time to see if \overrightarrow{p} + t * \overrightarrow{v} = k * \overrightarrow{L}.

This can be done in O(lcm(L_1, L_2)) time. An optimized search will pass under Python, Java or C++.

Subtask 2

This subtask was to help users visualize the problem.

Since the LCMs are now too large to individually check each point in time, we can only consider when the current position is on one of the edges. This can be simulated with a priority queue, pushing in the current position and the time elapsed to get to the current position, and checking each position to see if it lies on a corner.

This can be simulated in O(Nlog(N)*max(L_i)) time.

Subtask 3

Note that the dimensions with v_j = 0 need to be handled separately.

Note that it is now not feasible to check each dimension individually. After doing some math, we can determine that the congruence (v_iL_j)k_j \equiv (v_ip_j-v_jp_i) \mod v_kL_i, where j represents a dimension such that v_j \neq 0, and k_j represents the final coordinate of the jth dimension.

To check if this system of congruences has a solution, we can check pairwise equations to check if there exists a solution to the two congruences. It can be shown that if all pairwise congruences have a solution, then the entire system has a solution.

This can be done in O(N^2) time.

Note: A very optimized solution to subtask 2 can pass the final subtask, but is not feasible to do within contest(I could not come up with a solution that passes).


Comments

There are no comments at the moment.