📌  相关文章
📜  计算四个数组中的所有 Quadruples,使得它们的 XOR 等于“x”

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

计算四个数组中的所有 Quadruples,使得它们的 XOR 等于“x”

给定四个数组和一个整数 x,找出满足 a^b^c^d = x 的四元组的数量,其中 a 属于 Arr 1 ,b 属于 Arr 2 ,c 属于 Arr 3 ,d 属于 Arr 4

例子 :

Input :  x = 0;
         a[] = { 1 , 10 };
         b[] = { 1 , 10 };
         c[] = { 1 , 10 };
         d[] = { 1 , 10 };
Output : 4
Explanation: There are total 8 Quadruples
with XOR value equals to 0.
{1, 1, 1, 1}, {10, 10, 10, 10}, {1, 1, 10, 10},
{10, 10, 1, 1}, {10, 1, 10, 1}, {1, 10, 1, 10},
{1, 10, 10, 1}, {10, 1, 1, 10}

Input : x = 3
        a[] = {0, 1}
        b[] = {2, 0}
        c[] = {0, 1}
        d[] = {0, 1}
Output : 4
Explanation: There are total 4 Quadruples
with XOR value equals to 3.
{0, 2, 0, 1}, {1, 2, 0, 0}, {0, 2, 1, 0},
{1, 2, 1, 1}

方法1(朴素的方法)
它可以使用 4 个循环来完成,覆盖每个四元组并检查它是否等于 x。

C++
// C++ program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
#include
using namespace std;
 
// Function to return the number of Quadruples with XOR
// equals to x such that every element of Quadruple is
// from different array.
int findQuadruples(int a[], int b[], int c[], int d[],
                    int x, int n)
{
    int count = 0;
    for (int i = 0 ; i < n ; i++)
        for (int j = 0 ; j < n ; j++)
            for (int k = 0 ; k < n ; k++)
                for (int l = 0 ; l < n ; l++)
 
                    // Check whether XOR is equal to x
                    if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
                        count++;
 
    return count;
}
 
// Driver Program
int main()
{
    int x = 3;
    int a[] = {0, 1};
    int b[] = {2, 0};
    int c[] = {0, 1};
    int d[] = {0, 1};
 
    int n = sizeof(a)/sizeof(a[0]);
 
    cout << findQuadruples(a, b, c, d, x, n) << endl;
 
    return 0;
}


Java
// Java program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
class GFG {
     
    // Function to return the number of Quadruples with XOR
    // equals to x such that every element of Quadruple is
    // from different array.
    static int findQuadruples(int a[], int b[], int c[],
                                int d[], int x, int n)
    {
        int count = 0;
        for (int i = 0 ; i < n ; i++)
            for (int j = 0 ; j < n ; j++)
                for (int k = 0 ; k < n ; k++)
                    for (int l = 0 ; l < n ; l++)
     
                        // Check whether XOR is equal to x
                        if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
                            count++;
     
        return count;
    }
     
    // Driver method
    public static void main(String[] args)
    {
        int x = 3;
        int a[] = {0, 1};
        int b[] = {2, 0};
        int c[] = {0, 1};
        int d[] = {0, 1};
     
        int n = a.length;
     
        System.out.println(findQuadruples(a, b, c, d, x, n));
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python3 program to find number of
# Quadruples from four arrays such
# that their XOR equals to 'x'
 
# Function to return the number of
# Quadruples with XOR equals to x
# such that every element of Quadruple
# is from different array.
def findQuadruples(a, b, c, d, x, n):
 
    count = 0
    for i in range(n):
        for j in range(n):
            for k in range(n):
                for l in range(n):
 
                    # Check whether XOR is equal to x
                    if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x):
                        count += 1
    return count
 
# Driver Code
x = 3
a = [0, 1]
b = [2, 0]
c = [0, 1]
d = [0, 1]
n = len(a)
print(findQuadruples(a, b, c, d, x, n))
 
# This code is contributed by Anant Agarwal.


C#
// C# program to find number of
// Quadruples from four arrays such
// that their XOR equals to 'x'
using System;
 
class GFG {
     
    // Function to return the number of
    // Quadruples with XOR equals to x such that
    // every element of Quadruple is from different array.
    static int findQuadruples(int []a, int []b, int []c,
                                int []d, int x, int n)
    {
        int count = 0;
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                for (int k = 0; k < n; k++)
                    for (int l = 0; l < n; l++)
     
                        // Check whether XOR is equal to x
                        if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
                            count++;
     
        return count;
    }
     
