📜  最多N个代表二进制数的整数的计数

📅  最后修改于: 2021-06-25 15:24:46             🧑  作者: Mango

给定整数N ,任务是对从1到N (包括两个端点)的每个数字i进行计数,以使i是某个整数的二进制表示,其中N可以是[1,10 9 ]范围内的任何值。

例子:

天真的方法:由于N中的最大位数可以为10,所以存储10位的每个二进制组合,然后使用Binary search或upper_bound检查N的给定范围内的最大整数。
时间复杂度: O(MAX + log(MAX))其中MAX = 1024(2 10 )

高效的方法:我们可以观察到,对于任何N值,此类可能表示的最大数量为2个N – 1的数字。因此,我们需要遵循以下步骤:

  • 从右到左提取N的数字并将当前数字的位置存储在变量ctr中
  • 如果当前数字超过1,则意味着可以使用ctr数字获得最大可能的表示形式。因此,将答案设置为等于2 ctr – 1
  • 否则,如果当前数字为1,则将2 ctr – 1添加到到目前为止获得的答案。
  • 遍历所有数字后获得的最终值给出了答案。

下面是上述方法的实现:

C++
// C++ Program to count the
// number of integers upto N
// which are of the form of
// binary representations
 
#include 
using namespace std;
 
// Function to return the count
int countBinaries(int N)
{
 
    int ctr = 1;
    int ans = 0;
    while (N > 0) {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1) {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += pow(2, ctr - 1);
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1) {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = pow(2, ctr) - 1;
        }
 
        ctr++;
        N /= 10;
    }
 
    return ans;
}
// Driver Code
int main()
{
 
    int N = 20;
    cout << countBinaries(N);
 
    return 0;
}


Java
// Java program to count the number
// of integers upto N which are of
// the form of binary representations
import java.util.*;
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
         
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += Math.pow(2, ctr - 1);
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = (int) (Math.pow(2, ctr) - 1);
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 20;
    System.out.print(countBinaries(N));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to count the
# number of integers upto N
# which are of the form of
# binary representations
from math import *
 
# Function to return the count
def countBinaries(N):
     
    ctr = 1
    ans = 0
     
    while (N > 0):
         
        # If the current last
        # digit is 1
        if (N % 10 == 1):
             
            # Add 2^(ctr - 1) possible
            # integers to the answer
            ans += pow(2, ctr - 1)
 
        # If the current digit exceeds 1
        elif (N % 10 > 1):
             
            # Set answer as 2^ctr - 1
            # as all possible binary
            # integers with ctr number
            # of digits can be obtained
            ans = pow(2, ctr) - 1
 
        ctr += 1
        N //= 10
 
    return ans
 
# Driver Code
if __name__ == '__main__':
     
    N = 20
     
    print(int(countBinaries(N)))
 
# This code is contributed by Bhupendra_Singh


C#
// C# program to count the number
// of integers upto N which are of
// the form of binary representations
using System;
 
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
         
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
             
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += (int)Math.Pow(2, ctr - 1);
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = (int)(Math.Pow(2, ctr) - 1);
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 20;
    Console.Write(countBinaries(N));
}
}
 
// This code is contributed by amal kumar choubey


Javascript


C++
// C++ Program to count the
// number of integers upto N
// which are of the form of
// binary representations
 
#include 
using namespace std;
 
// Function to return the count
int countBinaries(int N)
{
    // PreCompute and store
    // the powers of 2
    vector powersOfTwo(11);
 
    powersOfTwo[0] = 1;
    for (int i = 1; i < 11; i++) {
        powersOfTwo[i]
= powersOfTwo[i - 1]
* 2;
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0) {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1) {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo[ctr - 1];
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1) {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo[ctr] - 1;
        }
 
        ctr++;
        N /= 10;
    }
 
    return ans;
}
// Driver Code
int main()
{
 
    int N = 20;
    cout << countBinaries(N);
 
    return 0;
}


Java
// Java program to count the number of
// integers upto N which are of the
// form of binary representations
import java.util.*;
 
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
     
    // PreCompute and store
    // the powers of 2
    Vector powersOfTwo = new Vector(11);
    powersOfTwo.add(1);
     
    for(int i = 1; i < 11; i++)
    {
       powersOfTwo.add(powersOfTwo.get(i - 1) * 2);
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo.get(ctr - 1);
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo.get(ctr) - 1;
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 20;
    System.out.print(countBinaries(N));
}
}
 
// This code is contributed by amal kumar choubey


Python3
# Python3 program to count the
# number of integers upto N
# which are of the form of
# binary representations
 
# Function to return the count
def countBinaries(N):
 
    # PreCompute and store
    # the powers of 2
    powersOfTwo = [0] * 11
 
    powersOfTwo[0] = 1
     
    for i in range(1, 11):
        powersOfTwo[i] = powersOfTwo[i - 1] * 2
 
    ctr = 1
    ans = 0
     
    while (N > 0):
 
        # If the current last
        # digit is 1
        if (N % 10 == 1):
 
            # Add 2^(ctr - 1) possible
            # integers to the answer
            ans += powersOfTwo[ctr - 1]
 
        # If the current digit exceeds 1
        elif (N % 10 > 1):
 
            # Set answer as 2^ctr - 1
            # as all possible binary
            # integers with ctr number
            # of digits can be obtained
            ans = powersOfTwo[ctr] - 1
 
        ctr += 1
        N = N // 10
 
    return ans
 
