1325D - Ehab the Xorcist - CodeForces Solution


bitmasks constructive algorithms greedy number theory *1700

Please click on ads to support us..

Python Code:

u,v = map(int,input().split())
if(u > v): print(-1)
elif (v-u)%2 == 1: print(-1)
else:
	x = (v-u)//2
	if(v == 0 and u == 0):
		print(0)
	elif(v == u):
		print(1)
		print(u)
	elif(x^u) == x+u:
		print(2)
		print(x+u,x)
	else:
		print(3)
		print(x,x,u)

C++ Code:

#include<bits/stdc++.h>
#define MOD 1000000007
#define MAX 200000
using namespace std;
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	long long xr, sm;
	cin>>xr>>sm;
	if(xr>sm)
		cout<<"-1"<<endl;
	else if(xr==sm)	
	{
		if(sm)
			cout<<"1"<<endl<<xr<<endl;
		else cout<<"0"<<endl;
	}
	else if((sm-xr)%2==0)
	{
		//sm = a+b
		//xr = a^b
		//sm=xr+2(a&b) -> (sm-xr)/2 = a&b	
		long long ab = (sm-xr)/2,a=0,b=0;
		for(int i=0;i<62;++i)
		{
			if(ab&((1LL)<<i))
			{
				a |= ((1LL)<<i);
				b |= ((1LL)<<i);
			}
			if(xr&((1LL)<<i))
				a |= ((1LL)<<i);
		}
		if((a^b)==xr&&(a+b)==sm)
		{
			cout<<"2"<<endl;
			cout<<a<<" "<<b<<endl;
		}
		else
		{
			cout<<"3"<<endl;
			cout<<xr<<" "<<ab<<" "<<ab<<endl;		
		}
	}
	else
	{
		cout<<"-1"<<endl;
	}
	return 0;
}


Comments

Submit
0 Comments
More Questions

1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number
228A - Is your horseshoe on the other hoof
122A - Lucky Division
1611C - Polycarp Recovers the Permutation
432A - Choosing Teams
758A - Holiday Of Equality
1650C - Weight of the System of Nested Segments
1097A - Gennady and a Card Game
248A - Cupboards
1641A - Great Sequence
1537A - Arithmetic Array
1370A - Maximum GCD
149A - Business trip
34A - Reconnaissance 2
59A - Word
462B - Appleman and Card Game
1560C - Infinity Table
1605C - Dominant Character
1399A - Remove Smallest
208A - Dubstep
1581A - CQXYM Count Permutations
337A - Puzzles
495A - Digital Counter
796A - Buying A House