Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor(xor运算应用)
来源:互联网 发布:oracle数据库日志满了 编辑:程序博客网 时间:2024/05/12 00:18
题目链接:
http://codeforces.com/problemset/problem/862/C
题目大意:
给你两个数N和X,让你判断能否找出N个不同的数字,N个数字的xor值为X。(N的各个值是不相同的)
题目思路:
结合xor运算的规律,相同数xor为0,0 xor任何数都为任意数(0^x=x)、那么结合这个规律就能很简单的得出答案;
例如N=4,X=6。 X=1^2^3^M M=(1^2^3^X) 所以X=1^2^3^(1^2^3^X) 。在这里还要注意M是否和前面的值是否相同。因为1e5是16位(2进制),那么只要M和另外一个数同时xor(1<<17)即可。
学到的东西:
xor运算规律的合理利用。
代码:
#include <bits/stdc++.h>using namespace std;const int MAXN=1e5;int a[MAXN];//答案数组int main(){ fill(a,a+MAXN,0); int n,x; cin>>n>>x; if(n==1){//特殊情况 cout<<"YES"<<endl<<x<<endl; } else if(n==2&&x==0){//特殊情况 cout<<"NO"<<endl; } else{ a[n]=x; for(int i=1;i<=n-1;i++){ a[i]=i; a[n]^=i; } if(a[n]<=n-1){//当最后一个数和某一个数相同的时候 if(a[n]==a[n-1]){ a[n]^=(1<<17); a[n-2]^=(1<<17); } else{ a[n]^=(1<<17); a[n-1]^=(1<<17); } } cout<<"YES"<<endl; for(int i=1;i<=n;i++) cout<<a[i]<<" "; cout<<endl; }}
阅读全文
0 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 862 C. Mahmoud and Ehab and the xor (技巧)
- CF C. Mahmoud and Ehab and the xor
- CF 862C Mahmoud and Ehab and the xor 构造
- 解决自定义Toast(吐司)设置宽高无效的问题
- springcloud记录篇1-eurekaserver
- 数据库---常见约束的SQL语句的研究与使用
- 读书笔记——并行处理器架构
- struct和typedef struct
- Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor(xor运算应用)
- Mac下搭建Vue开发环境
- andorid6.0上如何利用中断管脚去上报2个键值
- MINI框架
- 洛谷 P3317 [SDOI2014]重建(矩阵树定理+数学推导) [bzoj3534]
- postgresql的时间数据处理
- reactive-native极光推送不成功问题总结(android)
- spring 事务不起作用 问题汇总
- 大概是:整数划分||DP||母函数||递推