Count pairs

3.8

11 votes
Bit Manipulation, Basic Programming, Basics of Bit Manipulation, C++
Problem

You are given the following:

  • An integer N
  • An integer X
  • An integer Y
  • An array A of N elements
  • An array B of N elements

Find the number of pairs of (i,j) such that:

  • (A[i]^B[j])&X=(A[i]^B[j])&Y,  where ^ represents bitwise XOR operator and & represents bitwise AND operator.
  • 1i,jN

Note: Assume 1-based indexing.

Input

  • The first line contains a single integer T that denotes the number of test cases. 
  • For each test case:
    • The first line contains an integer N.
    • The second line contains an integer X.
    • The third line contains an integer Y.
    • The next line contains N space-separated integers denoting array A.
    • The next line contains N space-separated integers denoting array B.

Output format

For each test case, print the number of pairs (i,j) that satisfy the conditions in a new line.

Constraints

1T101N1051X,Y1060A[i],B[i]106

Sample Input
1
3
5
4
2 4 6
3 5 6
Sample Output
3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Approach

  • For pair (1,3):
    • (2^6) & 5 = .(2^6) & 4 = 4
  • For pair (2,3):
    • (4^6) & 5 = .(5^6) & 4 = 4
  • For pair (3,3)
    • (6^6) & 5 = .(6^6) & 4 = 0
  • Therefore, the required answer is 3.
Editor Image

?