codeforces 862 C. Mahmoud and Ehab and the xor(构造)

题目链接:http://codeforces.com/contest/862/problem/C

题解:一道简单的构造题,一般构造题差不多都考自己脑补,脑洞一开就过了

由于数据x只有1e5,但是要求是1e6,而且我们知道3个数可以组合成任意数也就是说n-3的数从1~1e5直接任意找然后使得其总xor为sum

当sum=x时(定义Max=1<<17 > 1e5)那么这三个数只要组成0就行,当sum!=x时那么这三个数只要构成sum^x就行,这3个数从1e5~1e6中找

很好找的。

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;

int main() {
    int n , x;
    cin >> n >> x;
    if(n == 1) {
        cout << "YES" << endl;
        cout << x << endl;
    }
    else if(n == 2) {
        if(x == 0) {
            cout << "NO" << endl;
        }
        else {
            cout << "YES" << endl;
            cout << 0 << ' ' << x << endl;
        }
    }
    else {
        int Max = 1 << 17;
        int sum = 0;
        cout << "YES" << endl;
        for(int i = 1 ; i <= n - 3 ; i++) {
            cout << i << ' ';
            sum ^= i;
        }
        if(sum == x) {
            cout << Max << ' ' << (Max << 1) << ' ' << (Max ^ (Max << 1)) << endl;
        }
        else {
            cout << 0 << ' ' << (Max ^ sum) << ' ' << (Max ^ x) << endl;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/TnT2333333/p/7561798.html