异或——Red Scarf

这是一道编程题,描述了有一群Snuke猫,每只猫脖子上的红围巾上写着它们喜欢的非负整数。已知除了自己以外的所有猫的数字异或值,目标是恢复所有猫围巾上的原始数字。通过异或运算的性质,由于猫的数量是偶数,可以推导出所有数字异或后等于一个常数X,然后逐个计算出每个猫的原始数字。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Red Scarf

P r o b l e m S t a t e m e n t \color{blue}Problem Statement ProblemStatement
There are N N N Snuke Cats numbered 1 , 2 , … , N 1,2,…,N 1,2,,N, where N N N is even.

Each Snuke Cat wears a red scarf, on which his favorite non-negative integer is written.

Recently, they learned the operation called xor (exclusive OR).

We know that the xor calculated by Snuke Cat i, that is, the xor of the integers written on the scarfs except the scarf of Snuke Cat i is ai
. Using this information, restore the integer written on the scarf of each Snuke Cat.

C o n s t r a i n t s \color{blue}Constraints Constraints
All values in input are integers. 2 ≤ N ≤ 200000 2≤N≤200000 2N200000, N N N is even. 0 ≤ a i ≤ 1 0 9 0≤a_i≤10^9 0ai109
There exists a combination of integers on the scarfs that is consistent with the given information.

I n p u t \color{blue}Input Input
Input is given from Standard Input in the following format:

N
a1 
a2 
… 
aN

O u t p u t \color{blue}Output Output
Print a line containing N N N integers separated with space.
The i-th of the integers from the left should represent the integer written on the scarf of Snuke Cat i.
If there are multiple possible solutions, you may print any of them.

根据题目意思和样例解释,我们可以得到以下式子:
a i a_i ai为题目给出的, b i b_i bi为答案
a 1 = b 2 ⊕ b 3 ⊕ b 4 ⊕ . . . b n a_1=b_2 \oplus b_3 \oplus b_4 \oplus ...b_n a1=b2b3b4...bn
a 2 = b 1 ⊕ b 3 ⊕ b 4 ⊕ . . . b n a_2=b_1 \oplus b_3 \oplus b_4 \oplus ...b_n a2=b1b3b4...bn
. . . ... ...
a n = b 1 ⊕ b 2 ⊕ b 3 ⊕ . . . b n − 1 a_n=b_1 \oplus b_2 \oplus b_3 \oplus ... b_{n-1} an=b1b2b3...bn1
因为 n n n是个偶数,所以根据上面的式子,易得:
a 1 ⊕ a 2 ⊕ a 3 . . ⊕ a n = b 1 ⊕ b 2 ⊕ b 3 . . . . ⊕ b n a_1 \oplus a_2 \oplus a_3..\oplus a_n = b_1 \oplus b_2 \oplus b_3 ....\oplus b_n a1a2a3..an=b1b2b3....bn
我们设它等于 X X X
因为 a 1 = b 2 ⊕ b 3 ⊕ b 4 ⊕ . . . b n a_1=b_2 \oplus b_3 \oplus b_4 \oplus ...b_n a1=b2b3b4...bn,再根据 a ⊕ a = 0 a \oplus a=0 aa=0 0 ⊕ a = a 0 \oplus a=a 0a=a,所以可以得到 a 1 = X ⊕ b 1 a_1=X \oplus b_1 a1=Xb1,也就是 b 1 = a 1 ⊕ X b_1=a_1 \oplus X b1=a1X
同理可得, b i = X ⊕ a i b_i=X \oplus a_i bi=Xai

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 2e5 + 10;
const int INF = 0x3f3f3f3f;
const double eps = 1e-7;
#define endl '\n'
#define pb(a) push_back(a)
#define ALL(x) x.begin(), x.end()
#define SIZE(x) int(x.size())
#define IOS ios::sync_with_stdio(0);

int n;
int a[N],b[N];
int main() {
	scanf("%d",&n);
	int x=0;
	for(int i=1;i<=n;++i) scanf("%d",&a[i]),x^=a[i];
	for(int i=1;i<=n;++i) {
		b[i] = x^a[i];
	}
	for(int i=1;i<=n;++i) printf("%d ",b[i]);
	return 0;
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值