Coding of Permutations
Time limit1sMemory limit128 MB
The task gives a permutation of 1 to n and asks for its 1-based rank among all permutations in lexicographic order.
- Level
Medium4 of 10
- Topics
- Combinatorics, Math
- Solved
- No attempts yet
Problem
A permutation uses each integer from to exactly once. List every permutation of length in lexicographic order and let be the position of in that list. The first one, , has number .
Write a program that computes without generating all permutations of length . Here is at most .
For , the permutations in lexicographic order and their numbers are shown below.

If then , and if then .
Input
The input has several lines. Each line holds one query in the form and contains no spaces. The last line holds only , and the input ends there. At least one query is given.
Output
Print for every query on a single line, in the order the queries are given. Separate two values with one comma, and put no space before or after the comma.