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 }

 

转载于:https://www.cnblogs.com/baocong/p/7560497.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值