ABC 171E Red Scarf (位运算)

题目链接

题目大意

有一个长度为 n n n 的序列 b b b n n n 为偶数)。现在已知序列 a a a,满足 a i = b 1 ⊕ ⋯ ⊕ b i − 1 ⊕ ⋯ b n a_i = b_1 \oplus \cdots \oplus b_{i-1} \oplus \cdots b_n ai=b1bi1bn,要求还原出 b b b 序列

前置芝士

异或基础知识

解题思路

t o t = a 1 ⊕ a 2 ⊕ ⋯ ⊕ a n − 1 ⊕ a n tot = a_1 \oplus a_2 \oplus \cdots \oplus a_{n-1} \oplus a_n tot=a1a2an1an
可以发现,所有 b i b_i bi 都在里面异或了 n − 1 n-1 n1
又因为 n n n 为偶数( n − 1 n-1 n1 为奇数),所以最后的到的结果 t o t = b 1 ⊕ b 2 ⊕ ⋯ ⊕ b n − 1 ⊕ b n tot = b_1 \oplus b_2 \oplus \cdots \oplus b_{n-1} \oplus b_n tot=b1b2bn1bn
最后 t o t ⊕ a i tot \oplus a_i totai 就是 b i b_i bi 的值

#include
#include
using namespace std;
const int Maxn=300000+20;
int n,tot;
int a[Maxn],b[Maxn];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;++i)
	{
		scanf("%d",&a[i]);
		tot^=a[i];
	}
	for(int i=1;i<=n;++i)
	{
		b[i]=tot^a[i];
		printf("%d ",b[i]);
	}
	putchar('\n');
	return 0;
}

你可能感兴趣的:(题解,AtCoder)