Task from an old DPA
Medium
Execution time limit is 1 second
Runtime memory usage limit is 64 megabytes
Rows (strings of digits) are constructed according to the following rule:
The first row consists of a single character, the digit "1". Each subsequent row is formed by first writing the row number (without leading zeros), followed by appending the previous row twice.
Here are the first 4 rows created according to this rule:
1
211
3211211
432112113211211
For instance, the 11-th row will begin as follows:
11. 1110987...
Given N and K, determine which digit appears in the N-th row at the K-th position, or if the row's length is less than K.
Input
Two natural numbers N and K are provided (1 ≤ N ≤ 100000, 1 ≤ K ≤ 10^15).
Output
Identify and output the digit located at the K-th position in the N-th row. If the N-th row is shorter than K, output -1 (minus one).
Examples
Input #1
Answer #1
Submissions 228
Acceptance rate 9%