Ehab the Xorcist (异或性质,构造)
题面翻译
给定整数 u u u 和 v ( 0 ≤ u , v ≤ 1 0 18 ) v(0\le u,v \le 10^{18}) v(0≤u,v≤1018),试构造长度最短的数组,使得数组内所有元素的异或和为 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.
注意到一些性质:
- a + b ≥ a ⨁ b a+b \ge a \bigoplus b a+b≥a⨁b
- 对于同一组数,对他们求和 与 对他们求异或和,奇偶性不变。

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





