D. Ehab the Xorcist

链接: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;
				}
			}
		}
	}
}

 

全部评论

相关推荐

07-30 13:44
门头沟学院 Java
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务