【构造】【分类讨论】Codeforces Round #435 (Div. 2) C. Mahmoud and Ehab and the xor
题意:给你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的更多相关文章
- 【二分】Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string
题意:交互题:存在一个至少有一个0和一个1的长度为n的二进制串,你可以进行最多15次询问,每次给出一个长度为n的二进制串,系统返回你此串和原串的海明距离(两串不同的位数).最后要你找到任意一个0的位置 ...
- Codeforces Round #435 (Div. 2)【A、B、C、D】
//在我对着D题发呆的时候,柴神秒掉了D题并说:这个D感觉比C题简单呀!,,我:[哭.jpg](逃 Codeforces Round #435 (Div. 2) codeforces 862 A. M ...
- 构造水题 Codeforces Round #206 (Div. 2) A. Vasya and Digital Root
题目传送门 /* 构造水题:对于0的多个位数的NO,对于位数太大的在后面补0,在9×k的范围内的平均的原则 */ #include <cstdio> #include <algori ...
- 【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& ...
- Codeforces Round #435 (Div. 2)
A. Mahmoud and Ehab and the MEX 题目链接:http://codeforces.com/contest/862/problem/A 题目意思:现在一个数列中有n个数,每个 ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- bzoj 1058 bst
因为是数列的维护,所以我们可以考虑用splay来维护,每次在x插入的时候就在x+1前面插入就行了,然后用bst来维护两问的答案,但是应该会tle.我们来考虑这个问题的性质,首先因为这个数列没有删除操作 ...
- 类图(Class Diagram)
类图(Class Diagram): 类(Class)封装了数据和行为,是面向对象的重要组成部分,它是具有相同属性.操作.关系的对象集合的总称. 类一般由三部分组成: 类名(Class):每个类都必须 ...
- CursorFileManager对cursor文件的读写
public class CursorFileManager implements CursorManager{public void write(String key, LongCursor cur ...
- 新建一个express工程,node app无反应
1.问题描述 新建一个express工程,node app以后无反应,浏览器输入localhost:3000,显示如下 2.解决方法 在app.js文件中加入如下代码 app.listen(3000, ...
- swift 动态获取类, 获取命名空间
在做swift开发中很多时候会动态加载控制器的类, 可以让app更加灵活显示界面信息 一般情况下都是服务器返回显示的控制器类name然后动态显示, 但是服务器返回的类name是string, 怎么转换 ...
- Leetcode 之Longest Palindromic Substring(30)
很经典的一道题,最长回文子串,有多种方法. 首先介绍的一种方法是从中间向两边展开.注意区分aba和abba型的回文串:如果当前最长的子串已经当于两边中最长的子串了,则无需再去判断. //从中间向两边展 ...
- hadoop3.1 ha高可用部署
1.资源角色规划
- redis之(十五)redis的集群中的哨兵角色
一:redis集群的哨兵的目的是什么?. (1)监控主redis和从redis数据库是否正常运行 (2)主redis出现故障,自动将其中一台从redis升级为主redis.将原先的主redis转变成从 ...
- Matlab处理数据导出Paraview可读的vtk文件(二)
由于我在用SPH方法仿真时用的是FORTRAN语言,并且没有找到直接输出vtk文件的代码,因此偷懒通过MATLAB转换一下数据. 用到的Matlab子程序可通过一下链接找到. Matlab处理数据导出 ...
- (翻译)一起使用 .NET 和 Docker——DockerCon 2018 更新
原文:https://blogs.msdn.microsoft.com/dotnet/2018/06/13/using-net-and-docker-together-dockercon-2018-u ...