Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/kombinatorika-k-orin-almashtirish

k-th permutation

Medium 1000 ms 128 MB 56%
Share

All $n!$ permutations of the numbers $1, 2, \dots, n$ are written out in lexicographic (dictionary) order: the first is $1\ 2 \dots n$, the last is $n\ \dots 2\ 1$. Print the $k$-th permutation of the list.

Permutations in dictionary order; the k-th one is found block by block by its first elements

Input

One line with two positive integers: $n$ and $k$.

Output

$n$ space-separated numbers — the $k$-th permutation.

Note

$1 \le n \le 20$
$1 \le k \le n!$

Examples

Input #1
3 4
Output #1
2 3 1
Input #2
1 1
Output #2
1

Sign in to submit a solution

Signing up is free and takes a minute.