给定六个正整数代表三元组(a,b,c)的所有可能对的按位XOR和按位与,任务是找到三元组。
例子:
Input: aXORb = 30, aANDb = 0, aXORc = 10, aANDc = 20, aXORb = 20, aANDb = 10
Output: a = 10, b = 20, c= 30
Explanation:
If a = 10, b = 20, c= 30
a ^ b = 30, a & b = 0
a ^ c = 10, a & c = 20
a ^ b = 20, a & b = 10
Therefore, the required output is (a, b, c) = (10, 20, 30).
Input: aXORb = 3, aANDb = 0, aXORc = 2, aANDc = 1, aXORb = 1, aANDb = 2
Output: a = 1, b = 2, c = 3
方法:这个想法是基于以下观察结果,使用它们的按位XOR和按位AND值找到每个可能的三元组对的总和:
a + b = a ^ b + 2 * (a & b)
请按照以下步骤解决问题:
- 使用上述公式求出每对可能的三元组的总和,即(a + b,b + c,c + a) 。
- a的值可以被计算为((A + B)+(A + C) – (B + C))/ 2。
- b的值可以计算为((a + b)– a) 。
- c的值可以计算为((b + c)– b) 。
- 最后,打印三元组(a,b,c)的值。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to find the triplet with given
// Bitwise XOR and Bitwise AND values of all
// possible pairs of the triplet
void findNumbers(int aXORb, int aANDb, int aXORc, int aANDc,
int bXORc, int bANDc)
{
// Stores values of
// a triplet
int a, b, c;
// Stores a + b
int aSUMb;
// Stores a + c
int aSUMc;
// Stores b + c
int bSUMc;
// Calculate aSUMb
aSUMb = aXORb + aANDb * 2;
// Calculate aSUMc
aSUMc = aXORc + aANDc * 2;
// Calculate bSUMc
bSUMc = bXORc + bANDc * 2;
// Calculate a
a = (aSUMb - bSUMc + aSUMc) / 2;
// Calculate b
b = aSUMb - a;
// Calculate c
c = aSUMc - a;
// Print a
cout << "a = " << a;
// Print b
cout << ", b = " << b;
// Print c
cout << ", c = " << c;
}
// Driver Code
int main()
{
int aXORb = 30, aANDb = 0, aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
}
Java
// Java program to implement
// the above approach
class GFG{
// Function to find the triplet with given
// Bitwise XOR and Bitwise AND values of all
// possible pairs of the triplet
static void findNumbers(int aXORb, int aANDb,
int aXORc, int aANDc,
int bXORc, int bANDc)
{
// Stores values of
// a triplet
int a, b, c;
// Stores a + b
int aSUMb;
// Stores a + c
int aSUMc;
// Stores b + c
int bSUMc;
// Calculate aSUMb
aSUMb = aXORb + aANDb * 2;
// Calculate aSUMc
aSUMc = aXORc + aANDc * 2;
// Calculate bSUMc
bSUMc = bXORc + bANDc * 2;
// Calculate a
a = (aSUMb - bSUMc + aSUMc) / 2;
// Calculate b
b = aSUMb - a;
// Calculate c
c = aSUMc - a;
// Print a
System.out.print("a = " + a);
// Print b
System.out.print(", b = " + b);
// Print c
System.out.print(", c = " + c);
}
// Driver Code
public static void main(String[] args)
{
int aXORb = 30, aANDb = 0,
aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc,
aANDc, bXORc, bANDc);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python program to implement
# the above approach
# Function to find the triplet with given
# Bitwise XOR and Bitwise AND values of all
# possible pairs of the triplet
def findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc):
# Stores values of
# a triplet
a, b, c = 0, 0, 0;
# Stores a + b
aSUMb = 0;
# Stores a + c
aSUMc = 0;
# Stores b + c
bSUMc = 0;
# Calculate aSUMb
aSUMb = aXORb + aANDb * 2;
# Calculate aSUMc
aSUMc = aXORc + aANDc * 2;
# Calculate bSUMc
bSUMc = bXORc + bANDc * 2;
# Calculate a
a = (aSUMb - bSUMc + aSUMc) // 2;
# Calculate b
b = aSUMb - a;
# Calculate c
c = aSUMc - a;
# Pra
print("a = " , a, end = "");
# Prb
print(", b = " , b, end = "");
# Prc
print(", c = " , c, end = "");
# Driver Code
if __name__ == '__main__':
aXORb = 30; aANDb = 0; aXORc = 20; aANDc = 10; bXORc = 10; bANDc = 20;
findNumbers(aXORb, aANDb, aXORc, aANDc, bXORc, bANDc);
# This code contributed by shikhasingrajput
C#
// C# code for above approach
using System;
public class GFG
{
// Function to find the triplet with given
// Bitwise XOR and Bitwise AND values of all
// possible pairs of the triplet
static void findNumbers(int aXORb, int aANDb,
int aXORc, int aANDc,
int bXORc, int bANDc)
{
// Stores values of
// a triplet
int a, b, c;
// Stores a + b
int aSUMb;
// Stores a + c
int aSUMc;
// Stores b + c
int bSUMc;
// Calculate aSUMb
aSUMb = aXORb + aANDb * 2;
// Calculate aSUMc
aSUMc = aXORc + aANDc * 2;
// Calculate bSUMc
bSUMc = bXORc + bANDc * 2;
// Calculate a
a = (aSUMb - bSUMc + aSUMc) / 2;
// Calculate b
b = aSUMb - a;
// Calculate c
c = aSUMc - a;
// Print a
System.Console.Write("a = " + a);
// Print b
System.Console.Write(", b = " + b);
// Print c
System.Console.Write(", c = " + c);
}
// Driver code
static public void Main ()
{
int aXORb = 30, aANDb = 0,
aXORc = 20, aANDc = 10,
bXORc = 10, bANDc = 20;
findNumbers(aXORb, aANDb, aXORc,
aANDc, bXORc, bANDc);
}
}
// This code is contributed by offbeat.
Javascript
输出:
a = 10, b = 20, c = 30
时间复杂度: O(1)
辅助空间: O(1)