欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-06-04 08:21:03
...

题目:
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.

样例:
Codeforces Round #628 (Div. 2)------Ehab the Xorcist
题意:
给定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;
}