题意:给你n,x,均不超过10^5,让你构造一个无重复元素的n个元素的非负整数集合(每个元素不超过10^6),使得它们的Xor和恰好为x。

如果x不为0:

  随便在x里面找一个非零位,然后固定该位为0,其他位随意填写,恰好弄出n-1个数来,然后对这n-1个数求xor和,记作sum,再输出x xor sum即可。由于只有最后一个数的该位为1,所以必然可以保证不重。

如果x为0:

  如果n不能被4整除,那么输出0 ~ n-2,然后记这些数的异或和为sum,再输出(1<<18)|sum,以及1<<18即可。

  如果n能被4整除,那么输出1 ~ n-1,然后记这些数的异或和为sum,再输出(1<<18)|sum,以及1<<18即可。

  因为记f(x)=0 xor 1 xor ... xor x,则当且仅当x = 3,7,11,15,...时,f(x)=0。

无解当且仅当n=2,且x=0。

#include<cstdio>
using namespace std;
int n,x;
int main(){
scanf("%d%d",&n,&x);
if(x!=0){
puts("YES");
int I;
for(int i=0;i<=30;++i){
if((x>>i)&1){
I=i;
break;
}
}
int sum=0;
for(int i=0;i<=n-2;++i){
int now=i;
int t=(now^((now>>I)<<I))|((now>>I)<<(I+1));
printf("%d ",t);
sum^=t;
}
printf("%d\n",x^sum);
}
else{
if(n==1){
puts("YES");
puts("0");
return 0;
}
if(n==2){
puts("NO");
return 0;
}
if(n==3){
puts("YES");
puts("1 2 3");
return 0;
}
if((n-2)%4==0){
puts("YES");
int sum=0;
for(int i=1;i<=n-2;++i){
printf("%d ",i);
sum^=i;
}
printf("%d %d\n",(sum|(1<<18)),1<<18);
}
else{
puts("YES");
int sum=0;
for(int i=0;i<n-2;++i){
printf("%d ",i);
sum^=i;
}
printf("%d %d\n",(sum|(1<<18)),1<<18);
}
}
return 0;
}

【构造】【分类讨论】Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor的更多相关文章

  1. 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string

    题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数).最后要你找到任意一个0的位置 ...

  2. Codeforces Round #435 (Div. 2)【A、B、C、D】

    //在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...

  3. 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root

    题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...

  4. 【Codeforces Round #435 (Div. 2) A B C D】

    CF比赛题目地址:http://codeforces.com/contest/862 A. Mahmoud and Ehab and the MEX ·英文题,述大意:      输入n,x(n,x& ...

  5. Codeforces Round #435 (Div. 2)

    A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...

  6. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary 并查集

    D. Mahmoud and a Dictionary 题目连接: http://codeforces.com/contest/766/problem/D Description Mahmoud wa ...

  7. Codeforces Round #396 (Div. 2) D. Mahmoud and a Dictionary

    地址:http://codeforces.com/contest/766/problem/D 题目: D. Mahmoud and a Dictionary time limit per test 4 ...

  8. Codeforces Round #396 (Div. 2) E. Mahmoud and a xor trip dfs 按位考虑

    E. Mahmoud and a xor trip 题目连接: http://codeforces.com/contest/766/problem/E Description Mahmoud and ...

  9. Codeforces Round #396 (Div. 2) A. Mahmoud and Longest Uncommon Subsequence 水题

    A. Mahmoud and Longest Uncommon Subsequence 题目连接: http://codeforces.com/contest/766/problem/A Descri ...

随机推荐

  1. 安装magento配置完数据库后出现: PHP Extensions "0" must be loaded. 解决方案

    打开magento目录下的该文件: app/code/core/Mage/Install/etc/config.xml 将下面该段修改: <mysql4> <type>pdo_ ...

  2. CCC2018游记

    day (-1) 晚上睡觉没盖被子 day 0  2018年2月13日 下午放学回来感到一阵头痛,一量体温结果发烧了,感觉很蓝瘦,居然在CCC前一天生病. 本来注册了账号想打practise的,结果又 ...

  3. java 深度拷贝 复制 深度复制

    1.深度拷贝.复制代码实现 最近需要用到比较两个对象属性的变化,其中一个是oldObj,另外一个是newObj,oldObj是newObj的前一个状态,所以需要在newObj的某个状态时,复制一个一样 ...

  4. css3动画总结

  5. Python ctypes的byref和pointer有啥区别

    byref(n)返回的相当于C的指针右值&n,本身没有被分配空间: >>> from ctypes import *>>> n = c_int(0)> ...

  6. nvidia tk1使用记录--基本环境搭建

    前言 项目最开始是在X86+Nvidia(ubuntu+opencv+cuda)平台上实现,达到了期望性能,最近考虑将其移植到嵌入式平台,特别是最近nvidia出了tegra X1,基于和我们使用的g ...

  7. 2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6150 Vertex Cover 二分图,构造

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6150 题意:"最小点覆盖集"是个NP完全问题 有一个近似算法是说—每次选取度数最大 ...

  8. PHP常用函数总结(180多个)

    PHP常用函数总结 数学函数 1.abs(): 求绝对值 $abs = abs(-4.2); //4.2 数字绝对值数字 2.ceil(): 进一法取整 echo ceil(9.999); // 10 ...

  9. JVM 类加载过程、初始化、主动引用、被动引用、静态初始化块执行顺序

  10. Xshell 找到上次执行的命令

    ctrl + p   返回上一次输入命令字符 ctrl + r       输入单词搜索历史命令