D. Ehab the Xorcist

题目链接:https://codeforces.com/contest/1325/problem/D

想法:

#pragma GCC optimize(3,"Ofast","inline")//O3优化
#pragma GCC optimize(2)//O2优化
#include <algorithm>
#include <string>
#include <string.h>
#include <vector>
#include <map>
#include <stack>
#include <set>
#include <queue>
#include <math.h>
#include <cstdio>
#include <iomanip>
#include <time.h>
#include <bitset>
#include <cmath>
#include <sstream>
#include <iostream>
#include <cstring>

#define LL long long
#define ls nod<<1
#define rs (nod<<1)+1
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define INF 0x3f3f3f3f
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)

const double eps = 1e-10;
const int maxn = 2e5 + 10;
const int mod = 1e9 + 7;

int sgn(double a){return a < -eps ? -1 : a < eps ? 0 : 1;}
using namespace std;

LL dat[maxn];
LL ans[maxn];

int main() {
    ios::sync_with_stdio(0);
    LL u,v;
    cin >> v >> u;
    dat[0] = 1;
    for (int i = 1;i <= 64;i++)
        dat[i] = dat[i-1] * 2;
    if (v > u) {    // u 肯定要大于 v
        cout << -1 << endl;
        return 0;
    }
    u -= v;
    LL t = u;
    int cnt1 = 0,cnt2 = 0;
    while (t != 0) {
        cnt1++;
        t >>= 1;
    }
    while (v != 0) {   // 先处理出 v 
        ans[cnt2++] = v & 1;
        v >>= 1;
    }
    if (cnt1 < cnt2)  // 确保是位数
        cnt1 = cnt2;
    LL cnt = cnt1;
    while (cnt >= 0) {
        if (2ll * dat[cnt] <= u) {
            ans[cnt] += 2;
            u -= 2ll * dat[cnt];
        }
        cnt--;
    }
    if (u) {
        cout << -1 << endl;
        return 0;
    }
    LL maxx = 0;
    for (int i = 0;i <= cnt1;i++)
        maxx = max(maxx,ans[i]);
    cout << maxx << endl;
    for (int i = 1;i <= maxx;i++) {
        LL now = 0;
        for (int j = 0;j <= cnt1;j++) {
            if (ans[j]) {
                ans[j] -= 1;
                now += dat[j];
            }
        }
        cout << now << " ";
    }
    return 0;
}
原文地址:https://www.cnblogs.com/-Ackerman/p/12585044.html