Ehab the Xorcist (异或性质,构造)

本文解析了一道关于构造数组题目,目标是找到长度最短且元素异或和等于给定整数uuu,同时数组元素之和等于vvv的数组。关键在于利用异或性质和奇偶性,通过实验和推导,给出了解决策略。

Ehab the Xorcist (异或性质,构造)

题面翻译

给定整数 u u u v ( 0 ≤ u , v ≤ 1 0 18 ) v(0\le u,v \le 10^{18}) v(0u,v1018),试构造长度最短的数组,使得数组内所有元素的异或和为 u u u,加和为 v v v

如果有解,输出两行,第一行输出一个整数 n n n,第二行输出 n n n 个非负整数,表示数组里的元素。多解输出任意一组即可。如果无解,输出一行一个整数 − 1 -1 1

输出格式

If there’s no array that satisfies the condition, print “-1”. Otherwise:

The first line should contain one integer, $ n $ , representing the length of the desired array. The next line should contain $ n $ positive integers, the array itself. If there are multiple possible answers, print any.

样例 #1

样例输入 #1

2 4

样例输出 #1

2
3 1

样例 #2

样例输入 #2

1 3

样例输出 #2

3
1 1 1

样例 #3

样例输入 #3

8 5

样例输出 #3

-1

样例 #4

样例输入 #4

0 0

样例输出 #4

0

提示

In the first sample, $ 3\oplus 1 = 2 $ and $ 3 + 1 = 4 $ . There is no valid array of smaller length.

Notice that in the fourth sample the array is empty.


注意到一些性质:

  1. a + b ≥ a ⨁ b a+b \ge a \bigoplus b a+bab
  2. 对于同一组数,对他们求和 与 对他们求异或和,奇偶性不变。
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值