[英]Calculate how many binary numbers with X number of 1 bits exist between two given binary numbers with the same property
This has been a challenge for me for some time. 一段时间以来,这一直是我的挑战。
Given two arrays that represent binary numbers, A and C with the same size, consisting of bits represented by the numbers 0 or 1, such that C > A, and both have the same X number of 1 bits, what is the most efficient way to calculate how many binaries B exist, such that A < B < C, and that every B also has X number of 1 bits?. 给定两个表示二进制数的数组,A和C具有相同的大小,由数字0或1表示的位组成,使得C> A,并且都具有相同的X位数1,这是最有效的方法计算存在多少个二进制B,使得A <B <C,并且每个B也具有X个1位数字?
Example: For A={0,1,0,0,1} C={1,0,1,0,0} X=2 示例:对于A = {0,1,0,0,1} C = {1,0,1,0,0} X = 2
All B's would be {0,1,0,1,0},{0,1,1,0,0},{1,0,0,0,1},{1,0,0,1,0}, which would give me the answer 4. There are 4 binaries with 2 '1' bits between 01001 and 10100. 所有B均为{0,1,0,1,0},{0,1,1,0,0},{1,0,0,0,1},{1,0,0,1,0 },它会给我答案4。在01001和10100之间有4个二进制文件,带有2个“ 1”位。
I have an algorithm to generate the next B given some A, but i feel that it wouldnt be efficient to keep generating the next B AND checking if i have hit the C binary yet. 我有一个算法可以给定一些A来生成下一个B,但是我觉得继续生成下一个B并检查我是否已经达到C二进制代码效率不高。
Is there any way to calculate the exact number of B's between A and C without generating the B's? 有什么方法可以在不生成B的情况下计算A和C之间B的确切数目?
Don't know if you've got an answer on https://math.stackexchange.com/ , but let me take a stab at it. 不知道您是否在https://math.stackexchange.com/上找到了答案,但是让我take一下。
In all the discussions below, we're only interested in numbers with X
1-bits. 在下面的所有讨论中,我们只对
X
1位的数字感兴趣。 I'm not gonna keep saying that, to keep it simpler. 为了简单起见,我不会再这么说了。
So, let's assume we can calculate the count of numbers below a given value A
: smaller(A)
. 因此,假设我们可以计算低于给定值
A
的数字计数: smaller(A)
。 To find the count of numbers between A
and C
, we can calculate that as smaller(C) - smaller(A) - 1
. 要找到
A
和C
之间的数字计数,我们可以将其计算为smaller(C) - smaller(A) - 1
。
Let's define a function that counts how many numbers with X bits exists in a Y-bit space, count(X, Y)
, eg count(1, 3) = 3
( 001
, 010
, 100
) and count(2, 3) = 3
( 011
, 101
, 110
). 让我们定义计数多少数字与X个比特在Y位的空间中存在的功能,
count(X, Y)
例如count(1, 3) = 3
( 001
, 010
, 100
)和count(2, 3) = 3
( 011
, 101
, 110
)。 This is standard combination math, ie number of combinations to pull X balls numbered 1 to Y from a bag. 这是标准组合数学,即从袋子中拉出编号为1到Y的X球的组合数量。
count(X, Y) = Y! / ((Y-X)! * X!)
where X!
X!
在哪里X!
is factorial(X)
. 是
factorial(X)
。
Now I'm going to show the next part, how to calculate smaller(A)
, using an example. 现在,我将通过一个示例展示下一部分,如何计算
smaller(A)
。 Let A = 010010100
. 设
A = 010010100
。
First, count the 0's on the right (2). 首先,计算右边的0(2)。 There are then
count(1, 2)
numbers below A
where the right-most 1-bit is moved to the right ( 010010010
, 010010001
). 有然后
count(1, 2)
下面的数字A
其中最右边的1比特被移动到右侧( 010010010
, 010010001
)。
Hint: Use count = Integer.numberOfTrailingZeros(A)
提示:使用
count = Integer.numberOfTrailingZeros(A)
Remove that 1-bit, leaving A = 010010000
. 删除该1位,剩下
A = 010010000
。
Hint: Use A = A ^ (1 << count)
提示:使用
A = A ^ (1 << count)
Repeat, ie count 0's (4), but this time we need count of 2-bits combinations, ie count(2, 4)
. 重复,即计数0(4),但是这次我们需要计数2位组合,即
count(2, 4)
。
That leaves A = 010000000
, which leads to count(3, 7)
. A = 010000000
,这导致count(3, 7)
A = 010000000
count(3, 7)
。
So, because the 1-bits were at bit 2, 4, and 7, we can calculate: 因此,由于1位分别位于第2、4和7位,我们可以计算出:
smaller(A) = count(1, 2) + count(2, 4) + count(3, 7)
Now, with a good efficient implementation of count(X, Y)
, it shouldn't be too bad to calculate count of numbers between A and C, even for high bit-counts. 现在,有了
count(X, Y)
高效实现,即使对于高位数,也可以计算A和C之间的数字计数。
Anyway, that's one way to do it, but the geniuses over on the math side may have better algorithms for this. 无论如何,这是做到这一点的一种方法,但是数学方面的天才可能对此有更好的算法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.