CF1901C.Add, Divide and Floor

普及/提高-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

You are given an integer array a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^9). In one operation, you can choose an integer xx (0≤x≤10180 \le x \le 10^{18}) and replace aia_i with ⌊ai+x2⌋\lfloor \frac{a_i + x}{2} \rfloor (⌊y⌋\lfloor y \rfloor denotes rounding yy down to the nearest integer) for all ii from 11 to nn. Pay attention to the fact that all elements of the array are affected on each operation.

Print the smallest number of operations required to make all elements of the array equal.

If the number of operations is less than or equal to nn, then print the chosen xx for each operation. If there are multiple answers, print any of them.

给你一个整数数组 a1,a2,…,ana_1, a_2, \dots, a_n(其中 0≤ai≤1090 \le a_i \le 10^9)。在一次操作中,你可以选择一个整数 xx(其中 0≤x≤10180 \le x \le 10^{18}),并将所有 ii(从 11 到 nn)对应的 aia_i 替换为 ⌊ai+x2⌋\lfloor \frac{a_i + x}{2} \rfloor(⌊y⌋\lfloor y \rfloor 表示将 yy 向下取整至最近的整数)。注意:每次操作都会影响数组中的所有元素。

请输出使数组所有元素相等所需的最少操作次数。

如果操作次数不超过 nn,则还需按顺序输出每次操作所选的 xx 值。若存在多种可行方案,输出任意一种即可。

输入格式

The first line contains a single integer tt (1≤t≤1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer nn (1≤n≤2⋅1051 \le n \le 2 \cdot 10^5).

The second line contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (0≤ai≤1090 \le a_i \le 10^9).

The sum of nn over all testcases doesn't exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤1041 \le t \le 10^4)—— 测试用例的数量。

每个测试用例的第一行包含一个整数 nn(1≤n≤2⋅1051 \le n \le 2 \cdot 10^5)。

第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(0≤ai≤1090 \le a_i \le 10^9)。

所有测试用例的 nn 值之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each testcase, print the smallest number of operations required to make all elements of the array equal.

If the number of operations is less than or equal to nn, then print the chosen xx for each operation in the next line. If there are multiple answers, print any of them.

对于每个测试用例,输出使数组所有元素相等所需的最少操作次数。

如果操作次数小于或等于 nn,则在下一行输出每次操作所选的 xx。若存在多个答案,输出任意一个即可。

输入输出样例

  • 输入#1

    4
    1
    10
    2
    4 6
    6
    2 1 2 1 2 1
    2
    0 32

    输出#1

    0
    2
    2 5
    1
    1
    6

说明/提示

In the first testcase, all elements are already equal, so 00 operations are required. It doesn't matter if you print an empty line afterwards or not.

In the second testcase, you can't make less than 22 operations. There are multiple answers, let's consider the answer sequence [2,5][2, 5]. After applying an operation with x=2x = 2, the array becomes [⌊4+22⌋,⌊6+22⌋]=[3,4][\lfloor \frac{4 + 2}{2} \rfloor, \lfloor \frac{6 + 2}{2} \rfloor] = [3, 4]. After applying an operation with x=5x = 5 after that, the array becomes [⌊3+52⌋,⌊4+52⌋]=[4,4][\lfloor \frac{3 + 5}{2} \rfloor, \lfloor \frac{4 + 5}{2} \rfloor] = [4, 4]. Both elements are the same, so we are done.

In the last testcase, you can't make less than 66 operations. Since 66 is greater than nn, you don't have to print them. One possible answer sequence is [0,0,0,0,0,0][0, 0, 0, 0, 0, 0]. We are just dividing the second element by 22 every time and not changing the first element.

在第一个测试用例中,所有元素已经相等,因此需要 00 次操作。之后是否输出空行均不影响结果。

在第二个测试用例中,你无法少于 22 次操作完成目标。存在多种可行答案,我们以答案序列 [2,5][2, 5] 为例:首先执行 x=2x = 2 的操作,数组变为 [⌊4+22⌋,⌊6+22⌋]=[3,4][\lfloor \frac{4 + 2}{2} \rfloor, \lfloor \frac{6 + 2}{2} \rfloor] = [3, 4];再执行 x=5x = 5 的操作,数组变为 [⌊3+52⌋,⌊4+52⌋]=[4,4][\lfloor \frac{3 + 5}{2} \rfloor, \lfloor \frac{4 + 5}{2} \rfloor] = [4, 4]。此时两个元素相等,任务完成。

在最后一个测试用例中,你无法少于 66 次操作完成目标。由于 6>n6 > n,你无需输出这些操作。一种可能的答案序列是 [0,0,0,0,0,0][0, 0, 0, 0, 0, 0]。该序列每次仅将第二个元素除以 22(向下取整),而第一个元素保持不变。

输入解题思路,AI测评打分。不知道怎么写?

首页