Editorial for COCI '10 Contest 4 #3 Astro


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.

First, convert all timestamps to minutes. Now, find the minute at which both stars flash. How? Let A and B be minutes at which the stars flashed and let P and Q be the periods (in minutes) between consecutive flashes, accordingly. The first star thus flashes at the following minutes: A, A+P, A+2P, \dots. More generally: it flashes at minutes A + K \times P for non-negative integers K.

Assume that flash overlapping, if it occurs, will occur for K less than 10^6 (it can be proved that taking K less than 3\,000 suffices, which is left as an exercise for the reader). For each such K, we determine whether the second star will flash at the minute A + K \times P. Note that this will happen whenever the equation A + K \times P = B + L \times Q holds. Therefore, it is sufficient to check whether L = (A + K \times P - B) / Q is a non-negative integer - in that case, the flash occurs, indeed.

Once the first overlapping is found, the corresponding minute needs to be converted to days and hours and outputted. In case none was found for any K, we output Never.


Comments

There are no comments at the moment.