给定两个整数X和Y中,任务是找到方式的总数量,以产生一对整数A和B的,使得按位XOR和按位与A和B之间分别是X和Y
例子:
Input: X = 2, Y = 5
Output: 2
Explanation:
The two possible pairs are (5, 7) and (7, 5).
Pair 1: (5, 7)
Bitwise AND = 5 & 7 = 2
Bitwise XOR = 5 ^ 7 = 5
Pair 2: (7, 5)
Bitwise AND = 7 & 5 = 2
Bitwise XOR = 7 ^ 5 = 5
Input: X = 7, Y = 5
Output: 0
天真的方法:解决问题的最简单方法是在X和Y中选择最大值,并设置所有位,然后检查从0到该最大数(例如M)的所有可能的对。如果对于任意一对A和B , A和B和A andB分别等于X和Y ,则增加计数。在检查所有可能的对之后,打印count的最终值。
时间复杂度: O(M 2 )
辅助空间: O(1)
高效方法:想法是在每个位置生成所有可能的位组合。 X和Y中的第i位有4种可能性,如下所示:
- 如果X i = 0且Y i = 1,则A i = 1且B i = 1。
- 如果X i = 1且Y i = 1,则不存在可能的位分配。
- 如果X i = 0且Y i = 0,则A i = 0且B i = 0。
- 如果X i = 1且Y i = 0,则A i = 0且B i = 1或A i = 1且B i = 0,其中A i , B i , X i和Y i分别代表第i个比特其中。
请按照以下步骤解决问题:
- 将计数器初始化为1 。
- 对于第i个比特,如果X I和Y i为等于1,然后打印0。
- 如果在第i位, X i为1 , Y i为0,则将计数器乘以2,因为有2个选项。
- 之后,将X和Y分别除以2 。
- 对第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)