Problem 731
Permutation Sequence
Hard · 30 pts · Math, Recursion, Combinatorics
Problem
The permutations of the digits 1 to N, written as strings and sorted in lexicographic order, are numbered from 1 to N!. Print the K-th one. Listing all of them is too slow for N = 9 combined with many queries, so build the answer digit by digit: with m digits left, each choice of the next digit covers (m - 1)! permutations, so the index (K - 1) / (m - 1)! picks the digit, and the remainder continues with the next position.
Input
A single line with N and K.
Output
Print the K-th permutation as a string of N digits.
Constraints
1 <= N <= 9; 1 <= K <= N!
Example input
3 3
Example output
213
Explanation
For N = 3 the permutations in order are 123, 132, 213, 231, 312 and 321. The third one is 213, so for K = 3 the answer is 213.