Problem 703
Count Subarrays with XOR K
Medium · 20 pts · Array, Hash Table, Bit Manipulation, Prefix Sum
Problem
Count the non-empty blocks of consecutive elements whose XOR (bitwise exclusive or of all their elements) equals K. Blocks at different positions count separately. Let P be the prefix XOR, so the block from i to j has XOR P[j+1] XOR P[i]. A block ending at j qualifies when P[i] = P[j+1] XOR K, so keep a hash map counting the prefix XORs seen so far and add the count of P[j+1] XOR K at every step.
Input
The first line contains N and K. The second line contains N integers.
Output
Print the number of subarrays whose XOR equals K.
Constraints
1 <= N <= 100000; 0 <= A[i], K <= 10^9
Example input
5 6 4 2 2 6 4
Example output
4
Explanation
In 4 2 2 6 4 with K = 6, the blocks 4 2, 4 2 2 6 4, 2 2 6 and 6 each have XOR 6. So the answer is 4.