Determine whether a symbol is part of the ith combination nCr

前端 未结 3 1920
感情败类
感情败类 2021-01-21 03:15

UPDATE: Combinatorics and unranking was eventually what I needed. The links below helped alot:

http://msdn.microsoft.com/en-us/library/aa289166(v=vs.71).aspx

ht

3条回答
  •  天命终不由人
    2021-01-21 03:45

    I believe your problem is that of unranking combinations or subsets.

    I will give you an implementation in Mathematica, from the package Combinatorica, but the Google link above is probably a better place to start, unless you are familiar with the semantics.

    UnrankKSubset::usage = "UnrankKSubset[m, k, l] gives the mth k-subset of set l, listed in lexicographic order."
    
    UnrankKSubset[m_Integer, 1, s_List] := {s[[m + 1]]}
    UnrankKSubset[0, k_Integer, s_List] := Take[s, k]
    UnrankKSubset[m_Integer, k_Integer, s_List] := 
           Block[{i = 1, n = Length[s], x1, u, $RecursionLimit = Infinity}, 
                 u = Binomial[n, k]; 
                 While[Binomial[i, k] < u - m, i++]; 
                 x1 = n - (i - 1); 
                 Prepend[UnrankKSubset[m - u + Binomial[i, k], k-1, Drop[s, x1]], s[[x1]]]
           ]
    

    Usage is like:

    UnrankKSubset[5, 3, {0, 1, 2, 3, 4}]
    
       {0, 3, 4}

    Yielding the 6th (indexing from 0) length-3 combination of set {0, 1, 2, 3, 4}.

提交回复
热议问题