二进制字符串排列

发布于 2024-12-10 10:26:50 字数 624 浏览 0 评论 0原文

我在 http://www.interviewstreet.com 上遇到问题。

Bob 收到了 Alice 发送的长度为 N 的二进制字符串。他知道由于传输错误,多达 K 位可能已被损坏(并因此被翻转)。然而,他也知道爱丽丝想要传输的字符串不是周期性的。如果一个字符串不能表示为连接一定次数的较小字符串,则该字符串不是周期性的。例如,“0001”、“0110”不是周期性的,而“00000”、“010101”是周期性字符串。 现在他想知道 Alice 可以传输多少个可能的字符串。

因此,首先我用二项式定理做了一些测试,通过使用它,我能够找到给定一个字符串和许多损坏的位可以表示一个字符串有多少种不同的方式。我的第二步是找到一种方法来查找周期字符串的数量。我发现使用具有素数长度的字符串可以轻松完成此操作。这是通过检查是否有足够的 0 或 1 来仅用 0 或 1 填充字符串来完成的。

1111111 或 0000000

现在我使用纯粹的强力算法,当涉及到任何类型的大字符串时,它不会削减它。有人可以向我指出任何类型的组合技术可以帮助解决这个问题吗?谢谢。

I came across a problem on http://www.interviewstreet.com .

Bob has received a binary string of length N transmitted by Alice. He knows that due to errors in transmission, up to K bits might have been corrupted (and hence flipped). However, he also knows that the string Alice had intended to transmit was not periodic. A string is not periodic if it cannot be represented as a smaller string concatenated some number of times. For example, "0001", "0110" are not periodic while "00000", "010101" are periodic strings.
Now he wonders how many possible strings could Alice have transmitted.

So first off I did some tests with the Binomial theorem and through the use of that I am able to find how many different ways a String can be represented given a String and a number of corrupted bits. My second step was to find a way with which to find the number of periodic Strings. I see that this can easily be done with Strings with a prime numbered length. This is done by checking if there are enough 0's or 1's to fill the String up with only 0's or 1's.

1111111 or 0000000

Right now I use a pure brute force algorithm which just wont cut it when it comes to any sort of large string. Is there any sort of Combinatorics techniques that somebody could point me to that would help solve this problem? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

水波映月 2024-12-17 10:26:50

利奥尔走在正确的道路上。

长度为 N 的字符串总数为 2^N。其中一些是周期性的。其他人则不然。我们将周期字符串的数量称为A(N),将非周期字符串的数量称为B(N)。那么

A(N) + B(N) = 2^N

如果我们将长度为 1 的字符串定义为非周期性的,那么

A(1) = 0
B(1) = 2

现在让我们假设 N > N。 1..那么长度为N的周期串集合包括周期短于N的周期串。然而,对于长度为 N 的非周期字符串集来说,情况并非如此。

长度为 N 的周期性字符串集合由长度为 n 的约数的非周期性字符串的重复组成,包括长度为 1 的字符串。换句话说:

A(N) = sum(B(k) where k divides N and k < N)

例如:

A(6) = B(1) + B(2) + B(3)
     = (2^1 - A(1)) + (2^2 - A(2)) + (2^3 - A(3))
     = 2 + (4 - B(1)) + (8 - B(1))
     = 2 + 2 + 6
     = 10

现在我们有一个关于长度N的周期和非周期字符串数量的递推方程。

不幸的是,这对我们回答实际问题没有多大帮助。

这个问题意味着 Bob 收到了一个特定的字符串,他想知道有多少个非周期字符串与该字符串相差最多 K 位。接收到的字符串可能有 C(N,K) 种可能的突变,而这些突变可能是传输的字符串。我们需要从中减去该集合中周期串的数量。我们该怎么做呢?

