题意:给你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. $.on方法与$.click()的区别

    1.$.on("click") 支持动态元素绑定事件,该事件是绑定到document上,只要符合条件的元素即可绑定事件,同时$.on()可以绑定多个事件 on方法 on(event ...

  2. linux加载指定目录的so文件

    linux加载指定目录的so文件 http://blog.csdn.net/win_lin/article/details/8286125 download urlhttp://download.ch ...

  3. redis cluster 实现

    Redis cluster是一个redis官方提供的集群功能,集群节点最小3个节点,配置比较多,记录下来,以供下次使用.我在这使用的redis 4.0.6. 因为最新的ruby redis扩展需要ru ...

  4. mybatis源码阅读(动态代理)

    这一篇文章主要是记录Mybatis的动态代理学习成果,如果对源码感兴趣,可以看一下上篇文章  https://www.cnblogs.com/ChoviWu/p/10118051.html 阅读本篇的 ...

  5. 刷题中熟悉Shell命令之Tenth Line和Transpose File [leetcode]

    首先介绍题目中要用的4个Shell命令 sed awk head tail的常用方法.(打好地基,才能建成高楼!) sed:(转自:http://www.cnblogs.com/barrychiao/ ...

  6. leetcode 之Copy List with Random Pointer(23)

    深拷贝一个链表,不同的是这个链表有个额外的随机指针.参考:http://blog.csdn.net/ljiabin/article/details/39054999 做法非常的巧妙,分成三步,一是新建 ...

  7. jQuery实现,动态自动定位弹窗。JS分页,Ajax请求

    工作中碰到一个问题,一个页面中碰到多个地方需要弹窗数据. 网上找了一圈,没有找到合适的,所以自己写了一个. 兼容IE7+,chrome.其它未测试. 需求:点击任意的输入框(也可其它元素,代码中有注释 ...

  8. JVM内存分配与回收

    1.内存分配与回收策略 内存自动管理:自动化的解决了对象内存分配和回收对象内存的问题. 一般在堆上分配对象,也可能经过JTI编译后间接在栈上分配. 主要分配在新生代的Eden区,如果启动了本地线程分配 ...

  9. HDU 2829 Lawrence(斜率优化DP O(n^2))

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2829 题目大意:有一段铁路有n个站,每个站可以往其他站运送粮草,现在要炸掉m条路使得粮草补给最小,粮草 ...

  10. 在写一次epoll

    epoll & select & poll只能处理IO相关的操作,epoll每一个操作必须注册到时间监控机制中,并且还需要进程或者线程进行管理. 多进程/多线程 和epoll相比较 e ...