Codeforces Round #628 (Div. 2)------Ehab the Xorcist

题目:
Given 2 integers u and v, find the shortest array such that bitwise-xor of its elements is u, and the sum of its elements is v.

Input
The only line contains 2 integers u and v (0≤u,v≤1018).

Output
If there’s no array that satisfies the condition, print “-1”. Otherwise:
The first line should contain one integer, n, representing the length of the desired array. The next line should contain n positive integers, the array itself. If there are multiple possible answers, print any.

样例:
在这里插入图片描述
题意:
给定2个整数u和v,找到一个最短的数组,使得其元素的按位异或为u,并且其元素之和为v。

思路
根据异或的性质有,不存在这样的数组的情况:u > v时, u 和 v奇偶性不相同时.对于其他情况只有2和3,然后根据异或性质计算即可。

愉快AC

#include<iostream>
using namespace std;
#define ll long long
int main()
{
    ll u, v; cin >> u >> v;
    if(u > v || (u & 1) != (v & 1)){
        cout << -1 << endl;
        return 0;
    }
    //样例:
    if(u == 0 && v == 0){
        cout << 0 << endl;
        return 0;
    }
    if(u == v){
        cout << 1 << endl << u << endl;
        return 0;
    }
    ll x = u, y = (v - u) / 2;
    if((x ^ y) == (x + y)){
        cout << 2 << endl << x + y << " " << y << endl;
    }
    else
        cout << 3 << endl << x << " " << y << " " << y << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值