[英]Bijection between (n choose k) and bitstrings of length n with k bits set
While I know how to generate all ( n
choose k
) bitstrings of size n
with exactly k
bits set to one, I'm struggling finding a bijection, that gets as input a number i
between 1
and ( n
choose k
) and outputs the i
-th vector of that kind in an arbitrary ordering. 虽然我知道如何生成大小为
n
所有( n
选择k
)位串,其中k
位设置为1,但我正在努力寻找一个双射,它在1
和( n
选择k
)之间输入数字i
并输出i
在任意顺序中的那种矢量。
Obviously, one could simply enumerate all of that vectors in a list and then output the i
-th entry of the list, but unfortunately that approach has to high memory requirements for my setting. 显然,可以简单地枚举列表中的所有向量,然后输出列表的第
i
个条目,但不幸的是,这种方法对我的设置有很高的内存要求。
Edit: also it should be an efficient computation, computing the list of all vectors for each call to the bijection is also not an option. 编辑:它也应该是一个有效的计算,计算每次调用双向的所有向量的列表也不是一个选项。
The straightforward way: 直截了当的方式:
If i < (n-1 choose k) , then the leftmost bit is 0 and i determines the remainder of the bits recursively. 如果i <(n-1选择k) ,则最左边的比特为0,并且i递归地确定比特的剩余部分。 Otherwise, the leftmost bit is 1 , and i - (n-1 choose k) determines the remainder of the bits recursively.
否则,最左边的比特是1 ,并且i - (n-1选择k)递归地确定比特的剩余部分。 In the second case there are at most (n-1 choose k-1) possible values of i - (n-1 choose k) .
在第二种情况下,最多(n-1选择k-1) i的可能值- (n-1选择k) 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.