MMM '14 D - Kuuhaku Numbers

View as PDF

Submit solution

Points: 25 (partial)
Time limit: 1.4s
Memory limit: 256M

Author:
Problem type
Maniacal Midsummer Marathon 2014 by AL, TL, JJ

Shiro is enthralled by the latest FPS (first person shooter) game in the Eastern Federation. A lot is riding on this game, so she is planning to snipe the other player, Izuna, for a decisive victory. Shiro plans to pull this off from a kilometer away, and Izuna is in a shelter, so this requires very advanced mathematical calculations. Shiro needs a special sequence of numbers, the Kuuhaku numbers (a.k.a. KH-numbers) to accomplish her goal.

The KH-numbers have an interesting property: examining each digit of an N digit KH-number in base-10 from left to right (most significant to least significant), there will exist a length 6 subsequence of the digits that are nondecreasing in value. More formally: A number in the form aN×10N1+aN1×10N2++a1×100 has the sequence of digits {aN,aN1,,a1}. This number is a KH-number iff there exists a sequence {c1,c2,,ck} for k6, where 1ciN (for 1ik) and ci>ci+1 and aciaci+1 (for 1i<k).

Shiro needs all the KH-numbers between A and B (1AB<1018) to complete her calculations. In the time you need to compute all those numbers, she is sure that she can calculate the exact numbers in her head. Therefore, Shiro only needs you to determine the number of KH-numbers that exist between A and B to verify her calculations.

Input Specification

The 1st line of input will contain the integer A.
The 2nd line of input will contain the integer B.
You can assume for test cases worth 30% of points, 1AB<106.

Output Specification

A single integer, the number of KH-numbers between A and B, inclusive.

Sample Input 1

Copy
111111
111131

Sample Output 1

Copy
17

Explanation for Sample 1

The valid KH-numbers are 111111, 111112, 111113, 111114, 111115, 111116, 111117, 111118, 111119, 111122, 111123, 111124, 111125, 111126, 111127, 111128, and 111129.

Sample Input 2

Copy
2454449
2454455

Sample Output 2

Copy
2

Explanation for Sample 2

The valid KH-numbers are 2454449 and 2454455.

Sample Input 3

Copy
1111111
1111123

Sample Output 3

Copy
13

Explanation for Sample 3

All numbers between 1111111 and 1111123 are KH-numbers.


Comments

There are no comments at the moment.