📜  位大小最大为X并且按位或等于X的对的计数

📅  最后修改于: 2021-04-27 18:49:03             🧑  作者: Mango

给定数字X ,计算可能的对(a,b)的数量,以使a和b的按位或等于X并且a和b的位数都小于等于X的位数。

例子:

方法:要解决上述问题,请执行以下步骤:

  • 遍历给定数字X的每一位。
  • 如果该位为1,则根据按位或的真值表,我们知道数字a和b中的给定位可能有3种组合,即(0,1),(1,0),(1,1)有3种可能的方式。
  • 如果该位为0,则从按位或的真值表中,我们知道数字a和b中的给定位只有(0,0)的1种组合。
  • 因此,我们的答案将是3 ^(X中的接通位数)。

下面是上述方法的实现:

C++
// C++ implementation to Count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
  
#include 
using namespace std;
  
// Function to count the pairs
int count_pairs(int x)
{
    // Initializing answer with 1
    int ans = 1;
  
    // Iterating through bits of x
    while (x > 0) {
  
        // check if bit is 1
        if (x % 2 == 1)
  
            // multiplying ans by 3
            // if bit is 1
            ans = ans * 3;
  
        x = x / 2;
    }
    return ans;
}
  
// Driver code
int main()
{
    int X = 6;
  
    cout << count_pairs(X)
         << endl;
  
    return 0;
}


Java
// Java implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
class GFG{
  
// Function to count the pairs
static int count_pairs(int x)
{
      
    // Initializing answer with 1
    int ans = 1;
  
    // Iterating through bits of x
    while (x > 0)
    {
          
        // Check if bit is 1
        if (x % 2 == 1)
  
            // Multiplying ans by 3
            // if bit is 1
            ans = ans * 3;
  
        x = x / 2;
    }
    return ans;
}
  
// Driver code
public static void main(String[] args)
{
    int X = 6;
  
    System.out.print(count_pairs(X) + "\n");
}
}
  
// This code is contributed by amal kumar choubey


Python3
# Python3 implementation to count number of
# possible pairs of (a, b) such that
# their Bitwise OR gives the value X
  
# Function to count the pairs
def count_pairs(x):
  
    # Initializing answer with 1
    ans = 1;
  
    # Iterating through bits of x
    while (x > 0):
  
        # Check if bit is 1
        if (x % 2 == 1):
  
            # Multiplying ans by 3
            # if bit is 1
            ans = ans * 3;
  
        x = x // 2;
      
    return ans;
  
# Driver code
if __name__ == '__main__':
      
    X = 6;
  
    print(count_pairs(X));
  
# This code is contributed by amal kumar choubey


C#
// C# implementation to count number of
// possible pairs of (a, b) such that
// their Bitwise OR gives the value X
using System;
class GFG{
  
  // Function to count the pairs
  static int count_pairs(int x) 
  {
  
    // Initializing answer with 1
    int ans = 1;
  
    // Iterating through bits of x
    while (x > 0) 
    {
  
      // Check if bit is 1
      if (x % 2 == 1)
  
        // Multiplying ans by 3
        // if bit is 1
        ans = ans * 3;
  
      x = x / 2;
    }
    return ans;
  }
  
  // Driver code
  public static void Main(String[] args) 
  {
    int X = 6;
  
    Console.Write(count_pairs(X) + "\n");
  }
}
  
// This code is contributed by sapnasingh4991


输出:
9

时间复杂度: O(log(X))