CF76D Plus and xor

CF76D Plus and xor

洛谷传送门

题目描述

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=109_{10}=1101101_{2}X=10910=11011012 , Y=41_{10}=101001_{2}Y=4110=1010012 , then:

XX xor Y = 68_{10} = 1000100_{2}Y = 6810 = 10001002 . Write a program, which takes two non-negative integers AA and BB as an input and finds two non-negative integers XX and YY , which satisfy the following conditions:

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

输入格式

The first line contains integer number AA and the second line contains integer number BB ( 0<=A,B<=2^{64}-10<=A,B<=264−1 ).

输出格式

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

题意翻译

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

1.A=X+Y

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

且要求 X 是所有满足前两个条件中最小的

如果无解,输出-1

否则,输出 X,Y


题解:

挺有含金量的?我觉得可以评绿。

但是其实推推性质也没啥。

可以看出,^就是不进位加法。

那么它和加法之间会有很多地方相像。具体地,按位来讲,如果两个都是0或者一1一0,加法和异或都是一样的。如果两个都是1的话,那么就会出现加法进位异或变0的情况。

如果这一位为第p位,那么加法就比异或多了\(2^{p+1}\)这么多的数。

所以要求最小的x,其实只需要满足这些都是1的位都是1,其他都是0即可。

其解即为\((A-B)/2\)

注意特判无解情况和开unsigned longlong

代码:

#include<cstdio>
#define ull unsigned long long
using namespace std;
ull a,b;
ull ansx,ansy;
int main()
{
	scanf("%llu%llu",&a,&b);
	if(a<b)
	{
		puts("-1");
		return 0;
	}
	if((a-b)&1)
	{
		puts("-1");
		return 0;
	}
	ansx=(a-b)>>1ull;
	ansy=a-ansx;
	printf("%llu %llu\n",ansx,ansy);
	return 0;
}
posted @ 2020-11-10 17:12  Seaway-Fu  阅读(139)  评论(0编辑  收藏  举报