Kuuhaku Numbers

View as PDF

Submit solution

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

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 a_N \times 10^{N-1} + a_{N-1} \times 10^{N-2} + \ldots + a_1 \times 10^0 has the sequence of digits \{a_{N}, a_{N-1}, \ldots, a_1\} This number is a KH-number iff there exists a sequence \{c_1, c_2, \ldots, c_k\} for k \ge 6, where 1 \le c_i \le N (for 1 \le i \le k) and c_i > c_{i + 1} and a_{c_{i}} \le
a_{c_{i + 1}} (for 1 \le i < k).

Shiro needs all the KH-numbers between A and B (1 \le A \le B <
10^{18}) 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 1^{st} line of input will contain the integer A.
The 2^{nd} line of input will contain the integer B.
You can assume for test cases worth 30\% of points, 1 \le A \le B < 10^{6}.

Output Specification

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

Sample Input 1


Sample Output 1


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


Sample Output 2


Explanation for Sample 2

The valid KH-numbers are 2454449 and 2454455.

Sample Input 3


Sample Output 3


Explanation for Sample 3

All numbers between 1111111 and 1111123 are KH-numbers.


There are no comments at the moment.