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次),每次提问提出一个字符串,会返回这 ...
随机推荐
- 如何优雅的使用python中的代码注释
在编写代码时,确保您的代码易于被其他人理解时很重要的,给变量,函数起合适的名字以及合理的组织代码都是很好的方法. 使用代码注释时增加代码可读性的另一种方便简单且重要的方法! 1.为什么代码注释如此重要 ...
- hadoop启动报错处理
1. hadoop启动报错 1.1. 问题1 util.NativeCodeLoader: Unable to load native-hadoop library for your ...
- SSH整合hibernate无法正常自动生成表
检查持久化类的属性和映射文件是否正确配置,比如date格式的属性最容易配置错误
- CSS概述(最详细!!!)
一.先综述 二.分述: 1.简介: 2.基本用法 3.引入方式: 4.盒模型 5.选择器: 6.常见文本样式及复合样式 7.改变行.块元素的属性: 8.标签显示与隐藏: ...
- Windows驱动开发-手动创建IRP
手动创建IRP有以下几个步骤: 1,先得到设备的指针,一种方法是用IoGetDeviceObjectPointer内核函数得到设备对象指针,另外一种方法是用zwCreateFile内核函数先得到设备句 ...
- Jquery 分页案例
Jquery 分页案例 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...
- (转)jquery.validate插件的使用
JQuery Validate使用总结:一.导入js库<script src="../js/jquery.js" type="text/javascript&quo ...
- 「CSP」第一届提高组考后总结
「CSP」第一届提高组考后总结 问题分析+反思 成绩 心态 考前心态 考时心态 考后心态 方法 心灵鸡汤... 在学习了三年之后,我们信竞迎来了初中最后一次大考,也是第一次 CSPCSPCSP 考试. ...
- 洛谷 P6046 [CTSC2000]快乐的蜜月
先讲解一下如何处理这道题的毒瘤输入.\(m\) 和 \(d\) 之间的"/"和" TO "都可以用 getchar() 强行吃掉,日期的转换可以用公式 \(s_ ...
- C++中%d,%s,%x,%f,%.100f,%的意思
C++中%d,%s,%x,%f,%.100f,%的意思 标准格式化输出:格式字符有d,o,x,u,c,s,f,e,g等. 格式说明:由“%”和格式字符组成,如%d.%f等. %c用来输出一个字符; % ...