    // Driver method
    public static void Main()
    {
        int x = 3;
        int []a = {0, 1};
        int []b = {2, 0};
        int []c = {0, 1};
        int []d = {0, 1};
     
        int n = a.Length;
         
        // Function calling
        Console.Write(findQuadruples(a, b, c, d, x, n));
    }
}
 
// This code is contributed by nitin mittal.


PHP


Javascript


C++
// C++ program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
#include
using namespace std;
 
// Function to return the number of Quadruples with XOR
// equals to x such that every element of Quadruple is
// from different array.
int findQuadruples(int a[], int b[], int c[], int d[],
                   int x, int n)
{
    int count = 0;
    vector v1, v2;
 
    // Loop to get two different subsets
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < n ; j++)
        {
            // v1 for first and second array
            v1.push_back(a[i]^b[j]);
 
            // v2 for third and forth array.
            // x is a constant, so no need for
            // a separate loop
            v2.push_back(x ^ c[i] ^ d[j]);
        }
    }
 
    // Sorting the first set (Containing XOR
    // of a[] and b[]
    sort(v1.begin(), v1.end());
 
    // Finding the lower and upper bound of an
    // element to find its number
    for (int i = 0 ; i < v2.size() ; i++)
    {
        // Count number of occurrences of v2[i] in sorted
        // v1[] and add the count to result.
        auto low = lower_bound(v1.begin(), v1.end(), v2[i]);
        auto high = upper_bound(v1.begin(), v1.end(), v2[i]);
        count += high - low;
    }
 
    return count;
}
 
// Driver Program
int main()
{
    int  x = 3;
    int a[] = {0, 1};
    int b[] = {2, 0};
    int c[] = {0, 1};
    int d[] = {0, 1};
 
    int n = sizeof(a)/sizeof(a[0]);
 
    cout << findQuadruples(a, b, c, d, x, n) << endl;
 
    return 0;
}


Java
/*package whatever //do not write package name here */
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
 
class GFG
{
   
    // Function to return the number of Quadruples with XOR
    // equals to x such that every element of Quadruple is
    // from different array.
    public static int findQuadruples(int a[], int b[],
                                     int c[], int d[],
                                     int x, int n)
    {
        int count = 0;
        ArrayList v1 = new ArrayList<>();
        ArrayList v2 = new ArrayList<>();
 
        // Loop to get two different subsets
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
               
                // v1 for first and second array
                v1.add(a[i] ^ b[j]);
 
                // v2 for third and forth array.
                // x is a constant, so no need for
                // a separate loop
                v2.add(x ^ c[i] ^ d[j]);
            }
        }
        Collections.sort(v1);
 
        // Finding the lower and upper bound of an
        // element to find its number
        for (int i = 0; i < v2.size(); i++)
        {
           
            // Count number of occurrences of v2[i] in
            // sorted v1[] and add the count to result.
            int low
                = Collections.binarySearch(v1, v2.get(i));
            int j = low;
            for (j = low; j >= 0; j--)
            {
                if (v1.get(j) != v2.get(i))
                {
                    j++;
                    break;
                }
            }
            low = j;
            int high = Collections.binarySearch(v1, v2.get(i));
            j = high;
            for (j = high; j < v1.size(); j++)
            {
                if (v1.get(j) != v2.get(i)) {
                    break;
                }
            }
            high = j;
            count += high - low;
        }
 
        return count;
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int x = 3;
        int a[] = { 0, 1 };
        int b[] = { 2, 0 };
        int c[] = { 0, 1 };
        int d[] = { 0, 1 };
 
        int n = 2;
        System.out.println(
            findQuadruples(a, b, c, d, x, n));
    }
}
 
// This code is contributed by aditya7409


Python3
# Python3 program to find number of Quadruples
# from four arrays such that their XOR equals
# to 'x'
from bisect import bisect_left, bisect_right
 
# Function to return the number of Quadruples
# with XOR equals to x such that every element
# of Quadruple is from different array.
def findQuadruples(a, b, c, d, x, n):
     
    count = 0
    v1, v2 = [], []
 
    # Loop to get two different subsets
    for i in range(n):
        for j in range(n):
             
            # v1 for first and second array
            v1.append(a[i] ^ b[j])
 
            # v2 for third and forth array.
            # x is a constant, so no need for
            # a separate loop
            v2.append(x ^ c[i] ^ d[j])
 
    # Sorting the first set (Containing XOR
    # of aand b
    v1 = sorted(v1)
 
    # Finding the lower and upper bound of an
    # element to find its number
    for i in range(len(v2)):
         
        # Count number of occurrences of v2[i]
        # in sorted v1and add the count to result.
        low = bisect_left(v1, v2[i])
        high = bisect_right(v1, v2[i])
        count += high - low
 
    return count
 