首先,我们可以观察到任何周期串都是非周期串的重复。因此,对于每个潜在周期 kN 的除数),我们查看长度 k 的子串。如果所有字符串与公共字符串的差异加起来不超过 K 位,则该公共字符串是周期字符串的基础,我们应该将计数减一。如果最小距离为d,并且K - d > N/k,那么我们可以翻转每个子字符串中的各个位并且仍然有匹配项,并且我们必须相应地减少计数。

Lior was on the right track.

The total number of strings of length N is 2^N. Some of these are periodic. Others are not. Let's call the number of periodic strings A(N), and the number of non-periodic strings B(N). Then

A(N) + B(N) = 2^N

If we define strings of length 1 to be non-periodic, then

A(1) = 0
B(1) = 2

Let's assume now that N > 1. Then the set of periodic strings of length N includes strings that are periodic with a period shorter than N. However, this is not the case for the set of non-periodic strings of length N.

The set of periodic strings of length N is made up of repetitions of non-periodic strings of lengths that are divisors of n, including those of length 1. In other words:

A(N) = sum(B(k) where k divides N and k < N)

For example:

A(6) = B(1) + B(2) + B(3)
     = (2^1 - A(1)) + (2^2 - A(2)) + (2^3 - A(3))
     = 2 + (4 - B(1)) + (8 - B(1))
     = 2 + 2 + 6
     = 10

So we now have a recurrence equation for the number of periodic and non-periodic strings of length N.

Unfortunately, this doesn't help us that much to answer the actual question.

The question implies that Bob has received a specific string, and he wants to know how many non-periodic strings differ by at most K bits from this string. There are C(N,K) possible mutations of the received string that could be the transmitted string. We need to subtract from this the number of periodic strings in this set. How can we go about this?

First, we can use the observation that any periodic string is a repetition of non-periodic strings. So, for each potential period k (divisor of N), we look at the substrings of length k. If all strings are different from a common string by no more than K bits combined, then this common string is the basis for a periodic string and we should decrease the count by one. If the minimum distance is d, and K - d > N/k, then we can flip individual bits in each substring and still have a match, and we have to decrease our count accordingly.

暮年 2024-12-17 10:26:50

计算长度为n的非周期字符串的数量:

  • 字符串总数:2ⁿ
  • 减去长度为n的周期字符串的数量

计算周期字符串的数量关于长度n

  • 找到n的所有约数,除了n本身。例如:如果n=6 - 除数为1,2,3。

    此处讨论了此方法)< /p>

  • 每个除数 m 可用于表示 2^m 个周期字符串。例如

  • m=1: {0,1} - 2^1 个周期字符串
  • m=2: {00,01,10,11} - 2^2 个周期字符串
  • m=3: {000,...111 } - 2^3 周期字符串

    因此,对于 n=6,有 2+4+8 个周期串

    正如 Jeffery Sax 和 ANeves 指出的,其中一些周期字符串是相同的(例如 0* = 00* = 000*),因此我们必须消除这些。

    一个简单的方法是将所有这些字符串添加到存储唯一元素的关联容器中(例如 在 C++ 中设置),并计算该容器中的元素数量。

    更好的优化是:对于 m=m1,找到 m1 的所有除数,并避免添加这些集合中已有字符串的周期性字符串。

下一步是计算任何这些周期字符串与接收到的字符串之间的汉明距离 。如果小于K-则计数。


编辑:大 N 和小 K 的更好解决方案

检查字符串是否周期性的算法:

这可以通过将字符串与其自身的移位版本进行比较来完成。如果一个字符串与其 p 位循环移位相同,那么它的循环周期为 p。

因此,每次循环移位字符串一位 - 我们可以在最多楼层(N/2)字符串比较中检测它是否是周期性的。

计算可能传输的字符串

如果没有非周期性传输要求,并且我们收到了 N 位消息 - 可能已传输的消息数为 C(N, 0) + C(N, 1) + C(N, 2) + ... + C(N, K)

对于 N=1000 和 K=3: C(1000,0)+C(1000,1)+C(1000,2)+C(1000,3)= 166,667,501

