D. Co-growing Sequence-Codeforces Round #731 (Div. 3)

题目链接Problem - 1547D - Codeforces

D. Co-growing Sequence

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

A sequence of non-negative integers a1,a2,…,ana1,a2,…,an is called growing if for all ii from 11 to n−1n−1 all ones (of binary representation) in aiai are in the places of ones (of binary representation) in ai+1ai+1 (in other words, ai&ai+1=aiai&ai+1=ai, where && denotes bitwise AND). If n=1n=1 then the sequence is considered growing as well.

For example, the following four sequences are growing:

  • [2,3,15,175][2,3,15,175] — in binary it's [102,112,11112,101011112][102,112,11112,101011112];
  • [5][5] — in binary it's [1012][1012];
  • [1,3,7,15][1,3,7,15] — in binary it's [12,112,1112,11112][12,112,1112,11112];
  • [0,0,0][0,0,0] — in binary it's [02,02,02][02,02,02].

The following three sequences are non-growing:

  • [3,4,5][3,4,5] — in binary it's [112,1002,1012][112,1002,1012];
  • [5,4,3][5,4,3] — in binary it's [1012,1002,0112][1012,1002,0112];
  • [1,2,4,8][1,2,4,8] — in binary it's [00012,00102,01002,10002][00012,00102,01002,10002].

Consider two sequences of non-negative integers x1,x2,…,xnx1,x2,…,xn and y1,y2,…,yny1,y2,…,yn. Let's call this pair of sequences co-growing if the sequence x1⊕y1,x2⊕y2,…,xn⊕ynx1⊕y1,x2⊕y2,…,xn⊕yn is growing where ⊕⊕ denotes bitwise XOR.

You are given a sequence of integers x1,x2,…,xnx1,x2,…,xn. Find the lexicographically minimal sequence y1,y2,…,yny1,y2,…,yn such that sequences xixi and yiyi are co-growing.

The sequence a1,a2,…,ana1,a2,…,an is lexicographically smaller than the sequence b1,b2,…,bnb1,b2,…,bn if there exists 1≤k≤n1≤k≤n such that ai=biai=bi for any 1≤i<k1≤i<k but ak<bkak<bk.

Input

The first line contains an integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

The first line of each test case contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105) — length of the sequence xixi.

The second line contains nn integers x1,x2,…,xnx1,x2,…,xn (0≤xi<2300≤xi<230) — elements of the sequence xixi.

It is guaranteed that the sum of nn overall all test cases doesn't exceed 2⋅1052⋅105.

Output

For each test case, print nn integers y1,y2,…,yny1,y2,…,yn (0≤yi<2300≤yi<230) — lexicographically minimal sequence such that such that it's co-growing with given sequence xixi.

Example

input

Copy

5
4
1 3 7 15
4
1 2 4 8
5
1 2 3 4 5
4
11 13 15 1
1
0

output

Copy

0 0 0 0 
0 1 3 7 
0 1 0 3 2 
0 2 0 14 
0 

---------------------------------------------------------------------------------------------------------------------------------

首先定义了一个特殊序列,后面的&前面的==前面的,&符号要求两个全是1,结果就是1,既然&之后,还等于前面的,那么前面元素各位上有的1,后面元素也必须有。

然后又要求构造一个b数组,使得新组成的ci=bi^ai 也满足这个特殊序列,且字典序最小

突破点就在字典序最小!

首先看b1,为了字典序最小,填1不填2,填0不填1,先填上,c1=0^a1就是a1

现在只需要求b2,显然b2已经确定。以此类推。

怎样求b2?  根据条件,b2^a2之后,要包含上a1全部的1,直接求不方便,我们可以求出来^之后的结果是  a1|a2  这个结果包含了两者全部的1,实际上就是一个a2上填补1的过程。^啥原理?不同则是1,相同是0,那么我们把b2上不包含的1提取出来,和b2上原有的0异或,答案就是a1|a2.

怎么提取,a1|a2-a2 ,二进制下前者对后者的减法和十进制下等价。这样做也保证了字典序最小,why?因为我们必须要填上这些个1,且仅需要填上这些个1,在满足条件的前提下,保证了不多添加1从而字典序最小。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
# include<map>
# include<iomanip>
# include<vector>
# pragma optimize(2)
using namespace std;
typedef long long int ll;
int a[200000+10];

int main ()
{
      int t;
      cin>>t;

      while(t--)
      {
          int n;
          cin>>n;
          for(int i=1;i<=n;i++)
          {
              cin>>a[i];

          }

          cout<<0<<" ";
          //&有一个1,就是1
          //^相同是0,不相同是1


          for(int i=2;i<=n;i++)
          {
              int now=(a[i]|a[i-1])-a[i];

              a[i]=(a[i]|a[i-1]);

              cout<<now<<" ";

          }

          cout<<'\n';


      }




    return 0;
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qinsanma and Code

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值