#P13218. [GCJ 2015 #1B] Counter Culture

[GCJ 2015 #1B] Counter Culture

题目描述

In the Counting Poetry Slam, a performer takes the microphone, chooses a number NN, and counts aloud from 11 to NN. That is, she starts by saying 11, and then repeatedly says the number that is 11 greater than the previous number she said, stopping after she has said NN.

It's your turn to perform, but you find this process tedious, and you want to add a twist to speed it up: sometimes, instead of adding 11 to the previous number, you might reverse the digits of the number (removing any leading zeroes that this creates). For example, after saying "16", you could next say either "17" or "61"; after saying "2300", you could next say either "2301" or "32". You may reverse as many times as you want (or not at all) within a performance.

The first number you say must be 11; what is the fewest number of numbers you will need to say in order to reach the number NN? 11 and NN count toward this total. If you say the same number multiple times, each of those times counts separately.

输入格式

The first line of the input gives the number of test cases, TT. TT lines follow. Each has one integer NN, the number you must reach.

输出格式

For each test case, output one line containing "Case #xx: yy", where xx is the test case number (starting from 11) and yy is the minimum number of numbers you need to say.

3
1
19
23
Case #1: 1
Case #2: 19
Case #3: 15

提示

Sample Explanation

In Case #2, flipping does not help and the optimal strategy is to just count up to 1919.

In Case #3, the optimal strategy is to count up to 1212, flip to 2121, and then continue counting up to 2323. That is, the numbers you will say are 1,2,3,4,5,6,7,8,9,10,11,12,21,22,231, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 21, 22, 23.

Limits

  • 1T1001 \leq T \leq 100.

Small dataset

  • Time limit: 240 5 seconds.
  • 1N1061 \leq N \leq 10^6.

Large dataset

  • Time limit: 480 10 seconds.
  • 1N10141 \leq N \leq 10^{14}.