(这是原字符串中切换0/1/2/3位的组合数)。

根据这个数字,我们需要减少无法传输的周期字符串的数量。

例如:如果接收到的字符串是000000并且K=2,我们可以确定发送的字符串不在{000000,001001,010010,100100}中。这些都是周期性的,与接收到的字符串的汉明距离最大为 K。

C(6,0)+C(6,1)+C(6,2)=1+6+15=22
其中,有 4 个组合是周期性的。

算法:

我们将从接收到的字符串开始,并生成上述所有组合。对于每个组合,我们将检查它是否是周期性的。如果是这样 - 我们会将计数减 1。

To count the number of non-periodic strings on length n:

  • The total number of strings: 2ⁿ
  • Subtract the number of periodic strings of length n

To count the number of periodic strings on length n:

  • Find all divisors of n, except n itself. For example: if n=6 - the divisors are 1,2,3.

    (The method for this had been discussed here)

  • Each divisor m can be used to represent 2^m periodic strings. For example

  • m=1: {0,1} - 2^1 periodic strings
  • m=2: {00,01,10,11} - 2^2 periodic strings
  • m=3: {000,...111} - 2^3 periodic strings

    So for n=6, there are 2+4+8 periodic strings

    As Jeffery Sax and ANeves pointed out, some of these periodic strings are identical {for example 0* = 00* = 000*), so we have to eliminate these.

    A naive method would be to add all these strings to an associative container that stores unique elements (such as set in C++), and count the number of elements in the that container.

    A better optimization would be: for m=m1, find all divisors of m1, and avoid adding strings that are periodic of strings already in these sets.

The next step would be to calculate the Hamming distance between any of these periodic strings and the received string. If it is less than K- count it.


Edit: A better solution for large N and small K

Algorithm for checking if a string is periodic:

This can be accomplished by comparing the string with a shifted-version of itself. If a string is identical to it's p-bit circular-shift - then it has a cycle of p.

So circularly-shifting the string one bit at a time - we can detect if it is periodic in up to floor(N/2) string comparisons.

Counting possible transmitted strings

If there would be no non-periodic transmission requirement, and we received an N bits message - the number of possible messages that could have been transmitted is C(N, 0) + C(N, 1) + C(N, 2) + ... + C(N, K)

For N=1000 and K=3: C(1000,0)+C(1000,1)+C(1000,2)+C(1000,3)= 166,667,501

(This is the number of combinations of switching 0/1/2/3 bits in the original string).

From this number, we need to decrease the number of periodic strings - which couldn't have been transmitted.

For example: if the received string was 000000 and K=2, we can be sure that the transmitted string was not in {000000,001001,010010,100100}. These are all periodic, with hamming distance of up to K from the received string.

C(6,0)+C(6,1)+C(6,2)=1+6+15=22
Out of these, 4 combinations are periodic.

Algorithm:

We'll start with the received string, and generate all combinations stated above. For each combination we will check if it is periodic. If so - we'll decrease our count by 1.

红ご颜醉 2024-12-17 10:26:50

Lior 和 Jeffrey 的答案构成了解决该问题的基础,但本文仍有待解决的一个有趣问题是,如何有效地计算给定 [input string, N, K]。我的回答主要集中在这一点上。

正如 Lior 和 Jeffrey 所指出的,在检查周期性字符串时,我们只需要关心长度等于 n 的约数的子字符串。让我们看一个例子,看看实现这一目标的效率如何。

周期为 m 的周期串的数量

假设输入字符串为

0110 0011 0101 0001

,让我们尝试找到周期为 m=4 的周期串的数量

第一位

如果我们比较每个子串的第一位,我们会发现它们全部都是是0。如果我们假设所有子串中的所有后续位都相同,则在执行 0 位翻转或 4 位翻转时,可以使输入字符串成为周期性的(周期为 4)。

