Produce the nth lexicographic permutation of a string consisting of letters H and V
Let P(n,m) be the number of permutations of n Hs and m Vs.  For m&gt;0 and n&gt;0, P(n,m) = (n + m)!/(n! * m!).

Let's say you want the kth permutation of n Hs and m Vs:

If k &lt;= P(n-1,m), then the answer is an H followed by the kth permutation of n-1 Hs and m Vs.

Otherwise the answer is a V followed by the (k-P(n-1,m))th permutation of n Hs and m-1 Vs.