Co-growing Sequence

题目:https://codeforces.com/contest/1547/problem/D

Co-growing Sequence

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

For example, the following four sequences are growing:

[2,3,15,175] — in binary it’s [102,112,11112,101011112];
[5] — in binary it’s [1012];
[1,3,7,15] — in binary it’s [12,112,1112,11112];
[0,0,0] — in binary it’s [02,02,02].
The following three sequences are non-growing:

[3,4,5] — in binary it’s [112,1002,1012];
[5,4,3] — in binary it’s [1012,1002,0112];
[1,2,4,8] — in binary it’s [00012,00102,01002,10002].
Consider two sequences of non-negative integers x1,x2,…,xn and y1,y2,…,yn. Let’s call this pair of sequences co-growing if the sequence x1⊕y1,x2⊕y2,…,xn⊕yn is growing where ⊕ denotes bitwise XOR.

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

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

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

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

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

It is guaranteed that the sum of n overall all test cases doesn’t exceed 2⋅105.

Output
For each test case, print n integers y1,y2,…,yn (0≤yi<230) — lexicographically minimal sequence such that such that it’s co-growing with given sequence xi.

Example
input
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
0 0 0 0
0 1 3 7
0 1 0 3 2
0 2 0 14
0

题目意思:
要你求最小序列y1,y2,…,yn
如果ai包含于ai+1即ai & ai+1 = ai 说明是增长的,那么 yi取最小yi = 0,
如果不是增长的即ai&ai+1 != ai ,再ai某一位上为1,ai+1对应的一位上为0,那么取异或,例如当ai = 11,ai+1 = 13时,它们的二进制ai = 1011,ai+1 = 1101,这时候从右边数第二位取异或,那么yi = 2;

请见谅哈,水平有限,对这个位运算解释的不太清楚。

AC代码:

//https://codeforces.com/contest/1547/problem/D 
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int read() {
	int x = 0;
	char ch = getchar();
	while (ch < '0' || ch > '9') ch = getchar();
	while (ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();

	return x;
}

int main() {

	int t;

	t = read();

	while (t--) {

		int n;
		n = read();
		int a[200005] = { 0 };
		for (int i = 1; i <= n; i++) {
			a[i] = read();
			
		}

		for (int i = 1; i <= n; i++) {

			if ((a[i] & a[i - 1]) == a[i-1])

				printf("0 ");
			else {

				int ans = 0;
				for (int j = 0; j < 30; j++) {
					
					if (((a[i] >> j) & 1) < ((a[i - 1] >> j) & 1)) ans |= (1 << j);
				}
				a[i] = a[i] ^ ans;
				printf("%d ", ans);
			}

		}
		printf("\n");

	}


	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值