10 students are standing in a row. From left to right, they are labeled as 1 to 10. When the teacher left for the bathroom, they start to switch positions. When the teacher come back on the minute, the queue becomes the lexicography order.
The teacher needs your help to answer 2 types of queries:
K L N
: On the
minute, what is the label of the
person from the left?
K P N
: On the
minute, what is the position of the person labeled
?
This file contains 1000 queries. What is the sum of all output?
Sample Input
1 2 3 4 5 6 7 8 |
|
Sample Output
1 2 3 4 5 6 7 8 |
|
For this example, the answer is 45.
This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try
refreshing the page, (b) enabling javascript if it is disabled on your browser and,
finally, (c)
loading the
non-javascript version of this page
. We're sorry about the hassle.
The hardest part is to get the k th lexicographic order.
Why and how does this work? The first person will remain at the first position for 9 ! permutations, the second will remain for 8 ! permutations and so on. Using this logic, we can determine the position one by one. After that you just have to find the label of the n -th person or the position of the person labeled n .