PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000134. Đếm bộ ba có hai đoạn XOR bằng nhau (Count Triplets That Can Form Two Arrays of Equal XOR)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm bộ ba có hai đoạn XOR bằng nhau (Count Triplets That Can Form Two Arrays of Equal XOR)

Count Triplets That Can Form Two Arrays of Equal XOR

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An array of nn positive integers a0,a1,…,an−1a_0,a_1,\ldots,a_{n-1} is indexed from zero. Choose indices 0≤i<j≤k<n0\le i<j\le k<n. They define two adjacent nonempty intervals [i,j−1][i,j-1] and [j,k][j,k]. A triplet qualifies when

$$a_i\oplus\cdots\oplus a_{j-1}=a_j\oplus\cdots\oplus a_k.$$

Count qualifying triplets; differing in any index makes two triplets distinct.

Input

First line: integer nn.

Second line: nn integers a0,…,an−1a_0,\ldots,a_{n-1}.

Output

Print the number of qualifying triplets.

Subtasks

  • Subtask 1 (20 points): 1≤n≤301\le n\le 30; 1≤ai≤1081\le a_i\le 10^8 for every 0≤i<n0\le i<n.
  • Subtask 2 (30 points): 1≤n≤3001\le n\le 300; ai=1a_i=1 for every 0≤i<n0\le i<n.
  • Subtask 3 (50 points): 1≤n≤3001\le n\le 300; 1≤ai≤1081\le a_i\le 10^8 for every 0≤i<n0\le i<n.

Example 1

Input

5
2 3 1 6 7

Output

4

Explanation

The triplets (0,1,2),(0,2,2),(2,3,4),(2,4,4)(0,1,2),(0,2,2),(2,3,4),(2,4,4) have equal-side XORs 2,1,1,7 respectively. Only intervals [0,2][0,2] and [2,4][2,4] have XOR zero; each admits two cuts.

Example 2

Input

5
1 1 1 1 1

Output

10

Explanation

Four intervals of length 2 each have one cut, and two intervals of length 4 each have three cuts. Odd-length intervals have XOR 1 and cannot qualify. Total: 4+2⋅3=104+2\cdot3=10.