D - Xor Sum 2 Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 500500

問題文

長さ NN の整数列 AA があります。

次の条件を満たす整数 ll, rr ( 1lrN1 \leq l \leq r \leq N ) の組の個数を求めてください。

  • Al xor Al+1 xor ... xor Ar=Al + Al+1 + ... + ArA_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r

xorの説明

整数 c1,c2,...,cmc_1, c_2, ..., c_mxorxor は以下のように定義されます。

  • xorxor の値を XX とおく。XX22 進数表記したときの 2k2^k ( 0k0 \leq k, kk は整数 ) の位の値は、c1,c2,...cmc_1, c_2, ...c_m のうち、22 進数表記したときの 2k2^k の位の値が 11 となるものが奇数個ならば 11、偶数個ならば 00 となる。

例えば、3355xorxor の値は、3322 進数表記が 0110115522 進数表記が 101101 のため、22 進数表記が 11011066 となります。

制約

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0Ai<2200 \leq A_i < 2^{20}
  • 入力はすべて整数である

入力

入力は以下の形式で標準入力から与えられる。

NN
A1A_1 A2A_2 ...... ANA_N

出力

条件を満たす整数 ll, rr ( 1lrN1 \leq l \leq r \leq N ) の組の個数を出力せよ。


入力例 1Copy

Copy
4
2 5 4 6

出力例 1Copy

Copy
5

明らかに、(l,r)=(1,1),(2,2),(3,3),(4,4)(l,r)=(1,1),(2,2),(3,3),(4,4) は条件を満たします。 また、(l,r)=(1,2)(l,r)=(1,2) の場合、A1 xor A2=A1 + A2=7A_1\ xor\ A_2 = A_1\ +\ A_2 = 7 となるので、これも条件を満たします。 ほかに条件を満たす組はないので、答えは 55 になります。


入力例 2Copy

Copy
9
0 0 0 0 0 0 0 0 0

出力例 2Copy

Copy
45

入力例 3Copy

Copy
19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

出力例 3Copy

Copy
37

Score : 500500 points

Problem Statement

There is an integer sequence AA of length NN.

Find the number of the pairs of integers ll and rr (1lrN1 \leq l \leq r \leq N) that satisfy the following condition:

  • Al xor Al+1 xor ... xor Ar=Al + Al+1 + ... + ArA_l\ xor\ A_{l+1}\ xor\ ...\ xor\ A_r = A_l\ +\ A_{l+1}\ +\ ...\ +\ A_r

Here, xorxor denotes the bitwise exclusive OR.

Definition of XOR

The XOR of integers c1,c2,...,cmc_1, c_2, ..., c_m is defined as follows:

  • Let the XOR be XX. In the binary representation of XX, the digit in the 2k2^k's place (0k0 \leq k; kk is an integer) is 11 if there are an odd number of integers among c1,c2,...cmc_1, c_2, ...c_m whose binary representation has 11 in the 2k2^k's place, and 00 if that number is even.

For example, let us compute the XOR of 33 and 55. The binary representation of 33 is 011011, and the binary representation of 55 is 101101, thus the XOR has the binary representation 110110, that is, the XOR is 66.

Constraints

  • 1N2×1051 \leq N \leq 2 \times 10^5
  • 0Ai<2200 \leq A_i < 2^{20}
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NN
A1A_1 A2A_2 ...... ANA_N

Output

Print the number of the pairs of integers ll and rr (1lrN1 \leq l \leq r \leq N) that satisfy the condition.


Sample Input 1Copy

Copy
4
2 5 4 6

Sample Output 1Copy

Copy
5

(l,r)=(1,1),(2,2),(3,3),(4,4)(l,r)=(1,1),(2,2),(3,3),(4,4) clearly satisfy the condition. (l,r)=(1,2)(l,r)=(1,2) also satisfies the condition, since A1 xor A2=A1 + A2=7A_1\ xor\ A_2 = A_1\ +\ A_2 = 7. There are no other pairs that satisfy the condition, so the answer is 55.


Sample Input 2Copy

Copy
9
0 0 0 0 0 0 0 0 0

Sample Output 2Copy

Copy
45

Sample Input 3Copy

Copy
19
885 8 1 128 83 32 256 206 639 16 4 128 689 32 8 64 885 969 1

Sample Output 3Copy

Copy
37


2025-04-07 (Mon)
13:57:46 +00:00