Editorial for COCI '12 Contest 6 #6 Bakterije
Submitting an official solution before solving the problem yourself is a bannable offence.
Take a look at a bacterium: its state can be described using three parameters , and , where and present a row/column mark of the cell in which the bacterium currently is, and is the direction bacterium is facing. If we simulate the movement of the bacterium, we can notice the bacterium will eventually encounter a state it had been in before and, since the rules of moving do not change, the bacterium has closed the loop in which it will cycle forever (or by the end of the game). The path of the bacterium will be as shown in the picture:
Circles present the states of the bacterium, and arrows its movement. Part of the path which is not in the loop is called "tail". Let be the number of states in the loop of the bacterium, and the second in which the bacterium first encountered a state . If the state is on the "tail", this second is the only one in which the bacterium is in a state , and if it is in the loop, it will be there in seconds:
Assume that in the final second of the game, the bacterium will be in a state , where , marks a trapped cell. If some bacterium is in that state for the first and the only time (regardless of the game end), then it is trivial to check if other bacteria will be in the final state in that second. If not, we have a system of equations:
which can also be presented using congruences:
This problem is solved using the Chinese remainder theorem. Since the theorem offers a solution if the modules ( in our case) are pairwise coprime, we have to split every to coprime factors (powers of primes). We thus get a greater number of equations but the modules will now be pairwise coprime. In case there are two powers of the same prime number among the modules, we can eliminate the smaller one because the remainder of division by it is uniquely determined from the remainder of division by the greater one (if they do not match, there is no solution).
Comments