codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1547D.md (2625B)


      1 Source: https://codeforces.com/problemset/problem/1547/D
      2 
      3 D. Co-growing Sequence
      4 
      5 time limit per test
      6 
      7 2 seconds
      8 
      9 memory limit per test
     10 
     11 512 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 A sequence of non-negative integers $a_1, a_2, \dots, a_n$ is called growing if for all $i$ from $1$ to $n - 1$ all ones (of binary representation) in $a_i$ are in the places of ones (of binary representation) in $a_{i + 1}$ (in other words, $a_i \:\&\: a_{i + 1} = a_i$, where $\&$ denotes [bitwise AND](https://en.wikipedia.org/wiki/Bitwise_operation#AND)). If $n = 1$ then the sequence is considered growing as well.
     22 
     23 For example, the following four sequences are growing:
     24 
     25 * $[2, 3, 15, 175]$ — in binary it's $[10_2, 11_2, 1111_2, 10101111_2]$;
     26 * $[5]$ — in binary it's $[101_2]$;
     27 * $[1, 3, 7, 15]$ — in binary it's $[1_2, 11_2, 111_2, 1111_2]$;
     28 * $[0, 0, 0]$ — in binary it's $[0_2, 0_2, 0_2]$.
     29 
     30 The following three sequences are non-growing:
     31 
     32 * $[3, 4, 5]$ — in binary it's $[11_2, 100_2, 101_2]$;
     33 * $[5, 4, 3]$ — in binary it's $[101_2, 100_2, 011_2]$;
     34 * $[1, 2, 4, 8]$ — in binary it's $[0001_2, 0010_2, 0100_2, 1000_2]$.
     35 
     36 Consider two sequences of non-negative integers $x_1, x_2, \dots, x_n$ and $y_1, y_2, \dots, y_n$. Let's call this pair of sequences co-growing if the sequence $x_1 \oplus y_1, x_2 \oplus y_2, \dots, x_n \oplus y_n$ is growing where $\oplus$ denotes [bitwise XOR](https://en.wikipedia.org/wiki/Bitwise_operation#XOR).
     37 
     38 You are given a sequence of integers $x_1, x_2, \dots, x_n$. Find the lexicographically minimal sequence $y_1, y_2, \dots, y_n$ such that sequences $x_i$ and $y_i$ are co-growing.
     39 
     40 The sequence $a_1, a_2, \dots, a_n$ is lexicographically smaller than the sequence $b_1, b_2, \dots, b_n$ if there exists $1 \le k \le n$ such that $a_i = b_i$ for any $1 \le i \lt k$ but $a_k \lt b_k$.
     41 
     42 Input
     43 
     44 The first line contains an integer $t$ ($1 \le t \le 10^4$). Then $t$ test cases follow.
     45 
     46 The first line of each test case contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) — length of the sequence $x_i$.
     47 
     48 The second line contains $n$ integers $x_1, x_2, \dots, x_n$ ($0 \le x_i \lt 2^{30}$) — elements of the sequence $x_i$.
     49 
     50 It is guaranteed that the sum of $n$ overall all test cases doesn't exceed $2 \cdot 10^5$.
     51 
     52 Output
     53 
     54 For each test case, print $n$ integers $y_1, y_2, \dots, y_n$ ($0 \le y_i \lt 2^{30}$) — lexicographically minimal sequence such that such that it's co-growing with given sequence $x_i$.
     55 
     56 Example
     57 
     58 Input
     59 
     60 ```
     61 5
     62 
     63 4
     64 
     65 1 3 7 15
     66 
     67 4
     68 
     69 1 2 4 8
     70 
     71 5
     72 
     73 1 2 3 4 5
     74 
     75 4
     76 
     77 11 13 15 1
     78 
     79 1
     80 
     81 0
     82 ```
     83 
     84 Output
     85 
     86 ```
     87 0 0 0 0 
     88 0 1 3 7 
     89 0 1 0 3 2 
     90 0 2 0 14 
     91 0
     92 ```