Broken Clock
Time limit30sMemory limit1024 MB
Given three unlabeled clock-hand angles and an unknown rotation, find a time before noon whose hour, minute, and second hands match them.
- Level
Medium7 of 10
- Topics
- Math, Number theory, Implementation
- Solved
- No attempts yet
Problem
Emmett found an old clock in his attic. The clock is a circle with 3 hands attached to the center that rotate clockwise at constant speeds. They are called the hours hand, the minutes hand, and the seconds hand. At midnight, all hands point up. The hours hand completes a full revolution in 12 hours, the minutes hand completes a full revolution in 1 hour, and the seconds hand completes a full revolution in 1 minute. 1 hour is equal to 60 minutes, 1 minute is equal to 60 seconds, and 1 second is equal to nanoseconds.
For example, the clock in the picture below shows exactly 6 hours and 30 minutes after midnight. The hours hand (short black) is halfway between 6 and 7 (it has completed 6.5/12 of a revolution), the minutes hand (long black) points straight down because it has completed exactly 6 and a half full revolutions, and the seconds hand (red) points straight up because it has completed an integer number of full revolutions.

Unfortunately, the hands are broken, so they all look identical, and there is no way to know which hand is which. The clock in the picture above, with its hands broken, would look like this.

In addition, no markings remain that let Emmett know which way is up, so any rotation of the clock could be the correct one (the clock can be rotated, not reflected). To continue the example, the fully broken clock could look like this.

Emmett does know that the time was strictly before noon, that is, strictly less than 12 hours after midnight. Emmett has taken a picture of the clock. Given that picture (represented by the angles of the hands relative to one arbitrary axis), figure out what time it could correspond to.
Note that Emmett has already figured out a viable orientation of the clock in some cases (Test Set 1) and has narrowed the possible times down to a whole number of seconds (Test Sets 1 and 2) or nanoseconds (Test Set 3). See the limits section for details.
Input
The first line of the input gives the number of test cases, . lines follow. Each line describes a test case and contains three integers , , and : the angles of the hands, relative to an arbitrary axis and given in ticks in the clockwise direction. 1 tick is equal to degrees. This means the hours hand rotates exactly 1 tick each nanosecond, the minutes hand rotates exactly 12 ticks each nanosecond, and the seconds hand rotates exactly 720 ticks each nanosecond.
Output
For each test case, output one line containing Case #x: h m s n, where is the test case number (starting from 1) and , , , and are integers: is the number of full hours since midnight (between 0 and 11, inclusive), is the number of full minutes since the last full hour (between 0 and 59, inclusive), is the number of full seconds since the last full minute (between 0 and 59, inclusive), and is the number of full nanoseconds since the last full second (between 0 and , inclusive).
Limits
- .
- .