📜  鲍姆甜蜜序列

📅  最后修改于: 2021-10-19 08:10:07             🧑  作者: Mango

Baum Sweet Sequence 是一个由 0 和 1 组成的无限二进制序列。如果数 n 在其二进制表示中没有奇数个连续零,则序列的第 n 项为 1,否则第 n 项为 0。

The first few terms of the sequence are:
b1 = 1 (binary of 1 is 1)
b2 = 0 (binary of 2 is 10)
b3 = 1 (binary of 3 is 11)
b4 = 1 (binary of 4 is 100)
b5 = 0 (binary of 5 is 101)
b6 = 0 (binary of 6 is 110)

给定一个自然数n 。任务是找到 Baum Sweet 序列的第 n 项,即检查它是否包含任何奇数长度的连续零块。

Input: n = 8
Output: 0
Explanations:
Binary representation of 8 is 1000. It 
contains odd length block of consecutive 0s. 
Therefore B8 is 0.

Input: n = 5
Output: 1

Input: n = 7
Output: 0

这个想法是通过 n 的二进制表示运行一个循环并计算所有存在的连续零块的长度。如果至少有一个奇数长度为零的块,则给定输入 n 的第 n 项为 0,否则为 1。

// CPP code to find the nth term of the
// Baum Sweet Sequence
#include 
using namespace std;
  
int nthBaumSweetSeq(int n)
{
    // bitset stores bitwise representation
    bitset<32> bs(n);
  
    // len stores the number of bits in the 
    // binary of n. builtin_clz() function gives 
    // number of zeroes present before the 
    // leading 1 in binary of n
    int len = 32 - __builtin_clz(n);
  
    int baum = 1; // nth term of baum sequence
    for (int i = 0; i < len;) {
        int j = i + 1;
  
        // enter into a zero block
        if (bs[i] == 0) {
            int cnt = 1;
  
            // loop to run through each zero block
            // in binary representation of n
            for (j = i + 1; j < len; j++) {
  
                // counts consecutive zeroes 
                if (bs[j] == 0)                   
                    cnt++;
                else
                    break;
            }
  
            // check if the number of consecutive
            // zeroes is odd
            if (cnt % 2 == 1)
                baum = 0;
        }
        i = j;
    }
  
    return baum;
}
  
// Driver Code
int main()
{
    int n = 8;
    cout << nthBaumSweetSeq(n);
    return 0;
}
输出:
0