1901C.md (2486B)
1 Source: https://codeforces.com/problemset/problem/1901/C 2 3 C. Add, Divide and Floor 4 5 time limit per test 6 7 2 seconds 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 You are given an integer array $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$). In one operation, you can choose an integer $x$ ($0 \le x \le 10^{18}$) and replace $a_i$ with $\lfloor \frac{a_i + x}{2} \rfloor$ ($\lfloor y \rfloor$ denotes rounding $y$ down to the nearest integer) for all $i$ from $1$ to $n$. Pay attention to the fact that all elements of the array are affected on each operation. 22 23 Print the smallest number of operations required to make all elements of the array equal. 24 25 If the number of operations is less than or equal to $n$, then print the chosen $x$ for each operation. If there are multiple answers, print any of them. 26 27 Input 28 29 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of testcases. 30 31 The first line of each testcase contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). 32 33 The second line contains $n$ integers $a_1, a_2, \dots, a_n$ ($0 \le a_i \le 10^9$). 34 35 The sum of $n$ over all testcases doesn't exceed $2 \cdot 10^5$. 36 37 Output 38 39 For each testcase, print the smallest number of operations required to make all elements of the array equal. 40 41 If the number of operations is less than or equal to $n$, then print the chosen $x$ for each operation in the next line. If there are multiple answers, print any of them. 42 43 Example 44 45 Input 46 47 ``` 48 4 49 50 1 51 52 10 53 54 2 55 56 4 6 57 58 6 59 60 2 1 2 1 2 1 61 62 2 63 64 0 32 65 ``` 66 67 Output 68 69 ``` 70 0 71 2 72 2 5 73 1 74 1 75 6 76 ``` 77 78 Note 79 80 In the first testcase, all elements are already equal, so $0$ operations are required. It doesn't matter if you print an empty line afterwards or not. 81 82 In the second testcase, you can't make less than $2$ operations. There are multiple answers, let's consider the answer sequence $[2, 5]$. After applying an operation with $x = 2$, the array becomes $[\lfloor \frac{4 + 2}{2} \rfloor, \lfloor \frac{6 + 2}{2} \rfloor] = [3, 4]$. After applying an operation with $x = 5$ after that, the array becomes $[\lfloor \frac{3 + 5}{2} \rfloor, \lfloor \frac{4 + 5}{2} \rfloor] = [4, 4]$. Both elements are the same, so we are done. 83 84 In the last testcase, you can't make less than $6$ operations. Since $6$ is greater than $n$, you don't have to print them. One possible answer sequence is $[0, 0, 0, 0, 0, 0]$. We are just dividing the second element by $2$ every time and not changing the first element.