CodeForces - 862C Mahmoud and Ehab and the xor(构造)
题意:要求构造一个n个数的序列,要求n个数互不相同,且异或结果为x。
分析:
1、因为0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ (0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x) = x,
构造的n个数可以为0,1,2,3,...,(n - 3),(n - 2),(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)。
2、因为(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)可能与0,1,2,3,...,(n - 3),(n - 2)中某个数重复,因此可以通过将某两个数| (1 << 17)来避免重复。
这两个数,一个可以选择(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x),另一个可以选择与(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)不同的某个数。
3、如果(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)与n-2相同,则n-3一定与(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)不同,因此将n-3和(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)同时| (1 << 17)来避免重复。
如果(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)与n-2不同,则将n-2和(0 ^ 1 ^ 2 ^ 3 ^ ... ^ (n - 3) ^ (n - 2) ^ x)同时| (1 << 17)来避免重复。
4、因为n最大为105,二进制为11000011010100000,共17位,所以将某两个数| (1 << 17),最高位可以相互抵消,又可以使n个数互不相同。
因为106是20位,所以将某两个数| (1 << 17)后是18位,不会超过106,符合题意。
5、特判一下n=1以及n=2&&x=0即可。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
vector<int> ans;
int main(){
int n, x;
scanf("%d%d", &n, &x);
if(n == 1){
printf("YES\n");
printf("%d\n", x);
return 0;
}
if(n == 2){
if(x == 0){
printf("NO\n");
}
else{
printf("YES\n");
printf("0 %d\n", x);
}
return 0;
}
int tmp = 0;
for(int i = 0; i <= n - 2; ++i){
tmp ^= i;
}
tmp ^= x;
if(tmp == n - 2){
for(int i = 0; i <= n - 4; ++i){
ans.push_back(i);
}
ans.push_back((n - 3) | (1 << 17));
ans.push_back(n - 2);
ans.push_back(tmp | (1 << 17));
}
else{
for(int i = 0; i <= n - 3; ++i){
ans.push_back(i);
}
ans.push_back((n - 2) | (1 << 17));
ans.push_back(tmp | (1 << 17));
}
printf("YES\n");
int len = ans.size();
for(int i = 0; i < len; ++i){
if(i) printf(" ");
printf("%d", ans[i]);
}
printf("\n");
return 0;
}
CodeForces - 862C Mahmoud and Ehab and the xor(构造)的更多相关文章
- CodeForces - 862C Mahmoud and Ehab and the xor(构造)【异或】
<题目链接> 题目大意: 给出n.m,现在需要你输出任意n个不相同的数(n,m<1e5),使他们的异或结果为m,如果不存在n个不相同的数异或结果为m,则输出"NO" ...
- Codeforces 862C - Mahmoud and Ehab and the xor
862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了 ...
- 862C - Mahmoud and Ehab and the xor(构造)
原题链接:http://codeforces.com/contest/862/problem/C 题意:给出n,x,求n个不同的数,使这些数的异或和为x 思路:(官方题解)只有n==2&&am ...
- Coderfroces 862 C. Mahmoud and Ehab and the xor
C. Mahmoud and Ehab and the xor Mahmoud and Ehab are on the third stage of their adventures now. As ...
- Codeforces 959D. Mahmoud and Ehab and another array construction task(构造, 简单数论)
Codeforces 959D. Mahmoud and Ehab and another array construction task 题意 构造一个任意两个数都互质的序列,使其字典序大等于a序列 ...
- Codeforces 959F Mahmoud and Ehab and yet another xor task 线性基 (看题解)
Mahmoud and Ehab and yet another xor task 存在的元素的方案数都是一样的, 啊, 我好菜啊. 离线之后用线性基取check存不存在,然后计算答案. #inclu ...
- Codeforces 862A Mahmoud and Ehab and the MEX
传送门:CF-862A A. Mahmoud and Ehab and the MEX time limit per test 2 seconds memory limit per test 256 ...
- Codeforces 862B - Mahmoud and Ehab and the bipartiteness
862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc+ ...
- Codeforces 862D. Mahmoud and Ehab and the binary string (二分)
题目链接:Mahmoud and Ehab and the binary string 题意: 一道交互题,首先给出一个字符串的长度l.现在让你进行提问(最多15次),每次提问提出一个字符串,会返回这 ...
随机推荐
- 自身经历 .NET转Java 的一些分享
原因 楼主在二线城市从事.NET开发8年,当薪资达到15k想往20k跳的时候,发现一个残酷的现实.在招聘信息上给出这个薪资的一共10家,其中:2~3家给出的是假的薪资范围(吸引面试者),2家是总人数不 ...
- ajax的XmlHttpRequest对象常用方法
onreadystatechange用于检测readyState状态的改变,当readyState的状态发生改变的时候调用回调
- state thread api 查询
state thread api 查询: http://state-threads.sourceforge.net/docs/reference.html
- maven热部署
1.启动tomcat 2.修改 tomat/conf/tomcat-users.xml 配置用户名.密码.角色 manager-gui:图形界面的权限(调试时配置) man ...
- IDEA 解决 Maven 依赖冲突的高能神器,这一篇够不够?
1.何为依赖冲突 Maven是个很好用的依赖管理工具,但是再好的东西也不是完美的.Maven的依赖机制会导致Jar包的冲突.举个例子,现在你的项目中,使用了两个Jar包,分别是A和B.现在A需要依 ...
- [Misc] ZSH 常用快捷键
安装 zsh 终端执行 brew install zsh 终端执行 vim ~/.bash_profile 命令,打开 .bash_profile 文件 如果没有 vim,请自行安装 在打开的文件中, ...
- ZOJ4103 Traveler(2019浙江省赛)
构造+思维~ #include<bits/stdc++.h> using namespace std; ; int N,M,T; int visit[maxn]; stack<int ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 显示代码:按键提示
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- 华为事件对A股的影响思考
美国对华为实施禁商令: 利好:自主可控-替代品 软件:国产操作系统(中国软件,浪潮软件,湘邮科技...) 芯片:国产芯片(士微兰,国民技术...) 利好:华为优势产品 5G: 利好:反制资源 稀土永磁 ...
- 记一次安装体验:pwn工具
几天前删了JDK,结果和VM在一个目录中,结果VM全没了,重安走起 记载一下安装虚拟机出现的问题,官网一个字....慢,于是找了百度网盘,据说win10版本太低没法用,我就下了vw15.5.0(建立在 ...