📜  选择X以使(A xor X)+(B xor X)最小

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

给定两个整数AB。我们的任务是选择一个整数X ,以使(A xor X)+(B xor X)最小。

例子:

一个简单的解决方案是通过取AB的xor与X≤min(A,B)的所有可能值来生成所有可能的和。要生成所有可能的总和,将需要O(N)时间,其中N = min(A,B)

一种有效的解决方案基于以下事实:数字X仅在该索引处包含置位,而AB都包含置位,使得在与X进行异或运算后,该位将不被置位。这仅需要O(Log N)时间。
其他情况:如果在一个特定的索引处,一个或两个数字都包含0 (未设置位),数字X包含1 (设置位),则在XAB中的X进行异或后将设置为0 ,则总和不能最小化。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the integer X such that
// (A xor X) + (B ^ X) is minimized
int findX(int A, int B)
{
    int j = 0, x = 0;
 
    // While either A or B is non-zero
    while (A || B) {
 
        // Position at which both A and B
        // have a set bit
        if ((A & 1) && (B & 1)) {
 
            // Inserting a set bit in x
            x += (1 << j);
        }
 
        // Right shifting both numbers to
        // traverse all the bits
        A >>= 1;
        B >>= 1;
        j += 1;
    }
    return x;
}
 
// Driver code
int main()
{
    int A = 2, B = 3;
    int X = findX(A, B);
 
    cout << "X = " << X << ", Sum = " << (A ^ X) + (B ^ X);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG {
 
    // Function to return the integer X such that
    // (A xor X) + (B ^ X) is minimized
    static int findX(int A, int B)
    {
        int j = 0, x = 0;
 
        // While either A or B is non-zero
        while (A != 0 || B != 0) {
 
            // Position at which both A and B
            // have a set bit
            if ((A % 2 == 1) && (B % 2 == 1)) {
 
                // Inserting a set bit in x
                x += (1 << j);
            }
 
            // Right shifting both numbers to
            // traverse all the bits
            A >>= 1;
            B >>= 1;
            j += 1;
        }
        return x;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int A = 2, B = 3;
        int X = findX(A, B);
 
        System.out.println(
            "X = " + X + ", Sum = " + ((A ^ X) + (B ^ X)));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python 3 implementation of the approach
 
# Function to return the integer X such that
# (A xor X) + (B ^ X) is minimized
 
 
def findX(A, B):
    j = 0
    x = 0
 
    # While either A or B is non-zero
    while (A or B):
 
        # Position at which both A and B
        # have a set bit
        if ((A & 1) and (B & 1)):
 
            # Inserting a set bit in x
            x += (1 << j)
 
        # Right shifting both numbers to
        # traverse all the bits
        A >>= 1
        B >>= 1
        j += 1
    return x
 
 
# Driver code
if __name__ == '__main__':
    A = 2
    B = 3
    X = findX(A, B)
 
    print("X =", X, ", Sum =", (A ^ X) + (B ^ X))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG {
 
    // Function to return the integer X such that
    // (A xor X) + (B ^ X) is minimized
    static int findX(int A, int B)
    {
        int j = 0, x = 0;
 
        // While either A or B is non-zero
        while (A != 0 || B != 0) {
 
            // Position at which both A and B
            // have a set bit
            if ((A % 2 == 1) && (B % 2 == 1)) {
 
                // Inserting a set bit in x
                x += (1 << j);
            }
 
            // Right shifting both numbers to
            // traverse all the bits
            A >>= 1;
            B >>= 1;
            j += 1;
        }
        return x;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int A = 2, B = 3;
        int X = findX(A, B);
 
        Console.WriteLine(
            "X = " + X + ", Sum = " + ((A ^ X) + (B ^ X)));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP
>= 1;
        $B >>= 1;
        $j += 1;
    }
    return $x;
}
 
// Driver code
    $A = 2;
    $B = 3;
    $X = findX($A, $B);
 
    echo "X = " , $X , ", Sum = ",
        ($A ^ $X) + ($B ^ $X);
 
// This code is contributed by ajit.
?>


Javascript


C++
// c++ implementation of above approach
#include 
using namespace std;
 
// finding X
int findX(int A, int B) {
  return A & B;
}
 
// finding Sum
int findSum(int A, int B) {
  return A ^ B;
}
 
// Driver code
int main()
{
    int A = 2, B = 3;
    cout << "X = " << findX(A, B)
         << ", Sum = " << findSum(A, B);
    return 0;
}
 
// This code is contribued by yashbeersingh42


Java
// Java implementation of above approach
import java.io.*;
 
class GFG
{
    // finding X
    public static int findX(int A, int B)
    {
      return A & B;
    }
 
    // finding Sum
    public static int findSum(int A, int B)
    {
        return A ^ B;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 2, B = 3;
        System.out.print("X = " + findX(A, B)
                         + ", Sum = " + findSum(A, B));
    }
}
// This code is contribued by yashbeersingh42


Python3
# Python3 implementation of above approach
 
# finding X
def findX(A, B):
    return A & B
 
# finding Sum
def findSum(A, B):
    return A ^ B
 
# Driver code
A, B = 2, 3
print("X =", findX(A, B) , ", Sum =" , findSum(A, B))
 
# This code is contributed by divyeshrabadiya07


C#
// C# implementation of above approach
using System;
 
class GFG{
     
// Finding X
public static int findX(int A, int B)
{
    return A & B;
}
 
// Finding Sum
public static int findSum(int A, int B)
{
    return A ^ B;
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 2, B = 3;
     
    Console.Write("X = " + findX(A, B) +
                  ", Sum = " + findSum(A, B));
}
}
 
// This code is contributed by Princi Singh


Javascript


输出
X = 2, Sum = 1

最有效的方法:

使用X仅包含设置的位A和B的想法, X = A&B 。在替换X时,上述等式变为(A ^(A&B))+(B ^(A&B)) ,进一步等于A ^ B。

Proof:
Given (A ^ X) + (B ^ X)
Taking X = (A & B), we have 
(A ^ (A & B)) + (B ^ (A & B))
   (using x ^ y = x'y + y'x )
= (A'(A & B) + A(A & B)') + (B'(A & B) + B(A & B)')
   (using (x & y)' = x' + y')
= (A'(A & B) + A(A' + B')) + (B'(A & B) + B(A' + B'))
  (A'(A & B) = A'A & A'B = 0, B'(A & B) 
   = B'A & B'B = 0)
= (A(A' + B')) + (B(A' + B'))                               
= (AA' + AB') + (BA' + BB')
  (using xx' = x'x = 0)                                   
= (AB') + (BA')                                             
= (A ^ B)

单击此处以了解有关布尔属性的更多信息。

下面是上述方法的实现:

C++

// c++ implementation of above approach
#include 
using namespace std;
 
// finding X
int findX(int A, int B) {
  return A & B;
}
 
// finding Sum
int findSum(int A, int B) {
  return A ^ B;
}
 
// Driver code
int main()
{
    int A = 2, B = 3;
    cout << "X = " << findX(A, B)
         << ", Sum = " << findSum(A, B);
    return 0;
}
 
// This code is contribued by yashbeersingh42

Java

// Java implementation of above approach
import java.io.*;
 
class GFG
{
    // finding X
    public static int findX(int A, int B)
    {
      return A & B;
    }
 
    // finding Sum
    public static int findSum(int A, int B)
    {
        return A ^ B;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int A = 2, B = 3;
        System.out.print("X = " + findX(A, B)
                         + ", Sum = " + findSum(A, B));
    }
}
// This code is contribued by yashbeersingh42

Python3

# Python3 implementation of above approach
 
# finding X
def findX(A, B):
    return A & B
 
# finding Sum
def findSum(A, B):
    return A ^ B
 
# Driver code
A, B = 2, 3
print("X =", findX(A, B) , ", Sum =" , findSum(A, B))
 
# This code is contributed by divyeshrabadiya07

C#

// C# implementation of above approach
using System;
 
class GFG{
     
// Finding X
public static int findX(int A, int B)
{
    return A & B;
}
 
// Finding Sum
public static int findSum(int A, int B)
{
    return A ^ B;
}
 
// Driver Code
public static void Main(String[] args)
{
    int A = 2, B = 3;
     
    Console.Write("X = " + findX(A, B) +
                  ", Sum = " + findSum(A, B));
}
}
 
// This code is contributed by Princi Singh

Java脚本


输出
X = 2, Sum = 1