#P1643. Finding the N-th Palindromic Number Greater Than X
Finding the N-th Palindromic Number Greater Than X
Finding the N-th Palindromic Number Greater Than X
You are given two integers X and N. Your task is to find the N-th smallest palindromic number that is strictly greater than X.
A number is a palindrome if its decimal representation reads the same forwards and backwards. For example, 121, 33, and 101 are palindromic numbers. In other words, a number P is palindromic if it satisfies:
$$P = \overline{\,d_1 d_2 \ldots d_k} = \overline{\,d_k \ldots d_2 d_1} $$Example:
- For X = 1 and N = 1: The smallest palindrome greater than 1 is 2.
- For X = 17 and N = 2: The palindromes greater than 17 are 22, 33, 44, ...; hence the 2nd smallest is 33.
- For X = 98 and N = 2: The palindromes greater than 98 are 99, 101, 111, ...; therefore the 2nd smallest is 101.
Note: In order to handle very large values, it is recommended to construct palindromic numbers by considering the first half of the number and then mirroring it to create the full palindrome.
inputFormat
The input consists of a single line with two integers X and N separated by space.
Constraints:
- 1 ≤ X
- 1 ≤ N
- The values can be large so consider efficient construction of palindromic numbers.
outputFormat
Output the N-th smallest palindromic number that is strictly greater than X.
sample
1 1
2