0110 0011 0101 0001
^    ^    ^    ^
Number of 0s = 4
Number of 1s = 0
Number of bitflips to make all 0s to 1s = 4
Number of bitflips to make all 1s to 0s = 0


Number of periodic strings with period=4 for:
k = 0  =>  1
k = 4  =>  1

现在我们知道存在 2 个周期性字符串,一个用于 k=0,另一个用于 k=4(假设所有子串中的后续位都相同)。

第二位

现在让我们进入第二位。

0110 0011 0101 0001
 ^    ^    ^    ^
Number of 0s = 2
Number of 1s = 2
Number of bitflips to make all 0s to 1s = 2
Number of bitflips to make all 1s to 0s = 2

但是等等,上面的陈述是正确的,前提是子串中当前位之前的所有位也有助于使字符串具有周期性。我们知道,只有 k=0k=4 各自使字符串周期到第 1 位。

因此,当考虑到第 2 位之前的所有位时,我们可以在以下 4 种情况下获得周期字符串:

When previousK = 0:
    Flip the 2 `0`s to `1`s => new k = 2
    Flip the 2 `1`s to `0`s => new k = 2
When previousK = 4:
    Flip the 2 `0`s to `1`s => new k = 6
    Flip the 2 `1`s to `0`s => new k = 6

Number of periodic strings with period=4 for:
k = 2  =>  2
k = 6  =>  2

第三位

转到第三位,我们将看到:

0110 0011 0101 0001
  ^    ^    ^    ^
Number of 0s = 2
Number of 1s = 2
Number of bitflips to make all 0s to 1s = 2
Number of bitflips to make all 1s to 0s = 2

We can get a periodic string in the following 4 cases:
When previousK = 2:
    Flip the 2 `0`s to `1`s => new k = 4
    Flip the 2 `1`s to `0`s => new k = 4
When previousK = 6:
    Flip the 2 `0`s to `1`s => new k = 8
    Flip the 2 `1`s to `0`s => new k = 8

Number of periodic strings with period=4 for:
k = 4  =>  4
k = 8  =>  4

第四位

对于第四位也是最后一位:

0110 0011 0101 0001
   ^    ^    ^    ^
Number of 0s = 1
Number of 1s = 3

We can get a periodic string in the following 4 cases:
When previousK = 4:
    Flip the 1 `0`s to `1`s => new k = 5
    Flip the 3 `1`s to `0`s => new k = 7
When previousK = 8:
    Flip the 1 `0`s to `1`s => new k = 9
    Flip the 3 `1`s to `0`s => new k = 11

Number of periodic strings with period=4 for:
k = 5  =>  4
k = 7  =>  4
k = 9  =>  4
k = 11 =>  4

我们现在完成了加上子串的最后一位以及最后一步中各种 k 值的周期串的总数,得出 16。

递归关系和伪代码

让我们表示任何变化的 k 的周期串的当前计数从使用 R[k] 将 1 转换为 K。对于每次迭代,我们需要查找前一次迭代的 R[] 值。

我们在每次迭代中最终要做的事情是:

for offset = 0 to periodLen - 1
    flip R[] and previousR[]

    for currentK = 1 to K
        R[currentK] = 0

    numZeroes = 0
    for (pos = offset; pos < n; pos += periodLen)
        if (str[pos] == '0')
            ++numZeros

    numOnes = (n / m) - numZeroes;

    for currentK = 1 to K
        if m == 0
            R[currentK + numZeroes] = 1
            R[currentK + numOnes] = 1
        else if (previousR[currentK] > 0)
            R[currrentK + numZeroes] += previousR[currentK]
            R[currentK + numOnes] += previousR[currentK]

    totalPeriodicCount = 0
    for currentK = 1 to K
        totalPeriodicCount += R[currentK]

如果我们通过从最低到最高迭代所有周期来执行上述过程,我们将获得所有周期字符串的计数。将选择的周期将是小于 NN 的约数。从最低到最高的顺序查看它们会有优势,请阅读下一节以了解详细信息。

