📌  相关文章
📜  检查是否存在来自两个范围的一对整数,使得它们的按位异或超出两个范围

📅  最后修改于: 2022-05-13 01:56:08.630000             🧑  作者: Mango

检查是否存在来自两个范围的一对整数,使得它们的按位异或超出两个范围

给定两个整数AB ,任务是检查在[1, A][1, B]范围内是否分别存在两个整数PQ ,使得PQ的按位异或大于AB 。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

朴素方法:解决给定问题的最简单方法是通过遍历从1X1Y的所有整数来生成所有可能的 ( P, Q)对,并检查是否存在一对使得它们的位异或大于XY的按位异或,然后打印“是” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
void findWinner(int X, int Y)
{
    // Stores the Bitwise XOR of X & Y
    int playerA = (X ^ Y);
 
    bool flag = false;
 
    // Traverse all possible pairs
    for (int i = 1; i <= X; i++) {
 
        for (int j = 1; j <= Y; j++) {
 
            int val = (i ^ j);
 
            // If a pair exists
            if (val > playerA) {
                flag = true;
                break;
            }
        }
 
        if (flag) {
            break;
        }
    }
 
    // If a pair is found
    if (flag) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
}
 
// Driver Code
int main()
{
    int A = 2, B = 4;
    findWinner(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
     
    // Stores the Bitwise XOR of X & Y
    int playerA = (X ^ Y);
 
    boolean flag = false;
 
    // Traverse all possible pairs
    for(int i = 1; i <= X; i++)
    {
        for(int j = 1; j <= Y; j++)
        {
            int val = (i ^ j);
 
            // If a pair exists
            if (val > playerA)
            {
                flag = true;
                break;
            }
        }
 
        if (flag)
        {
            break;
        }
    }
 
    // If a pair is found
    if (flag)
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if there exists
# any pair (P, Q) whose Bitwise XOR
# is greater than the Bitwise XOR
# of X and Y
def findWinner(X, Y):
     
    # Stores the Bitwise XOR of X & Y
    playerA = (X ^ Y)
 
    flag = False
 
    # Traverse all possible pairs
    for i in range(1, X + 1, 1):
        for j in range(1, Y + 1, 1):
            val = (i ^ j)
 
            # If a pair exists
            if (val > playerA):
                flag = True
                break
 
        if (flag):
            break
 
    # If a pair is found
    if (flag):
        print("Yes")
    else:
        print("No")
 
# Driver Code
if __name__ == '__main__':
     
    A = 2
    B = 4
     
    findWinner(A, B)
 
# This code is contributed by bgangwar59


C#
// C# program for the above approach
using System.Collections.Generic;
using System;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
     
    // Stores the Bitwise XOR of X & Y
    int playerA = (X ^ Y);
 
    bool flag = false;
 
    // Traverse all possible pairs
    for(int i = 1; i <= X; i++)
    {
        for(int j = 1; j <= Y; j++)
        {
            int val = (i ^ j);
 
            // If a pair exists
            if (val > playerA)
            {
                flag = true;
                break;
            }
        }
 
        if (flag)
        {
            break;
        }
    }
 
    // If a pair is found
    if (flag)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by amreshkumar3


Javascript


C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second) {
        cout << "No";
    }
 
    // Otherwise,
    else {
        cout << "Yes";
    }
}
 
// Driver Code
int main()
{
    int A = 2, B = 4;
    findWinner(A, B);
 
    return 0;
}


Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second)
    {
        System.out.println("No");
    }
 
    // Otherwise,
    else
    {
        System.out.println("Yes");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to check if there exists
# any pair (P, Q) whose Bitwise XOR
# is greater than the Bitwise XOR
# of X and Y
def findWinner(X, Y):
     
    first = (X ^ Y)
    second = (X + Y)
 
    # Check for the invalid condition
    if (first == second):
        print ("No")
 
    # Otherwise,
    else:
        print ("Yes")
 
# Driver Code
if __name__ == '__main__':
     
    A, B = 2, 4
     
    findWinner(A, B)
 
# This code is contributed by mohit kumar 29


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second)
    {
        Console.Write("No");
    }
 
    // Otherwise,
    else
    {
        Console.Write("Yes");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by shivanisinghss2110


Javascript


输出:
No

时间复杂度: O(X * Y)
辅助空间: O(1)

有效的方法:上述方法也可以基于以下观察进行优化:

  • 对于任何两个整数PQ ,最大的按位异或值是(P + Q) ,只有在PQ的二进制表示中没有公共位时才能找到该值。
  • 有两种情况:
    • 情况 1:如果玩家 A 有两个整数产生最大的按位异或值,则打印“否”
    • 情况 2:在这种情况下, AB之间必须有一些公共位,因此总是存在两个整数PQ ,它们的按位异或总是大于AB的按位异或,其中(P ^ Q) = ( X | Y)

因此,根据上述观察,我们的想法是检查给定A^B的值是否等于A + B。如果发现是真的,则打印“否” 。否则,打印“是”

下面是上述方法的实现:

C++

// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second) {
        cout << "No";
    }
 
    // Otherwise,
    else {
        cout << "Yes";
    }
}
 
// Driver Code
int main()
{
    int A = 2, B = 4;
    findWinner(A, B);
 
    return 0;
}

Java

// Java program for the above approach
import java.lang.*;
import java.util.*;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second)
    {
        System.out.println("No");
    }
 
    // Otherwise,
    else
    {
        System.out.println("Yes");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by offbeat

Python3

# Python3 program for the above approach
 
# Function to check if there exists
# any pair (P, Q) whose Bitwise XOR
# is greater than the Bitwise XOR
# of X and Y
def findWinner(X, Y):
     
    first = (X ^ Y)
    second = (X + Y)
 
    # Check for the invalid condition
    if (first == second):
        print ("No")
 
    # Otherwise,
    else:
        print ("Yes")
 
# Driver Code
if __name__ == '__main__':
     
    A, B = 2, 4
     
    findWinner(A, B)
 
# This code is contributed by mohit kumar 29

C#

// C# program for the above approach
using System;
 
class GFG{
     
// Function to check if there exists
// any pair (P, Q) whose Bitwise XOR
// is greater than the Bitwise XOR
// of X and Y
static void findWinner(int X, int Y)
{
    int first = (X ^ Y);
    int second = (X + Y);
 
    // Check for the invalid condition
    if (first == second)
    {
        Console.Write("No");
    }
 
    // Otherwise,
    else
    {
        Console.Write("Yes");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int A = 2, B = 4;
     
    findWinner(A, B);
}
}
 
// This code is contributed by shivanisinghss2110

Javascript


输出:
No

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