异或= =

文章描述了一个关于异或操作的问题,其中N个戴着标有非负整数红围巾的猫学习了异或操作并计算除了自己围巾上的数以外其他所有数的异或值。给定这些异或值,需要恢复出每只猫围巾上的原始整数。解题的关键在于利用异或一个数偶数次结果为零的性质,通过对初始输入数组进行异或求和,然后逐个与输入数组元素异或来恢复每个元素的值。

简介:

异或,英文为exclusive OR,缩写成xor。

运算规则:相同为1,不同为0,

1 ^ 0 == 1
1 ^ 1 == 0
0 ^ 0 == 0
0 ^ 1 == 0
// 通过观察可知,异或的结果是加法

性质:

a ^ a = 0;
a ^ 0 = a; // 二进制中加0相当于不加

题目:

There are NN Snuke Cats numbered 1,2,…,N1,2,…,N, where NN 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).

What is xor? For example, .

They wanted to use this operation quickly, so each of them calculated the xor of the integers written on their scarfs except his scarf.

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

Constraints

  • All values in input are integers.
  • 2≤N≤2000002≤N≤200000
  • NN is even.
  • 0≤ai≤1090≤ai​≤109
  • There exists a combination of integers on the scarfs that is consistent with the given information.

Input

Input is given from Standard Input in the following format:

Output

Print a line containing NN integers separated with space.

The ii-th of the integers from the left should represent the integer written on the scarf of Snuke Cat ii.

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

Sample 1

InputcopyOutputcopy
4
20 11 9 24
26 5 7 22
  • 5 xor 7 xor 22=205 xor 7 xor 22=20
  • 26 xor 7 xor 22=1126 xor 7 xor 22=11
  • 26 xor 5 xor 22=926 xor 5 xor 22=9
  • 26 xor 5 xor 7=2426 xor 5 xor 7=24

Thus, this output is consistent with the given information.

题意:输入一个n,然后输入n个数字,然后请输出n个数字,让他们的数字XOR的值能组成原来的数组

解题思路:其实这道题利用到了同一个数XOR偶数次等于0,这个原理,根据XOR的原理,位相同为0,不同为1,我们可以将开始输入的n个数XOR起来,然后输出的时候分别再和输入的元素XOR,就能得到新的元素。

AC代码:

#include <bits/stdc++.h>

using namespace std;

const int N = 2e5 + 10;
typedef long long LL;

int a[N];

int main()
{
	int n;
	scanf("%d",&n);
	int sum = 0;
	for(int i = 0 ; i < n ; i ++)
	{
		scanf("%d",&a[i]);
		sum ^= a[i];
	}
	
	for(int i = 0 ; i < n ; i ++)  printf("%d ",sum^a[i]);
	
	return 0;
} 

05-19
### 关于异或操作的使用方法 #### 基本定义 异或XOR)是一种位运算,其规则如下:对于两个二进制数中的每一位,当两者相同时结果为 `0`,不同则为 `1`。具体规则可以描述为: - \( 0 \oplus 0 = 0 \) - \( 0 \oplus 1 = 1 \) - \( 1 \oplus 0 = 1 \) - \( 1 \oplus 1 = 0 \)[^4] #### 应用场景 以下是异或的一些典型应用场景: #### 数据加密与解密 利用异或的 **自反性** 特点,\( a \oplus b \oplus b = a \),可以通过简单的两次异或操作完成数据的加密和解密过程。例如,假设原始数据为 `a`,密钥为 `key`,通过以下方式实现加密和解密: ```java int encryptedData = a ^ key; int decryptedData = encryptedData ^ key; // 结果等于 originalData ``` #### 不借助额外变量交换两个数值 在某些情况下,为了避免引入临时变量,可以使用异或来交换两个整型变量的值。例如: ```c++ void swap(int &a, int &b) { a = a ^ b; b = a ^ b; a = a ^ b; } ``` 需要注意的是,在实际开发中应谨慎使用此技巧,因为它可能降低代码的可读性和维护性。 #### 计算数组元素的唯一值 给定一个数组,其中除了某个元素外其他所有元素均成对出现,则可通过遍历整个数组并对所有元素执行连续异或操作找到唯一的那个单次出现的元素。这是因为任何数与其自身异或的结果均为零 (\( x \oplus x = 0 \)),而任意数与零异或仍为其本身 (\( x \oplus 0 = x \))[^3]。 ```python def find_single(nums): result = 0 for num in nums: result ^= num return result ``` #### 实现基本算术加法 通过组合异或 (^) 和按位与 (&) 的功能可以在不依赖传统加号的情况下模拟加法规则。这里的关键在于分别处理无进位部分以及由当前位产生的进位情况直到不再存在新的进位为止[^2]: ```java public int add(int a, int b){ while(b != 0){ int tempSumWithoutCarry = a ^ b; int carryForNextRound = (a & b) << 1; a = tempSumWithoutCarry; b = carryForNextRound; } return a; } ``` #### 性能优化方面的作用 尽管日常应用层面上较少直接接触底层硬件级别的细节,但在诸如哈希表设计这样的高性能需求场合下仍然可以看到它的身影。比如 Java 中 HashMap 类内部使用的 hash 函数就巧妙运用到了高阶位参与低阶位混合的技术手段以提升分布均匀度从而减少冲突概率[^1]: ```java static final int hash(Object key) { int h; return (key == null) ? 0 : (h = key.hashCode()) ^ (h >>> 16); } ``` --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

21RGHLY

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

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

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

打赏作者

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

抵扣说明:

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

余额充值