Bob And The Magic Pairs

3.3

3 votes
Trie, Advanced Data Structures, Data Structures
Problem

Bob is a brilliant student in the class, so his teacher assigned him a problem. He has been given an array A containing N positive integers and two integers U and V. He has been asked to find the number of magic pairs of array indices.

A pair of integers (i,j) is called a magic pair if i<j and UA[i]^A[j]V where ^ denotes the bitwise XOR operation.

Help Bob to find the number of magic pairs in the given array.

Input Format:

  • The first line contains a single integer T denoting the number of test cases.
  • The first line of each test case contains the integers N, U, and V separated by spaces.
  • The second line of each test case contains N spaced integers, the elements of the array A.

Output Format:

For each test case, print the number of magic pairs in the given array.

Constraints:

1T101N1040A[i]1090UV109

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

For test case 1:
There are 8 magic pairs in the given array:

  • A[0] XOR A[2] = 13
  • A[0] XOR A[3] = 11
  • A[0] XOR A[4] = 8
  • A[1] XOR A[2] = 12
  • A[1] XOR A[3] = 10
  • A[1] XOR A[4] = 9
  • A[2] XOR A[3] = 6
  • A[2] XOR A[4] = 5

For test case 2:
Since the array has only one element, there are no magic pairs.

Editor Image

?