Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor 构造

链接:

http://codeforces.com/contest/862/problem/C

题意:

构造n个不同的数,使他们的异或和为x

题解:

0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n-3) ^ (n-2) ^ ( 0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n-3) ^ (n-2) ^ x) == x

如果最后一项<n-1,那就说明和前面的重复了,如果a[n-1]!=a[n-2],那就直接让这两项都异或(1<<18),如果相等,那就让a[n-3],a[n-1]都异或上(1<<18)

另外还要特判n=2,x=0的情况,只有这一种是No

代码:

31 int n, x;
32 int a[MAXN];
33 
34 int main() {
35     ios::sync_with_stdio(false), cin.tie(0);
36     cin >> n >> x;
37     if (n == 1) return (cout <<"YES"<<endl<< x << endl), 0;
38     if (n == 2 && x == 0) return (cout << "NO" << endl), 0;
39     a[n - 1] = x;
40     rep(i, 0, n - 1) a[i] = i, a[n - 1] ^= i;
41     if (a[n - 1] < n - 1) {
42         if (a[n - 1] != a[n - 2]) a[n - 2] ^= (1 << 18);
43         else a[n - 3] ^= (1 << 18);
44         a[n - 1] ^= (1 << 18);
45     }
46     cout << "YES" << endl;
47     rep(i, 0, n) cout << a[i] << ' ';
48     cout << endl;
49     return 0;
50 }

 

posted @ 2017-09-20 11:21  Flowersea  阅读(116)  评论(0编辑  收藏  举报