TSOC '16 Contest 1 #5 - Max and Cards

View as PDF

Submit solution

Points: 7 (partial)
Time limit: 1.0s
Memory limit: 64M

Authors:
Problem type

Max is a magician whose job is to shuffle magic cards and confuse innocent bystanders. He uses a special deck with N cards, labelled from 1 to N. Suddenly, he throws his hands up in the air and the cards fall back down in sorted order (from least to greatest). He then quickly swipes his hands over the cards and forms all N! possible permutations from lexicographically least to greatest ordered from 0 to N!-1. For example, if N=3 then he would have made the permutations:

1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

Afterwards, Max asks you Q questions in the form of what was the Q_i^\text{th} permutation that Max made. Since your tiny eyes were too slow to see Max's amazing trick, you try using your old Pentium 4 computer to recreate Max's magic, but can you do it fast enough?

Input Specification

The first line will contain N (3 \le N \le 19).
The second line will contain Q (3 \le Q \le 100).
The next Q lines will contain Q_i (0 \le Q_i < N!).

Output Specification

For each question, output the Q_i^\text{th} permutation on its own line.

Scoring

At least 50% of the test cases will have N \le 10.

Sample Input

3
6
5
4
3
2
1
0

Sample Output

3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3

Comments

There are no comments at the moment.