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

#PS0000135. Tất cả giá trị XOR của đoạn con (All Subarray Xors)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tất cả giá trị XOR của đoạn con (All Subarray Xors)

All Subarray Xors

Source: CSES

Version: Phuoc Hung OJ Extended

Problem Statement

An integer array a1,…,ana_1,\ldots,a_n is given. A subarray consists of all elements from position ll through position rr, where 1≤l≤r≤n1\le l\le r\le n. Its value is the bitwise XOR al⊕⋯⊕ara_l\oplus\cdots\oplus a_r. List every distinct value attained by a nonempty subarray, including zero only if a nonempty subarray has XOR zero.

Input

  • Line 1: integer nn, the array length.
  • Line 2: nn integers a1,…,ana_1,\ldots,a_n in order.

Output

Print the number kk of distinct attainable XOR values on the first line. On the next line, print the kk values in strictly increasing order, separated by spaces.

Subtasks

  • Subtask 1 (20 points): 1≤n≤10001\le n\le 1000; 0≤ai≤10000000\le a_i\le 1000000; 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le 200000; 0≤ai≤10230\le a_i\le 1023; 1≤i≤n1\le i\le n.
  • Subtask 3 (50 points): 1≤n≤2000001\le n\le 200000; 0≤ai≤10000000\le a_i\le 1000000; 1≤i≤n1\le i\le n.

Example 1

Input

4
5 1 5 9

Output

7
1 4 5 8 9 12 13

Explanation

Lengths 1, 2, 3 and 4 give respectively (5, 1, 5, 9), (4, 4, 12), (1, 13) and (8). Removing duplicates and sorting yields the seven printed values.

Example 2

Input

2
7 7

Output

2
0 7

Explanation

Both singletons have XOR 7. The entire array has XOR 7⊕7=07\oplus7=0, so the answer set is {0,7}\{0,7\}.