Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
来源:互联网 发布:qq表情生成软件 编辑:程序博客网 时间:2024/05/19 13:06
Codeforces Round #435 (Div. 2) C. :Mahmoud and Ehab and the xor
Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won't show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.
Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set ofn distinct non-negative integers such the bitwise-xor sum of the integers in it is exactlyx. Dr. Evil doesn't like big numbers, so any number in the set shouldn't be greater than106.
The only line contains two integers n andx (1 ≤ n ≤ 105,0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.
If there is no such set, print "NO" (without quotes).
Otherwise, on the first line print "YES" (without quotes) and on the second line printn distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.
5 5
YES1 2 4 5 7
3 6
YES1 2 5
You can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR
For the first sample .
For the second sample .
题意: 找出n 个不同元素的集合,其异或值为 x;
思路: 考虑异或的特性,暴力肯定不好写,那么我们可以想到
(0^1^2^...^n-2)^(0^1^2^...^n-2)^x = x ;
又 可能出现重复的数字,题中说 x < 1e5 ,那么如果出现了重复的数字 ,可以给两项同时异或一个大于 1e 5 的数。
注意可能是 a(n-1)==a(n-2); 所以要判断一下。
特判一下 n==1 ||n==2&&x==0 的情况。
#include <bits/stdc++.h>using namespace std;int n, x;int a[100005];int main(){ cin>>n>>x; if (n == 1) { cout<<"YES"<<endl<<x<<endl; return 0; } if (n == 2) if (x == 0) { cout <<"NO"<< endl;return 0; } cout <<"YES"<< endl; a[n-1]=x; for(int i=0;i<n-1;i++) { a[i]=i; a[n-1]^=i; } if(a[n-1]<n-1) { if(a[n-1]!=a[n-2]) a[n-2]^= (1<<17); else a[n-3]^= (1<<17); a[n-1]^= (1<<17); } for(int i=0;i<n;i++) printf("%d%c",a[i],i==n-1?'\n':' '); return 0;}
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor(xor运算应用)
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2): C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2)C. Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2)C. Mahmoud and Ehab and the xor(异或的性质)
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor 【位运算+思维】
- Codeforces Round #435 (Div. 2)-Mahmoud and Ehab and the xor(异或)
- Codeforces Round #435 (Div. 2)-异或规律&思维-Mahmoud and Ehab and the xor
- Mahmoud and Ehab and the xor CodeForces
- Mahmoud and Ehab and the xor CodeForces
- Codeforces 862C. Mahmoud and Ehab and the xor
- Codeforces 862C Mahmoud and Ehab and the xor
- Codeforces Round #435 (Div. 2) 之Mahmoud and Ehab and the MEX
- Codeforces Round #435 (Div. 2) A. Mahmoud and Ehab and the MEX(思路)
- Codeforces Round #435 (Div. 2)-B. Mahmoud and Ehab and the bipartiteness
- IT职涯入门首选,计算机网络技术
- IFeatureLayerDefinition接口实现IFeatureLayer图层显示过滤,图层全范围视图的获取
- laytpl--前端数据绑定 (示例)
- 敏感词查询过滤DFA算法
- Horizontal+异步+pullTorefresh+viewPager+侧滑(XListView)
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
- HDU 1010 Tempter of the Bone (DFS)
- 关于CarbonData+Spark SQL的一些应用实践和调优
- Android 通知栏
- 友好城市(动态规划,排序)
- 《小马SpringBoot框架》介绍与安装
- Docker下HBase学习,三部曲之一:极速体验
- 去上海面试Java外包工作的郁闷
- PAT 甲级 1013. Battle Over Cities (25)