codeforces 76 D. Plus and xor相加或异或

本文探讨了一道计算机科学中的经典问题:已知两个非负整数A和B,如何找到最小的非负整数X及对应的Y,使得A等于X与Y之和且B等于X与Y的按位异或结果。文中提供了一个简洁的C++程序实现。

D. Plus and xor

time limit per test0.5 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ≤ 264 - 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

这里主要是异或的问题,x^y=B,不进位加法,可以解决到x=(A-B)/2,但是因为题目要整数。假如得到值非整数,就是无解所以之后需要判断,是否是整数=》
if(2*x+B==A)
{
cout<< x<<” “<< y;
}

#include <iostream>
#include <cstdio>
using namespace std;
int main()
{
    long long A,B;
    long long x,y;
    cin>>A;
    cin>>B;
    x=(A-B)/2;
    y=A-x;
    if(2*x+B==A)
    {
        cout<<x<<" "<<y;
    }
    else
    {
        cout<<"-1";
    }
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值