Problem 677
Count Triplets That Can Form Two Arrays of Equal XOR
Medium · 20 pts · Array, Bit Manipulation, Prefix Sum
Problem
Count the triplets of indices (i, j, k) with 0 <= i < j <= k < N such that A[i] XOR ... XOR A[j-1] equals A[j] XOR ... XOR A[k]. The two XORs are equal exactly when the XOR of the whole block A[i..k] is 0, and then every j between i + 1 and k works. So for each pair i < k whose block XOR is 0, add k - i. Prefix XORs make each block XOR O(1), and the answer can exceed 2^31 for the largest inputs.
Input
The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].
Output
Print the number of triplets.
Constraints
1 <= N <= 300; 1 <= A[i] <= 10^8
Example input
5 2 3 1 6 7
Example output
4
Explanation
In 2 3 1 6 7, the blocks with XOR 0 are 2 3 1 (indices 0 to 2) and 1 6 7 (indices 2 to 4). Each allows 2 choices of j, giving 4 triplets such as (0, 1, 2) and (2, 3, 4).