题解:AT_abc347_d [ABC347D] Popcount and XOR

· · 题解

解决

题意

您将得到非负整数 abC

确定是否有一对非负整数 (X, Y) 满足以下所有五个条件。如果存在这样的对,则打印一个。

思路

首先要满足 X \oplus Y = C 我们需要在把 C 转化成二进制后的所有位在 XY 中出现仅一次。那么 XY 的其余的位 只需要根据 ab 的大小都选或者都不选即可。

细节

代码

#include<bits/stdc++.h>
#define ll long long
#define int long long
#define int_128 __int128
#define lowbit(x) (x&(-x))
using namespace std;
int a,b,c;
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin>>a>>b>>c;
    int cnt=0;
    int x=c;
    while(x) {
        x-=lowbit(x);
        cnt++;
    }
    if(a+b<cnt || (a+b-cnt)%2==1) {
        cout<<-1;
    }
    else {
        int ans=0,anss=0;
        for(int i=0;i<=60;i++){
            if(c&(1ll<<i)){
                if(a>=b){
                    ans|=(1ll<<i);
                    a--;
                }
                else {
                    anss|=(1ll<<i);
                    b--;
                }
            }
        }
        for(int i=0;i<=60;i++){
            if((c&(1ll<<i)) == 0){
                if(a&&b){
                    a--;
                    b--;
                    ans|=(1ll<<i);
                    anss|=(1ll<<i);
                }
                else {
                    break;
                }
            }
        }
        if(a||b){
            cout<<-1;
        }
        else cout<<ans<<" "<<anss<<"\n";
    }
    return 0;
}