D. Ehab the Xorcist

本文探讨了一个算法问题,即寻找满足特定位运算和总和条件的最短整数序列。输入为两个整数u和v,目标是找到一个序列,其元素的按位异或等于u,元素总和等于v。文章通过分析输入数据的二进制表示,提出了一种有效的解决方案,并附带了完整的C++实现代码。

链接:https://codeforces.ml/contest/1325/problem/D

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.

代码:

#include<bits/stdc++.h>
using namespace std;
long long s,n,h,k,l,r,x,u,v,max1=0;
map<long long,long long>m,q;
long long a,b,c;
int main()
{
	cin>>u>>v;
	if(u==0&&v==0)
	{
		cout<<0;
		return 0;
	}
	int i=0;
	k=u;
	while(k)
	{
		if(k%2==1)
		m[i]=1;
		else
		m[i]=0;
		i++;
		k/=2;
	}
	a=0;
	for(int j=0;j<=i;j++)
	{
		if(m[j]==1)
		{
			n=pow(2,j);
			a+=n;
			v-=n;
		}
	}
	if(v<0)
	cout<<-1;
	else
	{
		if(v==0)
		{
			cout<<1<<endl;
			cout<<a;
		}
		else
		{
			k=v;
			int p=0;
			while(k)
			{
				if(k%2==1)
				q[p]=1;
				else
				q[p]=0;
				p++;
				k/=2;
			}
			b=0;
			c=0;
			for(int j=1;j<=p;j++)
			{
				if(q[j]==1&&m[j-1]==0)
				{
					n=pow(2,j-1);
					b+=n;
					a+=n;
					v-=n*2;
				}
				else if(q[j]==1)
				{
					n=pow(2,j-1);
					b+=n;
					c+=n;
					v-=n*2;
				}
			}
			if(q[0]==1)
			cout<<-1;
			else
			{
				if(c!=0)
				{
					cout<<3<<endl;
					cout<<a<<" "<<b<<" "<<c;
				}
				else
				{
					cout<<2<<endl;
					cout<<a<<" "<<b;
				}
			}
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值