#P13185. [GCJ 2016 Qualification] Counting Sheep

[GCJ 2016 Qualification] Counting Sheep

题目描述

Bleatrix Trotter the sheep has devised a strategy that helps her fall asleep faster. First, she picks a number N\mathrm{N}. Then she starts naming $\mathrm{N}, 2 \times \mathrm{N}, 3 \times \mathrm{N}$, and so on. Whenever she names a number, she thinks about all of the digits in that number. She keeps track of which digits (0,1,2,3,4,5,6,7,8(0,1,2,3,4,5,6,7,8, and 9)) she has seen at least once so far as part of any number she has named. Once she has seen each of the ten digits at least once, she will fall asleep.

Bleatrix must start with N\mathrm{N} and must always name (i+1)×N(i+1) \times \mathrm{N} directly after i×Ni \times \mathrm{N}. For example, suppose that Bleatrix picks N=1692\mathrm{N}=1692. She would count as follows:

  • N=1692\mathrm{N}=1692. Now she has seen the digits 1,2,61,2,6, and 99.
  • 2N=33842 \mathrm{N}=3384. Now she has seen the digits 1,2,3,4,6,81,2,3,4,6,8, and 99.
  • 3N=50763 \mathrm{N}=5076. Now she has seen all ten digits, and falls asleep.

What is the last number that she will name before falling asleep? If she will count forever, print INSOMNIA instead.

输入格式

The first line of the input gives the number of test cases, T\mathrm{T}. T\mathrm{T} test cases follow. Each consists of one line with a single integer N\mathrm{N}, the number Bleatrix has chosen.

输出格式

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 1) and yy is the last number that Bleatrix will name before falling asleep, according to the rules described in the statement.

5
0
1
2
11
1692
Case #1: INSOMNIA
Case #2: 10
Case #3: 90
Case #4: 110
Case #5: 5076

提示

Sample Explanation

In Case #1, since 2×0=0,3×0=02 \times 0=0,3 \times 0=0, and so on, Bleatrix will never see any digit other than 00, and so she will count forever and never fall asleep. Poor sheep!

In Case #2, Bleatrix will name 1,2,3,4,5,6,7,8,9,101,2,3,4,5,6,7,8,9,10. The 00 will be the last digit needed, and so she will fall asleep after 1010.

In Case #3, Bleatrix will name 2,4,62,4,6 \ldots and so on. She will not see the digit 9 in any number until 9090, at which point she will fall asleep. By that point, she will have already seen the digits 0,1,2,3,4,50,1,2,3,4,5, 6,76,7, and 88, which will have appeared for the first time in the numbers 10,10,2,30,4,50,6,7010,10,2,30,4,50,6,70, and 88, respectively.

In Case #4, Bleatrix will name 11,22,33,44,55,66,77,88,99,11011,22,33,44,55,66,77,88,99,110 and then fall asleep.

Case #5 is the one described in the problem statement. Note that it would only show up in the Large dataset, and not in the Small dataset.

Limits

  • 1T1001 \leqslant \mathrm{T} \leqslant 100.

Small dataset (7 Pts, Test Set 1 - Visible)

  • 0N2000 \leqslant \mathrm{N} \leqslant 200.

Large dataset (8 Pts, Test Set 2 - Hidden)

  • 0N1060 \leqslant \mathrm{N} \leqslant 10^{6}.