题解: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 b b of n − 1 n - 1 n1 integers.

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

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

给你一个由 n − 1 n - 1 n1 个整数组成的数组 b b b

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

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

Input

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

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

The second line of each test case contains n − 1 n - 1 n1 integers b 1 , b 2 , … , b n − 1 b_1, b_2, \ldots, b_{n - 1} b1,b2,,bn1 ( 0 ≤ b i < 2 30 0 \le b_i \lt 2^{30} 0bi<230) — the elements of the array b b b.

It is guaranteed that the sum of n n n over all test cases does not exceed 1 0 5 10^5 105.

输入

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

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

每个测试用例的第二行包含 n − 1 n - 1 n1 个整数 b 1 , b 2 , … , b n − 1 b_1, b_2, \ldots, b_{n - 1} b1,b2,,bn1 ( 0 ≤ b i < 2 30 0 \le b_i \lt 2^{30} 0bi<230 ) - 数组 b b b 的元素。

保证所有测试用例中 n n n 的总和不超过 1 0 5 10^5 105

Output

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

Otherwise, output n n n space-separated integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i < 2 30 0 \le a_i \lt 2^{30} 0ai<230) — the elements of a good array a a a.

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

输出

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

否则,输出 n n n 个空格分隔的整数 a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 0 ≤ a i < 2 30 0 \le a_i \lt 2^{30} 0ai<230 ) - 好数组 a a 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 ] b = [1] b=[1]. A possible good array is a = [ 5 , 3 ] a=[5, 3] a=[5,3], because a 1   &   a 2 = 5   &   3 = 1 = b 1 a_1 \, \& \, a_2 = 5 \, \& \, 3 = 1 = b_1 a1&a2=5&3=1=b1.

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

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

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

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

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

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

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

题意

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

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

题解

已知
b i = a i & a i + 1 b_i = a_i \& a_{i + 1} bi=ai&ai+1
可得(至少)
a i = b i ∣ b i − 1 a_{i} = b_i | b_{i-1} ai=bibi1
根据这个条件从数组b逆推数组a(顺序遍历即可)
其中 a 0 = b 0 a_0 = b_0 a0=b0 , KaTeX parse error: Expected '}', got 'EOF' at end of input: 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;
}

转载自博客https://www.cnblogs.com/jiejiejiang2004/p/18334703
博主已同意,我就是博主

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,Codeforces Round 511 (Div. 1)是一个比赛的名称。然而,引用内容中没有提供与这个比赛相关的具体信息或问题。因此,我无法回答关于Codeforces Round 511 (Div. 1)的问题。如果您有关于这个比赛的具体问题,请提供更多的信息,我将尽力回答。 #### 引用[.reference_title] - *1* [Codeforces Round 860 (Div. 2)题解](https://blog.csdn.net/qq_60653991/article/details/129802687)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Codeforces Round 867 (Div. 3)(A题到E题)](https://blog.csdn.net/wdgkd/article/details/130370975)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Codeforces Round 872 (Div. 2)(前三道](https://blog.csdn.net/qq_68286180/article/details/130570952)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值