【CodeForces - 76D】Plus and xor(位运算-异或)

版权声明:如果有什么问题,欢迎大家指出,希望能和大家一起讨论、共同进步。 https://blog.csdn.net/QQ_774682/article/details/84435597

Bitwise exclusive OR (or bitwise addition modulo two) is a binary operation which is equivalent to applying logical exclusive OR to every pair of bits located on the same positions in binary notation of operands. In other words, a binary digit of the result is equal to 1 if and only if bits on the respective positions in the operands are different.

For example, if X = 10910 = 11011012, Y = 4110 = 1010012, then:

X xor Y  =  6810  =  10001002.

Write a program, which takes two non-negative integers A and B as an input and finds two non-negative integers X and Y, which satisfy the following conditions:

  • A = X + Y
  • B  =  X xor Y, where xor is bitwise exclusive or.
  • X is the smallest number among all numbers for which the first two conditions are true.

Input

The first line contains integer number A and the second line contains integer number B (0 ≤ A, B ≤ 2^{64} - 1).

Output

The only output line should contain two integer non-negative numbers X and Y. Print the only number -1 if there is no answer.

Examples

Input

142
76

Output

33 109

题意:

给出两整数A,B,要求找到X,Y满足以下三个条件:

1.A=X+Y

2.B=X xor Y2其中xor表示异或运算

3.X是所有满足前两个条件中最小的

思路:

异或是不进位的加法,X和Y做异或运算,那么,他们在同一个 位置的1会变成0,而其他一个1一个0的位置,则会变成1,因此做异或运算会比加法运算少2倍的相同位置的1的值,因此\frac{A-B}{2}的值就是X和Y相同的部分,而X要求最小并且又必须要有相同部分,因此X可以取得最小就可以是,两者的相同部分,其他的都由Y来提供。

再看不成立的条件,首先当A小于B时是不成立的,因为异或是不进位的加法如果两个数没有1在同一个位置上,那么异或的值会和加法值相同,否则会有1变为0,异或的结果会变小。其次A-B是奇数的时候也是不成立的,由上面分析我们可以得出,A-B必定是偶数。

因为这道题的数据到了2^{64}-1超过longlong的取值范围,而在unsigned long long范围内,因此可以用unsigned long long。

longlong取值范围为-2^63到2^63-1,大概为9x10^18这么多。

#include<stdio.h>
#include<string.h>
#include<queue>
#include<set>
#include<iostream>
#include<map>
#include<stack>
#include<cmath>
#include<algorithm>
#define ll long long
#define mod 1000000007
#define eps 1e-8
using namespace std;
int main()
{
	unsigned ll a,b;
	cin>>a>>b;
	unsigned ll tt,y,x;
	tt=a-b;
	if(b>a||tt&1)
	{
		printf("-1\n");
		return 0;
	}
	x=tt>>1;
	y=a-x;
	cout<<x<<" "<<y<<endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/QQ_774682/article/details/84435597