📌  相关文章
📜  计算生成具有分别等于X和Y的按位XOR和按位AND的对的方法

📅  最后修改于: 2021-05-25 09:41:36             🧑  作者: Mango

给定两个整数XY中,任务是找到方式的总数量,以产生一对整数AB,使得按位XOR和按位与AB之间分别是XY

例子:

天真的方法:解决问题的最简单方法是在XY中选择最大值,并设置所有位,然后检查从0到该最大数(例如M)的所有可能的对。如果对于任意一对ABA和BA andB分别等于XY ,则增加计数。在检查所有可能的对之后,打印count的最终值。
时间复杂度: O(M 2 )
辅助空间: O(1)

高效方法:想法是在每个位置生成所有可能的位组合。 XY中的i位有4种可能性,如下所示:

  1. 如果X i = 0且Y i = 1,则A i = 1且B i = 1。
  2. 如果X i = 1且Y i = 1,则不存在可能的位分配。
  3. 如果X i = 0且Y i = 0,则A i = 0且B i = 0。
  4. 如果X i = 1且Y i = 0,则A i = 0且B i = 1或A i = 1且B i = 0,其中A iB iX iY i分别代表i比特其中。

请按照以下步骤解决问题:

  1. 将计数器初始化为1
  2. 对于i比特,如果X IY i为等于1,然后打印0。
  3. 如果在i位X i1Y i0,则将计数器乘以2,因为有2个选项。
  4. 之后,将XY分别除以2
  5. i比特重复上述步骤,直到两个比特都变为0并打印计数器的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to return the count of
// possible pairs of A and B whose
// Bitwise XOR is X and Y respectively
int countOfPairs(int x, int y)
{
    // Stores the count of pairs
    int counter = 1;
 
    // Iterate till any bit are set
    while (x || y) {
 
        // Extract i-th bit
        // of X and Y
        int bit1 = x % 2;
        int bit2 = y % 2;
 
        // Divide X and Y by 2
        x >>= 1;
        y >>= 1;
 
        // If Xi = 1 and Yi = 2,
        // multiply counter by 2
        if (bit1 == 1 and bit2 == 0) {
 
            // Increase required count
            counter *= 2;
            continue;
        }
 
        // If Xi =1 and Yi = 1
        if (bit1 & bit2) {
 
            // No answer exists
            counter = 0;
            break;
        }
    }
 
    // Return the final count
    return counter;
}
 
// Driver Code
int main()
{
    // Given X and Y
    int X = 2, Y = 5;
 
    // Function Call
    cout << countOfPairs(X, Y);
 
    return 0;
}


Java
// Java program for
// the above approach
import java.util.*;
class GFG{
 
// Function to return the count of
// possible pairs of A and B whose
// Bitwise XOR is X and Y respectively
static int countOfPairs(int x, int y)
{
  // Stores the count of pairs
  int counter = 1;
 
  // Iterate till any bit are set
  while (x > 0 || y > 0)
  {
    // Extract i-th bit
    // of X and Y
    int bit1 = x % 2;
    int bit2 = y % 2;
 
    // Divide X and Y by 2
    x >>= 1;
    y >>= 1;
 
    // If Xi = 1 and Yi = 2,
    // multiply counter by 2
    if (bit1 == 1 && bit2 == 0)
    {
      // Increase required count
      counter *= 2;
      continue;
    }
 
    // If Xi =1 and Yi = 1
    if ((bit1 & bit2) > 0)
    {
      // No answer exists
      counter = 0;
      break;
    }
  }
 
  // Return the final count
  return counter;
}
 
// Driver Code
public static void main(String[] args)
{
  // Given X and Y
  int X = 2, Y = 5;
 
  // Function Call
  System.out.print(countOfPairs(X, Y));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the above approach
 
# Function to return the count of
# possible pairs of A and B whose
# Bitwise XOR is X and Y respectively
def countOfPairs(x, y):
 
    # Stores the count of pairs
    counter = 1
 
    # Iterate till any bit are set
    while(x or y):
 
        # Extract i-th bit
        # of X and Y
        bit1 = x % 2
        bit2 = y % 2
 
        # Divide X and Y by 2
        x >>= 1
        y >>= 1
 
        # If Xi = 1 and Yi = 2,
        # multiply counter by 2
        if (bit1 == 1 and bit2 == 0):
             
            # Increase required count
            counter *= 2
            continue
 
        # If Xi =1 and Yi = 1
        if(bit1 & bit2):
 
            # No answer exists
            counter = 0
            break
 
    # Return the final count
    return counter
 
# Driver Code
 
# Given X and Y
X = 2
Y = 5
 
# Function call
print(countOfPairs(X, Y))
 
# This code is contributed by Shivam Singh


C#
// C# program for
// the above approach
using System;
class GFG{
 
// Function to return the count of
// possible pairs of A and B whose
// Bitwise XOR is X and Y respectively
static int countOfPairs(int x, int y)
{
  // Stores the count of pairs
  int counter = 1;
 
  // Iterate till any bit are set
  while (x > 0 || y > 0)
  {
    // Extract i-th bit
    // of X and Y
    int bit1 = x % 2;
    int bit2 = y % 2;
 
    // Divide X and Y by 2
    x >>= 1;
    y >>= 1;
 
    // If Xi = 1 and Yi = 2,
    // multiply counter by 2
    if (bit1 == 1 && bit2 == 0)
    {
      // Increase required count
      counter *= 2;
      continue;
    }
 
    // If Xi =1 and Yi = 1
    if ((bit1 & bit2) > 0)
    {
      // No answer exists
      counter = 0;
      break;
    }
  }
 
  // Return the readonly count
  return counter;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given X and Y
  int X = 2, Y = 5;
 
  // Function Call
  Console.Write(countOfPairs(X, Y));
}
}
  
// This code is contributed by Rajput-Ji


Javascript


输出:
2

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