📌  相关文章
📜  创建一个元素的XOR为y的序列

📅  最后修改于: 2021-04-22 07:33:13             🧑  作者: Mango

给定两个整数NY ,任务是生成N个不同的非负整数的序列,这些序列的所有元素的按位XOR等于Y,A 1 ^ A 2 ^ A 3 ^…。 ^ A N = Y ,其中^表示按位XOR。如果没有这样的顺序,则打印-1

例子:

方法:这是一个建设性的问题,可能包含多种解决方案。请按照以下步骤生成所需的序列:

  1. 将第一个N – 3个元素作为序列的一部分,即1,2,3,4,…,(N – 3)
  2. 令所选元素的XOR为xnum为尚未选择的整数。现在有两种情况:
    • 如果x = y,那么我们可以将numnum * 2(num ^(num * 2))加到剩余的最后3个数字中,因为num ^(num * 2)^(num ^(num * 2))= 0并且x ^ 0 = x
    • 如果x!= y,则可以添加0num(num ^ x ^ y),因为0 ^ num ^(num ^ x ^ y)= x ^ yx ^ x ^ y = y

注意:N = 2Y = 0时,序列是不可能的,因为该条件只能由两个相等的数来满足,这是不允许的。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to find and print
// the required sequence
void Findseq(int n, int x)
{
    const int pw1 = (1 << 17);
    const int pw2 = (1 << 18);
  
    // Base case
    if (n == 1)
        cout << x << endl;
  
    // Not allowed case
    else if (n == 2 && x == 0)
        cout << "-1" << endl;
    else if (n == 2)
        cout << x << " "
             << "0" << endl;
    else {
        int i;
        int ans = 0;
  
        // XOR of first N - 3 elements
        for (i = 1; i <= n - 3; i++) {
            cout << i << " ";
            ans = ans ^ i;
        }
  
        // Case 1: Add three integers whose XOR is 0
        if (ans == x)
            cout << pw1 + pw2 << " "
                 << pw1 << " " << pw2 << endl;
  
        // Case 2: Add three integers
        // whose XOR is equal to ans
        else
            cout << pw1 << " " << ((pw1 ^ x) ^ ans)
                 << " 0 " << endl;
    }
}
  
// Driver code
int main()
{
    int n = 4, x = 3;
    Findseq(n, x);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
  
class GFG {
  
    // Function to find and print
    // the required sequence
    static void Findseq(int n, int x)
    {
        int pw1 = 1 << 17;
        int pw2 = (1 << 18);
  
        // Base case
        if (n == 1) {
            System.out.println(x);
        }
  
        // Not allowed case
        else if (n == 2 && x == 0) {
            System.out.println("-1");
        }
        else if (n == 2) {
            System.out.println(x + " "
                               + "");
        }
        else {
            int i;
            int ans = 0;
  
            // XOR of first N - 3 elements
            for (i = 1; i <= n - 3; i++) {
                System.out.print(i + " ");
                ans = ans ^ i;
            }
  
            // Case 1: Add three integers whose XOR is 0
            if (ans == x) {
                System.out.println(pw1 + pw2 + " " + pw1 + " " + pw2);
            }
  
            // Case 2: Add three integers
            // whose XOR is equal to ans
            else {
                System.out.println(pw1 + " " + ((pw1 ^ x) ^ ans)
                                   + " 0 ");
            }
        }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int n = 4, x = 3;
        Findseq(n, x);
    }
}
  
// This code contributed by Rajput-Ji


Python3
# Python3 implementation of the approach 
  
# Function to find and print 
# the required sequence 
def Findseq(n, x) : 
      
    pw1 = (1 << 17); 
    pw2 = (1 << 18); 
  
    # Base case 
    if (n == 1) : 
        print(x); 
  
    # Not allowed case 
    elif (n == 2 and x == 0) :
        print("-1"); 
          
    elif (n == 2) :
        print(x, " ", "0"); 
          
    else :
      
        ans = 0; 
  
        # XOR of first N - 3 elements 
        for i in range(1, n - 2) :
            print(i, end = " "); 
            ans = ans ^ i; 
          
        # Case 1: Add three integers whose XOR is 0 
        if (ans == x) :
            print(pw1 + pw2, " ", pw1, " ", pw2); 
  
        # Case 2: Add three integers 
        # whose XOR is equal to ans 
        else :
            print(pw1, " ", ((pw1 ^ x) ^ ans), " 0 "); 
  
# Driver code 
if __name__ == "__main__" :
      
    n = 4; x = 3; 
    Findseq(n, x); 
      
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to find and print
    // the required sequence
    static void Findseq(int n, int x)
    {
        int pw1 = 1 << 17;
        int pw2 = (1 << 18);
  
        // Base case
        if (n == 1)
        {
            Console.WriteLine(x);
        }
  
        // Not allowed case
        else if (n == 2 && x == 0) 
        {
            Console.WriteLine("-1");
        }
        else if (n == 2) 
        {
            Console.WriteLine(x + " "
                            + "");
        }
        else 
        {
            int i;
            int ans = 0;
  
            // XOR of first N - 3 elements
            for (i = 1; i <= n - 3; i++)
            {
                Console.Write(i + " ");
                ans = ans ^ i;
            }
  
            // Case 1: Add three integers whose XOR is 0
            if (ans == x)
            {
                Console.WriteLine(pw1 + pw2 + " " + pw1 + " " + pw2);
            }
  
            // Case 2: Add three integers
            // whose XOR is equal to ans
            else
            {
                Console.WriteLine(pw1 + " " + ((pw1 ^ x) ^ ans)
                                + " 0 ");
            }
        }
    }
  
    // Driver code
    public static void Main()
    {
        int n = 4, x = 3;
        Findseq(n, x);
    }
}
  
// This code contributed by anuj_67..


PHP


输出:
1 131072 131074 0

时间复杂度: O(N)