Codeforces Round #628D

D. Ehab the Xorcist

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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

Input

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

Output

If there's no array that satisfies the condition, print "-1". Otherwise:

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

Examples

input

Copy

2 4

output

Copy

2
3 1

input

Copy

1 3

output

Copy

3
1 1 1

input

Copy

8 5

output

Copy

-1

input

Copy

0 0

output

Copy

0

Note

In the first sample, 3⊕1=23⊕1=2 and 3+1=43+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=a$\oplus$b+2*a&b;

这个式子可以这样理解,a$\oplus$b 只是计算出每一位不考虑进位时候的情况,可以+还是进位的,进位的时候肯定是a,b 当前位都是1,才会往前进位。而a&b 第i为1,说明第i位该往前进位,而乘2则是整体左移,就是往前进位。然后在加上刚才没考虑进位的情况。
这个题目不存在解的情况,就是u>v 的情况和u,v奇偶性不同的情况。前者是因为异或是不进位加法,所以肯定不会大于加法的值;后者是因为,如果某一位u,v不一致,则肯定是进位造成的,而最后一位肯定没有进位,所以要为1都为1,要为0,都为0。
对于u==v 的情况直接输出u 就行。

(这个大佬的思路太通俗易懂了,,很感谢)。。。

对于一般情况,
a⊕b=u

a+b=a⊕b+2⋅a&b =u+2.a&b==v

因为u\oplus x \oplus x=u
所以
我们令x=(v-u)/2 就行
答案就是[u,x,x] 
第一个样例长度为2
是因为u⊕x=u+x 
所以是[u+x,x]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
     ios::sync_with_stdio(false);
     ll u,v;
     cin>>u>>v;
     if(u==v&&u==0)
     {
         puts("0");
         return 0;
     }
     else if(u==v)
     {
         cout<<1<<" "<<u<<endl;
         return 0;
     }
     if(u>v||((u&1)!=(v&1)))
     {
         puts("-1");
         return 0;
     }
     ll x=(v-u)/2;
     if((u^x)==(u+x))
     {
         cout<<2<<endl<<x+u<<" "<<x<<endl;
     }
     else
     {
         cout<<3<<endl<<u<<" "<<x<<" "<<x<<endl;
     }
     return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值