Codeforces 1325D. Ehab the Xorcist(思维/构造)

Description

Given 2 integers u and v, find the shortest array such that bitwise-xor of its elements is u, and the sum of its elements is v.

Input

The only line contains 2 integers u and v (0≤u,v≤1018).

Output

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.

Examples
input

2 4

output

2
3 1

input

1 3

output

3
1 1 1

input

8 5

output

-1

input

0 0

扫描二维码关注公众号,回复: 11272056 查看本文章
output

0

Note

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

Notice that in the fourth sample the array is empty.

题目大意

给出两个整数u和v,求一个最短的数组,其所有元素异或得u,所有元素累加得v

思路

对于这个数组可以构造成a b b这种形式a=u,a+b+b=v
b=(v-u)/2
我们能够得到两个性质
如果v和u的奇偶不同不能构造(a和a+b+b的奇偶性相同)
如果v小于u不能构造(b为正整数)

有两个特殊情况需要特判
如果ub0
输出0
如果u==b!=0
输出1 \n u

最后由于需要这个数组最短,如果a和b的每一位上的数都不同为1那就可以将a和一个b加起来

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
int main(){
    ll u,v;
	scanf("%lld %lld",&u,&v);
	if(u>v||(u&1)!=(v&1)){
		printf("-1\n");
		return 0;
	}
	if(u==v){
		if(u==0)printf("0\n");
		else printf("1\n%lld",u);
		return 0;
	}
	v=(v-u)/2;
	if((v^u)==u+v)printf("2\n%lld %lld\n",u+v,v);
	else printf("3\n%lld %lld %lld\n",u,v,v);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43984169/article/details/105607176