码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces - 1325D - Ehab the Xorcist(构造)

时间:2020-03-25 23:21:31      阅读:77      评论:0      收藏:0      [点我收藏+]

标签:iostream   queue   eof   lis   force   The   shu   异或   max   

题目链接
题目大意:给你俩数\(u\)\(v\),问你是否能找出来最短的一列数,使它们的和为\(v\),异或值为\(u\)
??这题主要是考察对位运算的理解。
??1.首先,位运算是不存在进位的,所以位运算的结果只会比原来的和小,也就是说\(u\)绝对不会大于\(v\)
??2.如果异或后的结果是奇数的话,那么这列数里面奇数的个数肯定是奇数,同时,如果和是奇数的话,这列数里面奇数的个数也应该是奇数。偶数同理。也就是说,只有\(u\)\(v\)的奇偶性相同,才有解。
??3.那么我们现在来想一下如何来构造这个解。显然如果\(u\)\(v\)相等解就是它本身。如果\(u<v\),因为\(u\)\(v\)两者奇偶性相同,所以\(u-v\)必定为偶数进而可以拆成两个相同的数\(k\)。又因为两个相同的数异
或结果为\(0\)\(0\ xor\ u = u\),所以我们这组解是可行的,但是未必是最优解,因为可能有的解只有两个数。什么时候解的个数是两个呢?在\(u<v\)的情况下,要么是某个二进制位比\(u\)少若干个\(1\)的数和另
一个数异或得到了\(u\),要么是某个二进制位比\(u\)多若干个\(1\)的数和另一个数异或得到了\(u\)。对于第一种情况,两个数之和必定等于\(u\),而\(u<v\),显然与题意是矛盾的。对于第二种情况,我们用前面的结果就
能构造。如果\(u\ xor\ k == u+k\)的话,那么答案就是\(u+k\)\(k\)

//https://www.cnblogs.com/shuitiangong/
#include<set>
#include<map>
#include<list>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define endl ‘\n‘
#define rtl rt<<1
#define rtr rt<<1|1
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define zero(a) memset(a, 0, sizeof(a))
#define INF(a) memset(a, 0x3f, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("==================================================\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> P2;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD =  998244353;
const int INF = 0x3f3f3f3f;
template<typename T> void read(T &x){
    x = 0;char ch = getchar();ll f = 1;
    while(!isdigit(ch)){if(ch == ‘-‘)f*=-1;ch=getchar();}
    while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
const int maxn = 2e5+10;
int main(void) {
    ll u, v;
    while(cin >> u >> v) {
        if (u>v || (u&1)!=(v&1)) cout << -1 << endl;
        else if (u==v) {
            if (!u) cout << 0 << endl;
            else cout << 1 << endl << u << endl;
        }
        else {
            ll t = (v-u)/2;
            if ((u^t) == u+t) cout << 2 << endl << (u^t) << ‘ ‘ << t << endl;
            else cout << 3 << endl << u << ‘ ‘ << t << ‘ ‘ << t << endl;
        }
    }
    return 0;
}

Codeforces - 1325D - Ehab the Xorcist(构造)

标签:iostream   queue   eof   lis   force   The   shu   异或   max   

原文地址:https://www.cnblogs.com/shuitiangong/p/12571234.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!