TITLE: (Fast way to) Get a combination given its position in (reverse-)lexicographic order
QUESTION [10 upvotes]: This question is the inverse of the Fast way to get a position of combination (without repetitions).
Given all $\left(\!\!\binom{n}{k}\!\!\right)$ combinations without repetitions (in either lexicographic or reverse-lexicographic order), what would be the most efficient way to translate/map/lookup a 
position of the combination to the combination ($k$-tuple) itself?
I need this to be fast for combinations of $\left(\!\!\binom{70}{7}\!\!\right)$ order of magnitude - very large, but not exceeding 2 billion (fits into int32 maximum value).
Below is an example of $\left(\!\!\binom{6}{3}\!\!\right)$, where the aim is to quickly translate value 9 to tuple (a, d, f), while in the real problem $5 <= k <= 8$.
$$\begin{array}
{cccccc|c}
a&b&c&d&e&f&^{combination}/_{sort\_order}&
\\\hline
x&x&x& & & &1\\
x&x& &x& & &2\\
x&x& & &x& &3\\
x&x& & & &x&4\\
x& &x&x& & &5\\
x& &x& &x& &6\\
x& &x& & &x&7\\
x& & &x&x& &8\\
x& & &x& &x&9\\
x& & & &x&x&10\\
.&.&.&.&.&.&.\\
 & & &x&x&x&20\\
\end{array}$$
I know that I could pre-calculate all the combinations and reverse the lookup dictionary. However, such dictionary would not be efficient in terms of memory usage. Therefore I am looking for either calculation-based approach, or a more efficient data structure to perform this mapping, and be able to process about 1 million such lookups during data processing job.
Note: the position can be 0-based.

REPLY [2 votes]: For the preliminaries I have to refer you to my answer to the position-finding problem.
In particular, I will use reverse-lexicographic ordering and zero-based indices
because it is simpler.
Transforming to one-based indices and positions with lexicographic ordering,
as in your example table, can be done
by replacing the input position with its distance to $\binom{n}{k}$
and by transforming the output tuple from $(i_0,\ldots,i_{k-1})$
to $(n−i_{k-1},\ldots,n−i_0)$.
To recap: A $k$-index is herein defined to be a $k$-tuple of strictly increasing nonnegative integers.
For a $k$-index $I$, its zero-based position (or compressed index) $\operatorname{ordx}(I)$ is defined as the number of $k$-indices that are reverse-lexicographically smaller than $I$.
Denoting $I = (i_0,\ldots,i_{k-1})$, we have worked out the explicit formula
$$\operatorname{ordx}(I) = \sum_{r=1}^k\binom{i_{r-1}}{r} \tag{1}$$
Using $(1)$ and
$$\operatorname{ordx}(I) < \operatorname{ordx}\bigl((0,\ldots,k-2,i_{k-1}+1)\bigr) = \binom{i_{k-1}+1}{k}$$
we can deduce
$$\binom{i_{k-1}}{k} \leq \operatorname{ordx}(I) < \binom{i_{k-1}+1}{k}
\tag{2}$$
Given the requirement that $k$-index elements be nonnegative
and strictly increasing, we also know that $i_{k-1}\geq k-1$.
Now $\binom{x}{k}$ is a degree-$k$ polynomial in $x$ with zeros
$0,\ldots,k-1$ and positive leading coefficient,
so $\binom{x}{k}$ is nonnegative, unbounded, and strictly monotonic
for $x\geq k-1$, beginning with $\binom{k-1}{k}=0$,
so there exists precisely one solution $i_{k-1}\geq k-1$ for $(2)$.
From $(1)$ and $(2)$ we can figure out an algorithm to recover $i_{k-1}$
and ultimately all of $I$ from $s = \operatorname{ordx}(I)$.
Note that it requires explicit knowledge of the tuple length $k$:
Function $\operatorname{xord}(k, s)$:

Input: tuple length $k$, zero-based position $s$.
Output: The $k$-index $I$ such that $\operatorname{ordx}(I) = s$.

If $k = 0$, return an empty tuple.
Find $i$ such that $i\geq k-1$ and $b := \binom{i}{k}
   \leq s < \binom{i+1}{k}$.
Set the $(k-1)$-index
$(i_0,\ldots,i_{k-2}) = \operatorname{xord}(k-1, s-b)$.
Return $(i_0,\ldots,i_{k-2},i)$.


The Python implementation below uses loops instead of function call recursion.
The search for $i_{k-1}$ with suitable $\binom{i}{k}$ proceeds upward;
once found, the remaining $i_r$ are found by downward search.
The binomial coefficients are computed on the fly, requiring less than about
$2i_{k-1}$ multiplications and divisions in total.
The search for $\binom{i}{1} = s$ is shortcut to $i = s$.
In the answer to the question about finding the position
$\operatorname{ordx}(I)$, I have also demonstrated a variant named
$\operatorname{ords}$ which allows repeated elements, that is,
combinations with replacement: Just replace every $i_r$ in the above discussion
with $i_r + r$, then the latter forms a strictly increasing sequence even when
the former is merely nondecreasing. Code for the corresponding inverse function
$\operatorname{sord}$ is given below; and for the sake of brevity,
I have implemented xord in terms of sord:
def xord(k, sk):
    """
    Inverse function of ``ordx``, given output tuple size
    and compressed index.
    """
    return [i + r for r,i in enumerate(sord(k, sk))]

Alternatively, you might implement xord like sord below,
but with all output assignments of the form idx[r] = j
changed to idx[r] = j + r, including the case r = 1.
def sord(k, sk):
    """
    Inverse function of ``ords``, given output tuple size
    and compressed index.
    """
    # Allocate output array. Content does not matter here, only length.
    idx = [0] * k
    if k == 0: return idx
    s = sk
    if k > 1:
        # Find j such that binomial(j+k-1,k) <= s < binomial(j+k,k)
        j = 0
        prev_b = 0
        b = 1
        while b <= s:
            # Maintain invariants: 
            # prev_b == binomial(j+k-1,k), b == binomial(j+k,k)
            prev_b = b
            j += 1
            b *= j + k
            b //= j
        b = prev_b
        # j is now the greatest index occurring in the tuple.
        # From now on, we will search backwards, decreasing j.
        for r in xrange(k-1, 1, -1):
            # b == binomial(j+r,r+1) <= s < binomial(j+r+1,r+1)
            idx[r] = j
            s -= b
            # Update to b = binomial(j+r-1,r)
            b *= r + 1
            b //= j + r
            # Find j such that binomial(j+r-1,r) <= s < binomial(j+r,r)
            while b > s:
                j -= 1
                b *= j
                b //= j + r
        # Simplified handling of r = 1
        # b == binomial(j+r,r+1) <= s < binomial(j+r+1,r+1)
        idx[1] = j
        s -= b
    # Simplified handling of r = 0
    # binomial(j+r,r+1) == s iff j == s
    idx[0] = s
    return idx

If you use fixed-width integer variables, take care that the variable b
has enough bits available for the intermediate multiplications.
Verifying that ordx inverts xord can be done with something like:
assert ordx([]) == 0
assert xord(0, 0) == []
for k in xrange(1, 9):
    for s in xrange(10000):
        assert ordx(xord(k, s)) == s