CodeForces 76 D.Plus and xor(位运算)

Description

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

1. A = X + Y

2. B = X   x o r   Y ,其中 x o r 表示异或运算

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

Input

两个整数 A , B ( 0 A , B 2 64 1 )

Output

有解则输出 X , Y ,否则输出 1

Sample Input

142
76

Sample Output

33 109

Solution

异或即为不进位加法,那么 A B 即表示 X + Y 的进位状态,既然是进位那么 A B 必然为偶数且 X , Y 均应包含 A B 2 ,故若 A < B A B 为奇数则无解,否则 X = A B 2 , Y = A + B 2 ,注意 A , B u n s i g n e d   l o n g   l o n g 且求 Y 的时候不能用加法,而是 Y = A X

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef unsigned long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
int main()
{
    ll A,B;
    cin>>A>>B;
    if(A<B||((A-B)&1))printf("-1\n");
    else cout<<(A-B)/2<<" "<<A-(A-B)/2<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/v5zsq/article/details/81051471
76