# Driver code
if __name__ == '__main__':
     
    x = 3
    a = [ 0, 1 ]
    b = [ 2, 0 ]
    c = [ 0, 1 ]
    d = [ 0, 1 ]
 
    n = len(a)
 
    print(findQuadruples(a, b, c, d, x, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to return the number of Quadruples with XOR
    // equals to x such that every element of Quadruple is
    // from different array.
    static int findQuadruples(int[] a, int[] b,
                              int[] c, int[] d,
                              int x, int n)
    {
        int count = 0;
        List v1 = new List();
        List v2 = new List();
      
        // Loop to get two different subsets
        for (int i = 0 ; i < n ; i++)
        {
            for (int j = 0 ; j < n ; j++)
            {
               
                // v1 for first and second array
                v1.Add(a[i]^b[j]);
      
                // v2 for third and forth array.
                // x is a constant, so no need for
                // a separate loop
                v2.Add(x ^ c[i] ^ d[j]);
            }
        }
      
        // Sorting the first set (Containing XOR
        // of a[] and b[]
        v1.Sort();
      
        // Finding the lower and upper bound of an
        // element to find its number
        for (int i = 0 ; i < v2.Count; i++)
        {
            // Count number of occurrences of v2[i] in
            // sorted v1[] and add the count to result.
            int low = v1.BinarySearch(v2[i]);
            int j = low;
            for (j = low; j >= 0; j--)
            {
                if (v1[j] != v2[i])
                {
                    j++;
                    break;
                }
            }
            low = j;
            int high = v1.BinarySearch(v2[i]);
            j = high;
            for (j = high; j < v1.Count; j++)
            {
                if (v1[j] != v2[i])
                {
                    break;
                }
            }
            high = j;
            count += high - low;
        }    
        return count;
    }
 
  // Driver code
  static void Main()
  {
    int  x = 3;
    int[] a = {0, 1};
    int[] b = {2, 0};
    int[] c = {0, 1};
    int[] d = {0, 1};
  
    int n = a.Length;
    Console.WriteLine(findQuadruples(a, b, c, d, x, n));
  }
}
 
// This code is contributed by divyesh072019


Javascript


输出:

4

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

方法2(有效方法)
这个想法是在中间算法中使用 meet。
为此,请注意以下模式:
a ^ b ^ c ^ d = x
异或 c 和 d 两边
a ^ b ^ c ^ d ^ c ^ d = x ^ c ^ d
因为,c ^ c = 0 和 d ^ d = 0
a ^ b ^ 0 ^ 0 = x ^ c ^ d
即a ^ b = x ^ c ^ d

现在,我们只需要计算 a ^ b 和 x ^ c ^ d ,它们都可以在 O(n 2 ) 中计算,然后使用二分搜索找到元素。

C++

// C++ program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
#include
using namespace std;
 
// Function to return the number of Quadruples with XOR
// equals to x such that every element of Quadruple is
// from different array.
int findQuadruples(int a[], int b[], int c[], int d[],
                   int x, int n)
{
    int count = 0;
    vector v1, v2;
 
    // Loop to get two different subsets
    for (int i = 0 ; i < n ; i++)
    {
        for (int j = 0 ; j < n ; j++)
        {
            // v1 for first and second array
            v1.push_back(a[i]^b[j]);
 
            // v2 for third and forth array.
            // x is a constant, so no need for
            // a separate loop
            v2.push_back(x ^ c[i] ^ d[j]);
        }
    }
 
    // Sorting the first set (Containing XOR
    // of a[] and b[]
    sort(v1.begin(), v1.end());
 
    // Finding the lower and upper bound of an
    // element to find its number
    for (int i = 0 ; i < v2.size() ; i++)
    {
        // Count number of occurrences of v2[i] in sorted
        // v1[] and add the count to result.
        auto low = lower_bound(v1.begin(), v1.end(), v2[i]);
        auto high = upper_bound(v1.begin(), v1.end(), v2[i]);
        count += high - low;
    }
 
    return count;
}
 
// Driver Program
int main()
{
    int  x = 3;
    int a[] = {0, 1};
    int b[] = {2, 0};
    int c[] = {0, 1};
    int d[] = {0, 1};
 
    int n = sizeof(a)/sizeof(a[0]);
 
    cout << findQuadruples(a, b, c, d, x, n) << endl;
 
    return 0;
}

Java

/*package whatever //do not write package name here */
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
 
class GFG
{
   
    // Function to return the number of Quadruples with XOR
    // equals to x such that every element of Quadruple is
    // from different array.
    public static int findQuadruples(int a[], int b[],
                                     int c[], int d[],
                                     int x, int n)
    {
        int count = 0;
        ArrayList v1 = new ArrayList<>();
        ArrayList v2 = new ArrayList<>();
 
        // Loop to get two different subsets
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < n; j++)
            {
               
                // v1 for first and second array
                v1.add(a[i] ^ b[j]);
 
                // v2 for third and forth array.
                // x is a constant, so no need for
                // a separate loop
                v2.add(x ^ c[i] ^ d[j]);
            }
        }
        Collections.sort(v1);
 
        // Finding the lower and upper bound of an
        // element to find its number
        for (int i = 0; i < v2.size(); i++)
        {
           
            // Count number of occurrences of v2[i] in
            // sorted v1[] and add the count to result.
            int low
                = Collections.binarySearch(v1, v2.get(i));
            int j = low;
            for (j = low; j >= 0; j--)
            {
                if (v1.get(j) != v2.get(i))
                {
                    j++;
                    break;
                }
            }
            low = j;
            int high = Collections.binarySearch(v1, v2.get(i));
            j = high;
            for (j = high; j < v1.size(); j++)
            {
                if (v1.get(j) != v2.get(i)) {
                    break;
                }
            }
            high = j;
            count += high - low;
        }
 
        return count;
    }
 
  // Driver code
    public static void main(String[] args)
    {
        int x = 3;
        int a[] = { 0, 1 };
        int b[] = { 2, 0 };
        int c[] = { 0, 1 };
        int d[] = { 0, 1 };
 
        int n = 2;
        System.out.println(
            findQuadruples(a, b, c, d, x, n));
    }
}
 
