zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    函数式宏定义与普通函数
    linux之sort用法
    HDU 4390 Number Sequence 容斥原理
    HDU 4407 Sum 容斥原理
    HDU 4059 The Boss on Mars 容斥原理
    UVA12653 Buses
    UVA 12651 Triangles
    UVA 10892
    HDU 4292 Food
    HDU 4288 Coder
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/12585044.html
Copyright © 2011-2022 走看看