📜  计算从 1 到 N 的所有数字中的总设置位 |设置 3

📅  最后修改于: 2021-09-22 09:58:31             🧑  作者: Mango

给定一个正整数N ,任务是计算从1N的所有数字的二进制表示中设置位的总数。

例子:

处理方法:此问题的解决方案已在本文的Set 1 和Set 2 中发布。在这里,讨论了基于动态规划的方法。

  • 基本情况: 0 中的设置位数为 0。
  • 对于任何数字 n: n 和 n>>1 除了最右边的位外,具有相同的设置位。

示例:n = 11 ( 101 1), n >> 1 = 5 ( 101 )… 11 和 5 中的相同位用粗体标记。所以假设我们已经知道设置位数为 5,我们只需要注意 11 的最右边的位是 1。 setBit(11) = setBit(5) + 1 = 2 + 1 = 3

最右边的位是奇数为 1,偶数为 0。

循环关系: setBit(n) = setBit(n>>1) + (n & 1) and setBit(0) = 0

我们可以使用自底向上的动态规划方法来解决这个问题。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // To store the count of set
    // bits in every integer
    vector setBits(n + 1);
 
    // 0 has no set bit
    setBits[0] = 0;
 
    for (int i = 1; i <= n; i++) {
 
        setBits[i] = setBits[i >> 1] + (i & 1);
    }
 
    // Sum all the set bits
    for (int i = 0; i <= n; i++) {
        cnt = cnt + setBits[i];
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << countSetBits(n);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
class GFG
{
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
static int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // To store the count of set
    // bits in every integer
    int []setBits = new int[n + 1];
 
    // 0 has no set bit
    setBits[0] = 0;
 
    // For the rest of the elements
    for (int i = 1; i <= n; i++) {
 
        setBits[i] = setBits[i >> 1] + (i & 1);
    }
 
    // Sum all the set bits
    for (int i = 0; i <= n; i++)
    {
        cnt = cnt + setBits[i];
    }
    return cnt;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 6;
 
    System.out.println(countSetBits(n));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 implementation of the approach
 
# Function to return the count of
# set bits in all the integers
# from the range [1, n]
def countSetBits(n):
 
    # To store the required count
    # of the set bits
    cnt = 0
 
    # To store the count of set
    # bits in every integer
    setBits = [0 for x in range(n + 1)]
 
    # 0 has no set bit
    setBits[0] = 0
 
    # For the rest of the elements
    for i in range(1, n + 1):
        setBits[i] = setBits[i // 2] + (i & 1)
 
    # Sum all the set bits
    for i in range(0, n + 1):
        cnt = cnt + setBits[i]
     
    return cnt
 
# Driver code
n = 6
print(countSetBits(n))
 
# This code is contributed by Sanjit Prasad


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function to return the count of
    // set bits in all the integers
    // from the range [1, n]
    static int countSetBits(int n)
    {
     
        // To store the required count
        // of the set bits
        int cnt = 0;
     
        // To store the count of set
        // bits in every integer
        int []setBits = new int[n + 1];
     
        // 0 has no set bit
        setBits[0] = 0;
     
        // 1 has a single set bit
        setBits[1] = 1;
     
        // For the rest of the elements
        for (int i = 2; i <= n; i++)
        {
     
            // If current element i is even then
            // it has set bits equal to the count
            // of the set bits in i / 2
            if (i % 2 == 0)
            {
                setBits[i] = setBits[i / 2];
            }
     
            // Else it has set bits equal to one
            // more than the previous element
            else
            {
                setBits[i] = setBits[i - 1] + 1;
            }
        }
     
        // Sum all the set bits
        for (int i = 0; i <= n; i++)
        {
            cnt = cnt + setBits[i];
        }
        return cnt;
    }
     
    // Driver code
    static public void Main ()
    {
        int n = 6;
     
        Console.WriteLine(countSetBits(n));
    }
}
 
// This code is contributed by AnkitRai01


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // Calculate set bits in each number using
    // __builtin_popcount() and  Sum all the set bits
    for (int i = 1; i <= n; i++) {
        cnt = cnt + __builtin_popcount(i);
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << countSetBits(n);
 
    return 0;
}
 
// This article is contributed by Abhishek


输出:
9

另一个简单易懂的解决方案:

一个简单易于实现和理解的解决方案是不使用位操作。解决方案是使用 __builtin_popcount() 直接计算设置位。解决方案在代码中使用注释进行了解释。

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// set bits in all the integers
// from the range [1, n]
int countSetBits(int n)
{
 
    // To store the required count
    // of the set bits
    int cnt = 0;
 
    // Calculate set bits in each number using
    // __builtin_popcount() and  Sum all the set bits
    for (int i = 1; i <= n; i++) {
        cnt = cnt + __builtin_popcount(i);
    }
 
    return cnt;
}
 
// Driver code
int main()
{
    int n = 6;
 
    cout << countSetBits(n);
 
    return 0;
}
 
// This article is contributed by Abhishek
输出
9

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程