// This code is contributed by aditya7409

Python3

# Python3 program to find number of Quadruples
# from four arrays such that their XOR equals
# to 'x'
from bisect import bisect_left, bisect_right
 
# Function to return the number of Quadruples
# with XOR equals to x such that every element
# of Quadruple is from different array.
def findQuadruples(a, b, c, d, x, n):
     
    count = 0
    v1, v2 = [], []
 
    # Loop to get two different subsets
    for i in range(n):
        for j in range(n):
             
            # v1 for first and second array
            v1.append(a[i] ^ b[j])
 
            # v2 for third and forth array.
            # x is a constant, so no need for
            # a separate loop
            v2.append(x ^ c[i] ^ d[j])
 
    # Sorting the first set (Containing XOR
    # of aand b
    v1 = sorted(v1)
 
    # Finding the lower and upper bound of an
    # element to find its number
    for i in range(len(v2)):
         
        # Count number of occurrences of v2[i]
        # in sorted v1and add the count to result.
        low = bisect_left(v1, v2[i])
        high = bisect_right(v1, v2[i])
        count += high - low
 
    return count
 
# Driver code
if __name__ == '__main__':
     
    x = 3
    a = [ 0, 1 ]
    b = [ 2, 0 ]
    c = [ 0, 1 ]
    d = [ 0, 1 ]
 
    n = len(a)
 
    print(findQuadruples(a, b, c, d, x, n))
 
# This code is contributed by mohit kumar 29

C#

// C# program to find number of Quadruples from four
// arrays such that their XOR equals to 'x'
using System;
using System.Collections.Generic;
class GFG
{
     
    // Function to return the number of Quadruples with XOR
    // equals to x such that every element of Quadruple is
    // from different array.
    static int findQuadruples(int[] a, int[] b,
                              int[] c, int[] d,
                              int x, int n)
    {
        int count = 0;
        List v1 = new List();
        List v2 = new List();
      
        // Loop to get two different subsets
        for (int i = 0 ; i < n ; i++)
        {
            for (int j = 0 ; j < n ; j++)
            {
               
                // v1 for first and second array
                v1.Add(a[i]^b[j]);
      
                // v2 for third and forth array.
                // x is a constant, so no need for
                // a separate loop
                v2.Add(x ^ c[i] ^ d[j]);
            }
        }
      
        // Sorting the first set (Containing XOR
        // of a[] and b[]
        v1.Sort();
      
        // Finding the lower and upper bound of an
        // element to find its number
        for (int i = 0 ; i < v2.Count; i++)
        {
            // Count number of occurrences of v2[i] in
            // sorted v1[] and add the count to result.
            int low = v1.BinarySearch(v2[i]);
            int j = low;
            for (j = low; j >= 0; j--)
            {
                if (v1[j] != v2[i])
                {
                    j++;
                    break;
                }
            }
            low = j;
            int high = v1.BinarySearch(v2[i]);
            j = high;
            for (j = high; j < v1.Count; j++)
            {
                if (v1[j] != v2[i])
                {
                    break;
                }
            }
            high = j;
            count += high - low;
        }    
        return count;
    }
 
  // Driver code
  static void Main()
  {
    int  x = 3;
    int[] a = {0, 1};
    int[] b = {2, 0};
    int[] c = {0, 1};
    int[] d = {0, 1};
  
    int n = a.Length;
    Console.WriteLine(findQuadruples(a, b, c, d, x, n));
  }
}
 
// This code is contributed by divyesh072019

Javascript


输出:

4

时间复杂度: O(n 2 log(n))
辅助空间: O(n 2 )