#P2022. Lexicographical Position Finder
Lexicographical Position Finder
Lexicographical Position Finder
Consider the set of positive integers from 1 to N. When these numbers are sorted according to their lexicographical order (i.e. by comparing their decimal representations as strings), the order may be different from the natural numerical order. For example, when N = 11 the ordered sequence is:
1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9
Define \(Q(N, K)\) as the position (1-indexed) of the integer K within the lexicographically sorted list of 1, 2, \(\ldots\), N. For example, \(Q(11,2)=4\) because 2 is the 4th element in the above ordering.
Given two integers K and M, your task is to find the smallest N (with N ≥ K) such that \(Q(N, K) = M\).
It is guaranteed that a solution exists.
inputFormat
The input consists of two integers K and M separated by spaces.
outputFormat
Output the smallest integer N such that when the integers from 1 to N are arranged in lexicographical order, the integer K appears at the M-th position.
sample
2 4
11