This page is still under construction.

Parts of this page are still being built. What you see may change.

Clock Hands

Time limit5sMemory limit128 MB

Summary
The program finds the earliest time at or after the given time when the second hand bisects the angle between the other two hands with no overlap.
Level

Medium7 of 10

Topics
Math, Geometry
Solved
No attempts yet

Problem

An analog clock has three hands (the second hand, the minute hand and the hour hand) that all rotate smoothly. You can measure the two angles between the second hand and the two other hands.

Write a program that finds the earliest time, at or after a given time, where "no two hands overlap each other" and "the two angles between the second hand and the two other hands are equal" both hold.

Figure D.1. Angles between the second hand and the two other hands

Clocks are not limited to 12-hour clocks. The hour hand of an HH-hour clock goes around once in HH hours. The minute hand still goes around once every hour, and the second hand goes around once every minute. At 0:0:0 (midnight) all three hands point straight up.

Input

The input consists of multiple datasets. Each dataset is one line with four integers HH, hh, mm and ss, separated by a space. HH means the clock is an HH-hour clock, and hh, mm, ss are the hour, minute and second of the specified time.

You may assume 2≤H≤1002 \le H \le 100, 0≤h<H0 \le h < H, 0≤m<600 \le m < 60 and 0≤s<600 \le s < 60.

The end of the input is a line containing four zeros.

Figure D.2. Examples of an HH-hour clock (a 6-hour clock and a 15-hour clock)

Output

For each dataset, output on one line the time TT at which "no two hands overlap each other" and "the two angles between the second hand and the two other hands are equal" hold for the first time at or after the specified time.

For TT being hoh_o:mom_o:sos_o (sos_o seconds past mom_o minutes past hoh_o o'clock), output four non-negative integers hoh_o, mom_o, nn and dd, separated by a space, where n/dn/d is the irreducible fraction representing sos_o. When sos_o is an integer, including 0, let dd be 1.

The time is expressed as the remainder modulo HH hours. In other words, one second after (H−1)(H-1):59:59 is 0:0:0, not HH:0:0.

Examples1

  1. Example 1

    Input
    12 0 0 0
    12 11 59 59
    12 1 56 0
    12 1 56 3
    12 1 56 34
    12 3 9 43
    12 3 10 14
    12 7 17 58
    12 7 18 28
    12 7 23 0
    12 7 23 31
    2 0 38 29
    2 0 39 0
    2 0 39 30
    2 1 6 20
    2 1 20 1
    2 1 20 31
    3 2 15 0
    3 2 59 30
    4 0 28 48
    5 1 5 40
    5 1 6 10
    5 1 7 41
    11 0 55 0
    0 0 0 0
    
    Expected output
    0 0 43200 1427
    0 0 43200 1427
    1 56 4080 1427
    1 56 47280 1427
    1 57 4860 1427
    3 10 18600 1427
    3 10 61800 1427
    7 18 39240 1427
    7 18 82440 1427
    7 23 43140 1427
    7 24 720 1427
    0 38 4680 79
    0 39 2340 79
    0 40 0 1
    1 6 3960 79
    1 20 2400 79
    1 21 60 79
    2 15 0 1
    0 0 2700 89
    0 28 48 1
    1 6 320 33
    1 6 40 1
    1 8 120 11
    0 55 0 1