Date Bugs
Time limit1sMemory limit128 MB
Given up to 20 wrap-around year displays, find the smallest real year below 10000 consistent with every computer's shown value.
- Level
Medium4 of 10
- Topics
- Brute force, Math, Implementation, Number theory
- Solved
- No attempts yet
Problem
Many computers store the year using only a few digits or a fixed-size counter, so after a certain point the displayed year wraps around. A computer that shows only two digits, for example, jumps from 1999 back to 1900. Some systems instead store the number of seconds elapsed since a fixed reference time in a 32-bit integer; after about seconds (roughly 136 years) the value wraps back to that reference time.
Each computer has a bug year . While the real year is below the computer shows it correctly, but the moment the real year reaches the display wraps to (instead of ) and keeps counting up, wrapping again every time it would reach . Thus computer can only ever display a year in the range , and when the real year is its display is
Several computers each report the year they currently display together with their bug parameters and . Assuming every computer refers to the same real year, find the earliest real year consistent with all of them. Because a computer cannot display a year before and cannot predate its own construction, the real year is always at least the maximum of the .
Input
The input consists of several test cases. Each test case begins with a line containing an integer (), the number of computers. Each of the next lines contains three integers , , (): is the year the computer displays, is the first year at which the bug occurs (the first year the computer can no longer display), and is the year it wraps to instead.
The input ends with a test case where , which must not be processed.
Output
For the -th test case, first print Case #k:. If a real year consistent with every computer exists, print The actual year is z., where is the smallest such year that is at least the maximum of the . If no such year below 10000 exists, print Unknown bugs detected. instead. Print a blank line between consecutive test cases.