CF1689E.ANDfinity

普及/提高-

通过率:0%

AC君温馨提醒

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

题目描述

Bit Lightyear, to the ANDfinity and beyond!

After graduating from computer sciences, Vlad has been awarded an array a1,a2,,ana_1,a_2,\ldots,a_n of nn non-negative integers. As it is natural, he wanted to construct a graph consisting of nn vertices, numbered 1,2,,n1, 2,\ldots, n . He decided to add an edge between ii and jj if and only if ai&aj>0a_i \& a_j > 0 , where &\& denotes the bitwise AND operation.

Vlad also wants the graph to be connected, which might not be the case initially. In order to satisfy that, he can do the following two types of operations on the array:

  1. Choose some element aia_i and increment it by 11 .
  2. Choose some element aia_i and decrement it by 11 (possible only if ai>0a_i > 0 ).

It can be proven that there exists a finite sequence of operations such that the graph will be connected. So, can you please help Vlad find the minimum possible number of operations to do that and also provide the way how to do that?

输入格式

There are several test cases in the input data. The first line contains a single integer tt ( 1t10001 \leq t \leq 1000 ) — the number of test cases. This is followed by the test cases description.

The first line of each test case contains an integer nn ( 2n20002\leq n \leq 2000 ).

The second line of each test case contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 0ai<2300\leq a_i < 2^{30} ) — the elements of the array.

It is guaranteed that the sum of nn over all test cases does not exceed 20002000 .

输出格式

For each test case, print a single integer mm in the first line — the minimum number of operations. In the second line print the array after a valid sequence of operations that have been done such that the graph from the task becomes connected.

If there are multiple solutions, output any.

输入输出样例

  • 输入#1

    4
    5
    1 2 3 4 5
    2
    0 2
    2
    3 12
    4
    3 0 0 0

    输出#1

    0
    1 2 3 4 5
    2
    2 2
    1
    3 11
    3
    3 1 1 1

说明/提示

In the first test case, the graph is already connected.

In the second test case, we can increment 00 twice and end up with the array [2,2][2,2] . Since 2&2=2>02 \& 2 = 2 > 0 , the graph is connected. It can be shown that one operation is not enough.

In the third test case, we can decrement 1212 once and we end up with an array [3,11][3,11] . 3&11=3>03 \& 11 = 3 > 0 hence the graph is connected. One operation has to be done since the graph is not connected at the beginning.

首页