Nth pemutation of Lexicographic String Can someone please explain the logic behind the mathematical equation, that for finding the Nth Lexicographic rank of a string the Leading Entry is $a_q$ if $k=q\cdot (n!)+r.$ 
The link to the problem is Finding the n-th lexicographic permutation of a string

The introductory post actually describes the solution pretty well. But in another way, the idea is as follows:
1) Suppose there are $n+1$ entries in the string. To get the value for the first element, calculate the largest number $q$ of complete cycles through all of the permutations of remaining entries in such way that $q(n!) \leq N$. Then the $q^{th}$ element of the string is the first element in your answer, and so it follows that the leading entry is $a_q$ where $N = q(n!) + r$
2) For the second element, let $k$ be equal to $N - q(n!) = r$, and play the same game as (1) with a string having only $n$ entries where you wish to find the $k^{th}$ permutation. It should be obvious that replacing $N$ with $k$ in (1) you get the desired result.
3) once $n$ is 0 you're done.