考虑可被较小周期整除的周期

仔细观察,您会发现我们还会多次计算某些周期字符串。

例如。以下周期字符串:

0001 0001 0001 0001

最终将被计为 m = 4 和 m = 8 的一部分

C[m] 表示在长度为 的周期内获得的周期字符串的总数m 使用上面的伪代码获得。令C[m']表示使用长度为m的周期获得的周期串的实际计数,但不计算使用periods 可以形成的周期串。 m

更具体地说,如果当前周期 m 具有除数 tuv,它们是小于 m,那么我们将计算周期 tuv 周期字符串的数量同样,即

C[m] = C[t'] + C[u'] + C[v'] + C[m']

当计算所有 m 值的周期串总数时,我们需要注意排除 C[t]C[u]C[v] 并仅考虑 C[m']

因为当我们计算 C[m] 时,我们已经计算了 C[t']C[u'] 的值> 和 C[v'],我们只需查找它们并用 C[m] 减去它们即可得到 C[m']。我将把这个简单的部分作为读者的练习。

C[m'] = C[m] - C[t'] - C[u'] - C[v']

Lior and Jeffrey's answers form the foundation for solving the problem, but one interesting problem that remains to be solved in this post is, how can you efficiently compute the number of strings which are periodic for a given [input string, N, K]. My answer will primarily focus on that.

As pointed out by Lior and Jeffrey, we need only to care about the substrings of length equal to the divisors of n when checking for strings which are periodic. Let's look at an example to see how efficient we can achieve this.

Number of periodic strings with period m

Let the input string be

0110 0011 0101 0001

and let us try to find the number of periodic strings with period m=4

First Bit

If we compare the first bit of each of the substrings, we'll see that all of them are 0s. If we assume all the subsequent bits are the same, amongst all the substrings, then the input string can be made periodic (with period 4), when performing either 0 bitflips or 4 bitflips.

0110 0011 0101 0001
^    ^    ^    ^
Number of 0s = 4
Number of 1s = 0
Number of bitflips to make all 0s to 1s = 4
Number of bitflips to make all 1s to 0s = 0


Number of periodic strings with period=4 for:
k = 0  =>  1
k = 4  =>  1

So now we know there exists 2 strings which are periodic, one for k=0 and other for k=4 (assuming that the subsequent bits are the same in all the substrings).

Second Bit

Now let's progress to the second bit.

0110 0011 0101 0001
 ^    ^    ^    ^
Number of 0s = 2
Number of 1s = 2
Number of bitflips to make all 0s to 1s = 2
Number of bitflips to make all 1s to 0s = 2

But wait, the above statement is true IFF all the bits prior to the current bit in the substring also contribute in making the string periodic. We know that only k=0 and k=4, each make the string periodic up to 1st bit.

So when accounting for all bits upto the 2nd bit, we can get a periodic string in the following 4 cases:

When previousK = 0:
    Flip the 2 `0`s to `1`s => new k = 2
    Flip the 2 `1`s to `0`s => new k = 2
When previousK = 4:
    Flip the 2 `0`s to `1`s => new k = 6
    Flip the 2 `1`s to `0`s => new k = 6

Number of periodic strings with period=4 for:
k = 2  =>  2
k = 6  =>  2

Third Bit

Moving on to third bit, we'll see this:

0110 0011 0101 0001
  ^    ^    ^    ^
Number of 0s = 2
Number of 1s = 2
Number of bitflips to make all 0s to 1s = 2
Number of bitflips to make all 1s to 0s = 2

We can get a periodic string in the following 4 cases:
When previousK = 2:
    Flip the 2 `0`s to `1`s => new k = 4
    Flip the 2 `1`s to `0`s => new k = 4
When previousK = 6:
    Flip the 2 `0`s to `1`s => new k = 8
    Flip the 2 `1`s to `0`s => new k = 8

Number of periodic strings with period=4 for:
k = 4  =>  4
k = 8  =>  4

Fourth Bit

For our fourth and final bit:

0110 0011 0101 0001
   ^    ^    ^    ^
Number of 0s = 1
Number of 1s = 3

We can get a periodic string in the following 4 cases:
When previousK = 4:
    Flip the 1 `0`s to `1`s => new k = 5
    Flip the 3 `1`s to `0`s => new k = 7
When previousK = 8:
    Flip the 1 `0`s to `1`s => new k = 9
    Flip the 3 `1`s to `0`s => new k = 11

Number of periodic strings with period=4 for:
k = 5  =>  4
k = 7  =>  4
k = 9  =>  4
k = 11 =>  4

We are now done with the last bit of the substring and the total of the periodic strings for various k values in the last step gives 16.

Recursive relation and Pseudocode

Let us denote the current count of periodic strings for any k which varies from 1 to K using R[k]. For each iteration, we need to look up the previous iteration's R[] value.

What we essentially end up doing in each iteration is:

for offset = 0 to periodLen - 1
    flip R[] and previousR[]

    for currentK = 1 to K
        R[currentK] = 0

    numZeroes = 0
    for (pos = offset; pos < n; pos += periodLen)
        if (str[pos] == '0')
            ++numZeros

    numOnes = (n / m) - numZeroes;

    for currentK = 1 to K
        if m == 0
            R[currentK + numZeroes] = 1
            R[currentK + numOnes] = 1
        else if (previousR[currentK] > 0)
            R[currrentK + numZeroes] += previousR[currentK]
            R[currentK + numOnes] += previousR[currentK]

    totalPeriodicCount = 0
    for currentK = 1 to K
        totalPeriodicCount += R[currentK]

If we perform the above process by iterating over all periods from the lowest to the highest we'll get the count of all the periodic strings. The periods which will be chosen will be the divisors of N which are less than N. Going over them from lowest to highest will have an advantage, read the next section for the details.

Accounting for periods which are divisible by smaller periods

On close observation you'll notice that we're also counting certain periodic strings more than once.

eg. the following periodic string:

0001 0001 0001 0001

will end up being counted as part of both m = 4, and m = 8

Let C[m] denote the total count of periodic strings obtained for a period of length m obtained using the above pseudocode. Let C[m'] denote the actual count of periodic strings obtained using period of length m but not counting the periodic strings which can be formed using periods < m

More specifically, if the current period m has divisors t, u and v which are less than m, then we'll be counting the number of periodic strings of periods t, u and v as well, i.e.

C[m] = C[t'] + C[u'] + C[v'] + C[m']

When counting the total number of periodic strings for all values of m, we need to take care of excluding C[t], C[u], and C[v] and only account for C[m'].

Since when we're computing C[m], we already would have computed the values for C[t'], C[u'] and C[v'], we just need to look them up and subtract them from C[m] to obtain C[m']. I'll leave this simple portion as an exercise for the reader.

C[m'] = C[m] - C[t'] - C[u'] - C[v']
橘虞初梦 2024-12-17 10:26:50
checkperiodic(char [] string, int i, int length)
{
for(l=i; l <=length-i ; l=l+i)
if(strncmp(string, &string[l],i) != 0)
    return 0;
return 1;
}

N=strlen(string);
n= sqrt(N);
for(i=2;i <= n; i++)
{
if (N%i == 0)
    if(checkperiodic(string,i,N) || checkperiodic(string,N/i,N))
        break;
}

复杂度 N^1/2 * N * (除数之和)

checkperiodic(char [] string, int i, int length)
{
for(l=i; l <=length-i ; l=l+i)
if(strncmp(string, &string[l],i) != 0)
    return 0;
return 1;
}

N=strlen(string);
n= sqrt(N);
for(i=2;i <= n; i++)
{
if (N%i == 0)
    if(checkperiodic(string,i,N) || checkperiodic(string,N/i,N))
        break;
}

complexity N^1/2 * N * (sum of divisors)

魂归处 2024-12-17 10:26:50

扩展 Lior Kogan 的答案 二进制字符串排列

设 F(n) 为非周期数可以形成给定长度n的字符串。

为了找到 n 的非周期字符串 F(n) 的数量,我们需要从 n 的所有排列中消除所有 F(m),其中 m 是 n 的约数。

| n |     m | F(m)                     | answer |
| 1 |       | 2^1                      |      2 |
| 2 |     1 | 2^2-F(1)                 |      2 |
| 3 |     1 | 2^3-F(1)                 |      6 |
| 4 |   1,2 | 2^4-F(2)-F(1)            |     12 |
| 5 |     1 | 2^5 - F(1)               |     30 |
| 6 | 1,2,3 | 2^6 - F(1) - F(2) - F(3) |     54 |

简单的 python 实现

nperiodics = [0, 2] # for n = 1(0,1) and n = 2(01, 10)

def divisors(n):
    for d in xrange(1, n / 2 + 1):
        if n %  d == 0:
            yield d

def periodic(n, counts):
    return 2**n - sum(counts[d] for d in divisors(n))


for i in xrange(2, 10):
    nperiodics.append(periodic(i, nperiodics))

print nperiodics

输出:[0, 2, 2, 6, 12, 30, 54, 126, 240, 504]

Extending the Lior Kogan's answer Binary String permutations

Let F(n) be the number of aperiodic strings that can be formed for a given length n.

To find number of aperiodic string for n, F(n), we need to eliminate all the F(m)'s where m is a divisor of n from all the permutations of n.

| n |     m | F(m)                     | answer |
| 1 |       | 2^1                      |      2 |
| 2 |     1 | 2^2-F(1)                 |      2 |
| 3 |     1 | 2^3-F(1)                 |      6 |
| 4 |   1,2 | 2^4-F(2)-F(1)            |     12 |
| 5 |     1 | 2^5 - F(1)               |     30 |
| 6 | 1,2,3 | 2^6 - F(1) - F(2) - F(3) |     54 |

naive python implementation

nperiodics = [0, 2] # for n = 1(0,1) and n = 2(01, 10)

def divisors(n):
    for d in xrange(1, n / 2 + 1):
        if n %  d == 0:
            yield d

def periodic(n, counts):
    return 2**n - sum(counts[d] for d in divisors(n))


for i in xrange(2, 10):
    nperiodics.append(periodic(i, nperiodics))

print nperiodics

output : [0, 2, 2, 6, 12, 30, 54, 126, 240, 504]

凡间太子 2024-12-17 10:26:50

我用下面的算法解决了这个答案,这是非常基本的。希望我解决的问题是您所问问题的答案。

如果字符串中有 8 个二进制字符,并且我想获得它的所有可能的排列,那么以下算法将正确地给出这些值。在其中,我特意跳过了排列“00000000”,因为它对我来说没有价值:)。

下面的代码是用 Ruby 编写的:

size = 8
max_binary_value = (2 ** size) - 1  # In this case, 255

permutations = 1.upto(max_binary_value).map do |num|
  # This will zero-padd the string, ie: "%08d" % "10" # => "00000010",
  #   while to_s(2) will convert it to Base 2 in String form:
  "%0#{size}d" % num.to_s(2)
end

I solved this answer with the following algorithm, which is extremely basic. Hopefully the problem I solved is the answer to what you are asking about.

If I have 8 binary characters in a string, and I want to get all possible permutations for it, then the following algorithm will correctly give you those values. In it, I specifically skip the permutation "00000000" as it has no value to me :).

The below code is in Ruby:

size = 8
max_binary_value = (2 ** size) - 1  # In this case, 255

permutations = 1.upto(max_binary_value).map do |num|
  # This will zero-padd the string, ie: "%08d" % "10" # => "00000010",
  #   while to_s(2) will convert it to Base 2 in String form:
  "%0#{size}d" % num.to_s(2)
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文