#K58892. K-th Permutation Sequence
K-th Permutation Sequence
K-th Permutation Sequence
Given two positive integers (n) and (k), the task is to find the (k)-th permutation sequence of the numbers from 1 to (n) when all permutations are arranged in lexicographical order.
For example, when (n=3), all permutations in lexicographical order are:
1. 123
2. 132
3. 213
4. 231
5. 312
6. 321
Thus, if (k=3), the answer is 213.
Note: The formula for factorial is given by (n! = n \times (n-1)!) with (0! = 1).
inputFormat
Input is read from standard input (stdin). The input consists of a single line containing two integers (n) and (k) separated by a space. (n) represents the number of digits (from 1 to (n)) and (k) represents the permutation index (1-indexed).
outputFormat
Output the (k)-th permutation sequence as a string to standard output (stdout).## sample
3 3
213