# Driver code
N = 20
 
print(countBinaries(N))
 
# This code is contributed by divyeshrabadiya07


C#
// C# program to count the number of
// integers upto N which are of the
// form of binary representations
using System;
using System.Collections.Generic;
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
     
    // PreCompute and store
    // the powers of 2
    List powersOfTwo = new List();
    powersOfTwo.Add(1);
     
    for(int i = 1; i < 11; i++)
    {
        powersOfTwo.Add(powersOfTwo[i - 1] * 2);
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo[ctr - 1];
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo[ctr] - 1;
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
static public void Main ()
{
    int N = 20;
    Console.Write(countBinaries(N));
}
}
 
// This code is contributed by ShubhamCoder


Javascript


输出:
3

时间复杂度: O(M 2 ) ,其中M是N中的位数
辅助空间: O(1)

优化:借助于前缀乘积数组,可以通过预先计算2到M(N的M的位数)的幂来优化上述方法。

以下是优化解决方案的实现:

C++

// C++ Program to count the
// number of integers upto N
// which are of the form of
// binary representations
 
#include 
using namespace std;
 
// Function to return the count
int countBinaries(int N)
{
    // PreCompute and store
    // the powers of 2
    vector powersOfTwo(11);
 
    powersOfTwo[0] = 1;
    for (int i = 1; i < 11; i++) {
        powersOfTwo[i]
= powersOfTwo[i - 1]
* 2;
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0) {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1) {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo[ctr - 1];
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1) {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo[ctr] - 1;
        }
 
        ctr++;
        N /= 10;
    }
 
    return ans;
}
// Driver Code
int main()
{
 
    int N = 20;
    cout << countBinaries(N);
 
    return 0;
}

Java

// Java program to count the number of
// integers upto N which are of the
// form of binary representations
import java.util.*;
 
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
     
    // PreCompute and store
    // the powers of 2
    Vector powersOfTwo = new Vector(11);
    powersOfTwo.add(1);
     
    for(int i = 1; i < 11; i++)
    {
       powersOfTwo.add(powersOfTwo.get(i - 1) * 2);
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo.get(ctr - 1);
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo.get(ctr) - 1;
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 20;
    System.out.print(countBinaries(N));
}
}
 
// This code is contributed by amal kumar choubey

Python3

# Python3 program to count the
# number of integers upto N
# which are of the form of
# binary representations
 
# Function to return the count
def countBinaries(N):
 
    # PreCompute and store
    # the powers of 2
    powersOfTwo = [0] * 11
 
    powersOfTwo[0] = 1
     
    for i in range(1, 11):
        powersOfTwo[i] = powersOfTwo[i - 1] * 2
 
    ctr = 1
    ans = 0
     
    while (N > 0):
 
        # If the current last
        # digit is 1
        if (N % 10 == 1):
 
            # Add 2^(ctr - 1) possible
            # integers to the answer
            ans += powersOfTwo[ctr - 1]
 
        # If the current digit exceeds 1
        elif (N % 10 > 1):
 
            # Set answer as 2^ctr - 1
            # as all possible binary
            # integers with ctr number
            # of digits can be obtained
            ans = powersOfTwo[ctr] - 1
 
        ctr += 1
        N = N // 10
 
    return ans
 
# Driver code
N = 20
 
print(countBinaries(N))
 
# This code is contributed by divyeshrabadiya07

C#

// C# program to count the number of
// integers upto N which are of the
// form of binary representations
using System;
using System.Collections.Generic;
class GFG{
 
// Function to return the count
static int countBinaries(int N)
{
     
    // PreCompute and store
    // the powers of 2
    List powersOfTwo = new List();
    powersOfTwo.Add(1);
     
    for(int i = 1; i < 11; i++)
    {
        powersOfTwo.Add(powersOfTwo[i - 1] * 2);
    }
 
    int ctr = 1;
    int ans = 0;
    while (N > 0)
    {
 
        // If the current last
        // digit is 1
        if (N % 10 == 1)
        {
 
            // Add 2^(ctr - 1) possible
            // integers to the answer
            ans += powersOfTwo[ctr - 1];
        }
 
        // If the current digit exceeds 1
        else if (N % 10 > 1)
        {
 
            // Set answer as 2^ctr - 1
            // as all possible binary
            // integers with ctr number
            // of digits can be obtained
            ans = powersOfTwo[ctr] - 1;
        }
        ctr++;
        N /= 10;
    }
    return ans;
}
 
// Driver Code
static public void Main ()
{
    int N = 20;
    Console.Write(countBinaries(N));
}
}
 
// This code is contributed by ShubhamCoder

Java脚本


输出:
3

时间复杂度: O(M)
辅助空间: O(M)