jiejiejiang2004

题解:Pinely Round 4 (Div. 1 + Div. 2) B

B. AND Reconstruction

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

You are given an array \(b\) of \(n - 1\) integers.

An array \(a\) of \(n\) integers is called good if \(b_i = a_i \, \& \, a_{i + 1}\) for \(1 \le i \le n-1\), where \(\&\) denotes the bitwise AND operator.

Construct a good array, or report that no good arrays exist.

给你一个由 \(n - 1\) 个整数组成的数组 \(b\)

如果 \(b_i = a_i \, \& \, a_{i + 1}\)\(1 \le i \le n-1\) ,其中 \(\&\) 表示 bitwise AND 运算符,那么由 \(n\) 个整数组成的数组 \(a\) 称为好数组。

构造一个好数组,或报告不存在好数组。

Input

Each test contains multiple test cases. The first line contains a single integer \(t\) (\(1 \le t \le 10^4\)) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer \(n\) (\(2 \le n \le 10^5\)) — the length of the array \(a\).

The second line of each test case contains \(n - 1\) integers \(b_1, b_2, \ldots, b_{n - 1}\) (\(0 \le b_i \lt 2^{30}\)) — the elements of the array \(b\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(10^5\).

输入

每个测试包含多个测试用例。第一行包含一个整数 \(t\) ( \(1 \le t \le 10^4\) ) - 测试用例的数量。测试用例说明如下。

每个测试用例的第一行包含一个整数 \(n\) ( \(2 \le n \le 10^5\) ) - 数组 \(a\) 的长度。

每个测试用例的第二行包含 \(n - 1\) 个整数 \(b_1, b_2, \ldots, b_{n - 1}\) ( \(0 \le b_i \lt 2^{30}\) ) - 数组 \(b\) 的元素。

保证所有测试用例中 \(n\) 的总和不超过 \(10^5\)

Output

For each test case, output a single integer \(-1\) if no good arrays exist.

Otherwise, output \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) (\(0 \le a_i \lt 2^{30}\)) — the elements of a good array \(a\).

If there are multiple solutions, you may output any of them.

输出

对于每个测试用例,如果不存在良好的数组,则输出单个整数 \(-1\)

否则,输出 \(n\) 个空格分隔的整数 \(a_1, a_2, \ldots, a_n\) ( \(0 \le a_i \lt 2^{30}\) ) - 好数组 \(a\) 的元素。

如果有多个解决方案,可以输出其中任意一个。

Example

Input

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

Output

5 3
3 2 1
-1
3 7 5 6 3

Note

In the first test case, \(b = [1]\). A possible good array is \(a=[5, 3]\), because \(a_1 \, \& \, a_2 = 5 \, \& \, 3 = 1 = b_1\).

In the second test case, \(b = [2, 0]\). A possible good array is \(a=[3, 2, 1]\), because \(a_1 \, \& \, a_2 = 3 \, \& \, 2 = 2 = b_1\) and \(a_2 \, \& \, a_3 = 2 \, \& \, 1 = 0 = b_2\).

In the third test case, \(b = [1, 2, 3]\). It can be shown that no good arrays exist, so the output is \(-1\).

In the fourth test case, \(b = [3, 5, 4, 2]\). A possible good array is \(a=[3, 7, 5, 6, 3]\).

在第一个测试案例中, \(b = [1]\) .一个可能的好数组是 \(a=[5, 3]\) ,因为 \(a_1 \, \& \, a_2 = 5 \, \& \, 3 = 1 = b_1\)

在第二个测试用例中, \(b = [2, 0]\) 。可能的好数组是 \(a=[3, 2, 1]\) ,因为 \(a_1 \, \& \, a_2 = 3 \, \& \, 2 = 2 = b_1\)\(a_2 \, \& \, a_3 = 2 \, \& \, 1 = 0 = b_2\)

在第三个测试案例中, \(b = [1, 2, 3]\) 。可以证明不存在好的数组,因此输出为 \(-1\)

在第四个测试用例中, \(b = [3, 5, 4, 2]\) 。一个可能的好数组是 \(a=[3, 7, 5, 6, 3]\)

题意

给你一个数组b
你要创建一个数组a,使得 \(b_i = a_i \, \& \, a_{i + 1}\)
如果全部成立,那这就是一个好数组

  • 如果存在好数组,输出任意一个好数组
  • 如果不存在好数组,输出 \(-1\)

题解

已知

\[b_i = a_i \& a_{i + 1} \]

可得(至少)

\[a_{i} = b_i | b_{i-1} \]

根据这个条件从数组b逆推数组a(顺序遍历即可)
其中 \(a_0 = b_0\) , \(a_{n-1} = b_{nm\)

逆推回来之后我们要检验数组a是否为好数组
顺序遍历数组b和数组a判断即可

如果是好数组,那就是好数组
如果不是好数组,那就没有好数组了

代码

#include <bits/stdc++.h>
#define int long long

const int N = 2e5 + 10;
int a[N],b[N];

void solve() {
    int n;
    std::cin >> n;
    n--;
    for(int i = 0 ; i < n ; i ++) {
        std::cin >> b[i];
    }
    for(int i = 1 ; i < n ; i ++) {
        a[i] = b[i]|b[i-1];
    }
    a[n] = b[n-1];
    a[0] = b[0];

    for(int i = 0 ; i < n ; i ++) {
        if(b[i] != (a[i]&a[i+1])) {
            std::cout << -1 << "\n";
            return ;
        }
    }

    for(int i = 0 ; i <= n ; i ++) std::cout << a[i] << " ";
    std::cout << "\n";
}

signed main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);

    int t;
    std::cin >> t;
    while(t--) {
        solve();
    }
    return 0;
}

posted on 2024-07-31 15:08  Jiejiejiang  阅读(7)  评论(0编辑  收藏  举报

导航