1632B.md (2823B)
1 Source: https://codeforces.com/problemset/problem/1632/B 2 3 B. Roof Construction 4 5 time limit per test 6 7 1 second 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 It has finally been decided to build a roof over the football field in School 179. Its construction will require placing $n$ consecutive vertical pillars. Furthermore, the headmaster wants the heights of all the pillars to form a permutation $p$ of integers from $0$ to $n - 1$, where $p_i$ is the height of the $i$-th pillar from the left $(1 \le i \le n)$. 22 23 As the chief, you know that the cost of construction of consecutive pillars is equal to the maximum value of the bitwise XOR of heights of all pairs of adjacent pillars. In other words, the cost of construction is equal to $\max\limits_{1 \le i \le n - 1}{p_i \oplus p_{i + 1}}$, where $\oplus$ denotes the [bitwise XOR operation](https://en.wikipedia.org/wiki/Bitwise_operation#XOR). 24 25 Find any sequence of pillar heights $p$ of length $n$ with the smallest construction cost. 26 27 In this problem, a permutation is an array consisting of $n$ distinct integers from $0$ to $n - 1$ in arbitrary order. For example, $[2,3,1,0,4]$ is a permutation, but $[1,0,1]$ is not a permutation ($1$ appears twice in the array) and $[1,0,3]$ is also not a permutation ($n=3$, but $3$ is in the array). 28 29 Input 30 31 Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 10^4$). Description of the test cases follows. 32 33 The only line for each test case contains a single integer $n$ ($2 \le n \le 2 \cdot 10^5$) — the number of pillars for the construction of the roof. 34 35 It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. 36 37 Output 38 39 For each test case print $n$ integers $p_1$, $p_2$, $\ldots$, $p_n$ — the sequence of pillar heights with the smallest construction cost. 40 41 If there are multiple answers, print any of them. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 50 2 51 52 3 53 54 5 55 56 10 57 ``` 58 59 Output 60 61 ``` 62 0 1 63 2 0 1 64 3 2 1 0 4 65 4 6 3 2 0 8 9 1 7 5 66 ``` 67 68 Note 69 70 For $n = 2$ there are $2$ sequences of pillar heights: 71 72 * $[0, 1]$ — cost of construction is $0 \oplus 1 = 1$. 73 * $[1, 0]$ — cost of construction is $1 \oplus 0 = 1$. 74 75 For $n = 3$ there are $6$ sequences of pillar heights: 76 77 * $[0, 1, 2]$ — cost of construction is $\max(0 \oplus 1, 1 \oplus 2) = \max(1, 3) = 3$. 78 * $[0, 2, 1]$ — cost of construction is $\max(0 \oplus 2, 2 \oplus 1) = \max(2, 3) = 3$. 79 * $[1, 0, 2]$ — cost of construction is $\max(1 \oplus 0, 0 \oplus 2) = \max(1, 2) = 2$. 80 * $[1, 2, 0]$ — cost of construction is $\max(1 \oplus 2, 2 \oplus 0) = \max(3, 2) = 3$. 81 * $[2, 0, 1]$ — cost of construction is $\max(2 \oplus 0, 0 \oplus 1) = \max(2, 1) = 2$. 82 * $[2, 1, 0]$ — cost of construction is $\max(2 \oplus 1, 1 \oplus 0) = \max(3, 1) = 3$.