异或问题AI (CI翻译)
You are given an integer $n$.
你将得到一个整数 $n$。AI (CI翻译)
Define the beauty of an array $a$ as the value of $|a| - f(a)$, where $|a|$ is the length of $a$ and $f(a)$ is the number of non-empty subarrays$^{\text{∗}}$ of $a$ with bitwise XOR equal to $0$. Formally, $f(a)$ is the number of pairs $(l, r)$ such that $1 \le l \le r \le |a|$ and $a_l \oplus a_{l + 1} \oplus \ldots \oplus a_r = 0$$^{\text{†}}$.
定义数组 $a$ 的美丽度为 $|a| - f(a)$,其中 $|a|$ 是 $a$ 的长度,$f(a)$ 是 $a$ 中按位异或等于 $0$ 的非空子数组$^{\text{∗}}$的数量。形式化地说,$f(a)$ 是满足 $1 \le l \le r \le |a|$ 且 $a_l \oplus a_{l + 1} \oplus \ldots \oplus a_r = 0$$^{\text{†}}$ 的数对 $(l, r)$ 的数量。AI (CI翻译)
Among all arrays $a$ consisting of integers from $0$ to $n$ that have the maximum possible beauty, find one with maximum length.
在所有由 $0$ 到 $n$ 之间的整数构成、且具有最大可能美丽度的数组 $a$ 中,找出一个长度最大的数组。AI (CI翻译)
$^{\text{∗}}$An array $c$ is a subarray of an array $d$ if $c$ can be obtained from $d$ by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end. Two subarrays are considered different if the sets of positions of the deleted elements are different.$^{\text{†}}$$\oplus$ denotes the bitwise XOR operation.
$^{\text{∗}}$如果数组 $c$ 可以通过从数组 $d$ 的开头删除若干个(可能为零个或全部)元素,并从末尾删除若干个(可能为零个或全部)元素得到,则称数组 $c$ 是数组 $d$ 的一个子数组。如果删除元素的位置集合不同,则认为两个子数组不同。$^{\text{†}}$$\oplus$ 表示按位异或运算。AI (CI翻译)
Input
输入格式
AI (CI翻译)Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). The description of the test cases follows.
每个测试包含多个测试用例。第一行包含测试用例的数量 $t$($1 \le t \le 10^4$)。接下来给出各测试用例的描述。AI (CI翻译)
The only line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the upper bound on the elements of the array.
每个测试用例仅包含一行,其中包含一个整数 $n$($1 \le n \le 2 \cdot 10^5$)——数组元素的上界。AI (CI翻译)
It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$.
保证所有测试用例的 $n$ 之和不超过 $2 \cdot 10^5$。AI (CI翻译)
Output
输出格式
AI (CI翻译)For each test case, output a single integer $k$ — the length of the array. On the next line, output $k$ integers $a_1, a_2, \ldots, a_k$ ($0 \le a_i \le n$) — the elements of the array.
对于每个测试用例,输出一个整数 $k$——数组的长度。下一行输出 $k$ 个整数 $a_1, a_2, \ldots, a_k$($0 \le a_i \le n$)——数组的元素。AI (CI翻译)
It can be shown that under the given constraints the sum of $k$ over all test cases does not exceed $10^6$.
可以证明,在给定约束下,所有测试用例的 $k$ 之和不超过 $10^6$。AI (CI翻译)
If there are multiple suitable arrays, output any of them.
如果有多个符合条件的数组,输出任意一个即可。AI (CI翻译)
Note
说明
AI (CI翻译)In the first example, $n = 1$, so every element of the array is equal to $0$ or $1$. One possible solution is $a = [1, 1, 1]$: it has exactly two subarrays with bitwise XOR equal to $0$, namely $(l, r) = (1, 2)$ and $(l, r) = (2, 3)$. These two subarrays consist of the same elements, but they occupy different positions, so both of them are counted, and the beauty of $a$ is $3 - 2 = 1$. It can be proven that every array of length $4$ with elements equal to $0$ or $1$ has at least four subarrays with bitwise XOR equal to $0$, so its beauty is at most $0$.
在第一个样例中,$n = 1$,因此数组中的每个元素都等于 $0$ 或 $1$。一种可能的解是 $a = [1, 1, 1]$:它恰好有两个按位异或等于 $0$ 的子数组,即 $(l, r) = (1, 2)$ 和 $(l, r) = (2, 3)$。这两个子数组由相同的元素组成,但它们位于不同的位置,因此都会被计数,数组的美丽度为 $3 - 2 = 1$。可以证明,任何长度为 $4$ 且元素均为 $0$ 或 $1$ 的数组,至少有四个按位异或等于 $0$ 的子数组,因此其美丽度至多为 $0$。AI (CI翻译)
In the second example, one possible solution is $a = [2, 2, 1, 0, 2, 0, 1]$. Its subarrays with bitwise XOR equal to $0$ are $(l, r) = (1, 2)$, $(2, 7)$, $(4, 4)$, and $(6, 6)$, so the beauty of $a$ is $7 - 4 = 3$. The array $[1, 2, 2, 1, 2]$ has beauty $5 - 2 = 3$ as well, but it is shorter. It can be shown that $3$ is the maximum possible beauty for $n = 2$, and that no array of length greater than $7$ achieves it.
在第二个样例中,一种可能的解是 $a = [2, 2, 1, 0, 2, 0, 1]$。其按位异或等于 $0$ 的子数组为 $(l, r) = (1, 2)$、$(2, 7)$、$(4, 4)$ 和 $(6, 6)$,因此数组的美丽度为 $7 - 4 = 3$。数组 $[1, 2, 2, 1, 2]$ 的美丽度同样为 $5 - 2 = 3$,但它更短。可以证明,对于 $n = 2$,$3$ 是可能达到的最大美丽度,并且不存在长度大于 $7$ 且能达到该美丽度的数组。AI (CI翻译)
样例
2
1
2
3
1 1 1
7
2 2 1 0 2 0 1
题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(gpt-6.1-sol),如有出入请以原文为准。