Counting Sheep (Small)
InterviewTime limit5sMemory limit512 MB
Track which digits 0 to 9 appear as Bleatrix counts multiples of N, and report the last multiple or INSOMNIA if digit 9 never completes.
- Level
Easy3 of 10
- Topics
- Simulation, Implementation, Math
- Solved
- No attempts yet
Problem
A sheep named Bleatrix has come up with a strategy to fall asleep faster.
First, she picks a number . Then she thinks of , , , and so on. Each time she thinks of a number, she writes down every digit that appears in it, but she does not write down a digit she has already written. Once all ten digits from 0 to 9 have been written down, she falls asleep.
Bleatrix starts from , and after she always thinks of . For example, if , the process goes like this:
- . She has written 1, 2, 6, 9.
- . She has written 1, 2, 3, 4, 6, 8, 9.
- . She has written every digit, and she falls asleep.
What is the last number Bleatrix thinks of before she falls asleep? If she will never fall asleep, print INSOMNIA.
Input
The first line contains the number of test cases . Each of the next lines contains one test case: the number that Bleatrix picked.
Output
For each test case, print one line Case #x: y, where is the test case number starting from 1 and is the last number Bleatrix thinks of before she falls asleep. If she will never fall asleep, print INSOMNIA in place of .
Hint
In the first case of the sample, , , and so on, so Bleatrix never writes down any digit other than 0. She will never fall asleep.
In the second case, she thinks of 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 in that order, and 0 is the last digit she writes down. The answer is 10.
In the third case, she thinks of 2, 4, 6, and so on. She cannot write down the digit 9 before she reaches 90. By the time she thinks of 90, she has already written every digit from 0 to 8, so the answer is 90.
In the fourth case, she thinks of 11, 22, 33, 44, 55, 66, 77, 88, 99, 110, so the answer is 110.