CF 862C Mahmoud and Ehab and the xor(异或)

题目链接:http://codeforces.com/problemset/problem/862/C

题目:

Mahmoud and Ehab are on the third stage of their adventures now. As you know, Dr. Evil likes sets. This time he won‘t show them any set from his large collection, but will ask them to create a new set to replenish his beautiful collection of sets.

Dr. Evil has his favorite evil integer x. He asks Mahmoud and Ehab to find a set of n distinct non-negative integers such the bitwise-xor sum of the integers in it is exactly x. Dr. Evil doesn‘t like big numbers, so any number in the set shouldn‘t be greater than 106.

Input

The only line contains two integers n and x (1 ≤ n ≤ 105, 0 ≤ x ≤ 105) — the number of elements in the set and the desired bitwise-xor, respectively.

Output

If there is no such set, print "NO" (without quotes).

Otherwise, on the first line print "YES" (without quotes) and on the second line print n distinct integers, denoting the elements in the set is any order. If there are multiple solutions you can print any of them.

Examples

input

5 5

output

YES1 2 4 5 7

input

3 6

output

YES1 2 5

Note

You can read more about the bitwise-xor operation here: https://en.wikipedia.org/wiki/Bitwise_operation#XOR

For the first sample .

For the second sample .

题意:判断是否能够用n个数异或得到x。不可以输出NO,可以的话输出YES,并输出那n个数。

题解:a^a=0,a^0=a。(1^2^3^4^5....^n-3)^(1^2^3^4^5....^n-3)^x=x。如果之前的n-3个数已经得到x,那么直接异或1<<17,1<<18,1<<18+1<<17,这三者异或值为0,不影响结果;没有得到我们就异或1<<17,0,(1<<17)^ans^x。注意特判一下  n==1 ||n==2&&x==0  的情况。

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4
 5 const int E=1<<17;
 6
 7 int main(){
 8     int n,x;
 9     cin>>n>>x;
10     if(n==1) cout<<"YES"<<endl<<x<<endl;
11     else if(n==2&&x==0) cout<<"NO"<<endl;
12     else if(n==2&&x!=0) cout<<"YES"<<endl<<"0 "<<x<<endl;
13     else{
14         int ans=0;
15         cout<<"YES"<<endl;
16         for(int i=1;i<=n-3;i++){
17             cout<<i<<" ";
18             ans^=i;
19         }
20         if(ans==x) cout<<(E)<<" "<<(E+E<<1)<<" "<<(E<<1)<<endl;
21         else{
22             cout<<(E)<<" 0 "<<(E^ans^x)<<endl;
23         }
24     }
25     return 0;
26 }
时间: 2024-11-24 10:50:14

CF 862C Mahmoud and Ehab and the xor(异或)的相关文章

Codeforces 862C - Mahmoud and Ehab and the xor

862C - Mahmoud and Ehab and the xor 思路:找两对异或后等于(1<<17-1)的数(相当于加起来等于1<<17-1),两个再异或一下就变成0了,0异或x等于x.所以只要把剩下的异或起来变成x就可以了.如果剩下来有3个,那么,这3个数可以是x^i^j,i,j. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back

Codeforces 862B - Mahmoud and Ehab and the bipartiteness

862B - Mahmoud and Ehab and the bipartiteness 思路:先染色,然后找一种颜色dfs遍历每一个点求答案. 代码: #include<bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define mem(a,b) memset(a,b,sizeof(a)) const int N=1e6+5; bool color[N]; vector<i

E - Mahmoud and Ehab and the bipartiteness CodeForces - 862B (dfs黑白染色)

Mahmoud and Ehab continue their adventures! As everybody in the evil land knows, Dr. Evil likes bipartite graphs, especially trees. A tree is a connected acyclic graph. A bipartite graph is a graph, whose vertices can be partitioned into 2 sets in su

Codeforces 959 D Mahmoud and Ehab and another array construction task

Discription Mahmoud has an array a consisting of n integers. He asked Ehab to find another arrayb of the same length such that: b is lexicographically greater than or equal to a. bi?≥?2. b is pairwise coprime: for every 1?≤?i?<?j?≤?n, bi and bj are c

[CF959E] Mahmoud and Ehab and the xor-MST - 贪心,最小生成树

\(n\) 个点的完全图标号 \((0-n-1)\),\(i\) 和 \(j\) 连边权值为 \(i\ \textrm{XOR}\ j\),求 MST 的值 Solution 设 \(f[n]\) 表示点数为 \(n+1\) 时的答案,那么贪心地考虑,显然 \(f[0]=0, f[n]=f[n-1]+lowbit(n)\) 根据观察易得 \(f[n]=2f[n-1]+2^n-2^{n-1}\),同时由于 \(f[]\) 就是个 \(lowbit\) 的前缀和,满足可加性,所以直接对 \(n\)

Codeforces Round #435 (Div. 2) D. Mahmoud and Ehab and the binary string[二分]

题目:http://codeforces.com/problemset/problem/862/D 题意:交互题,询问15次以内Hamming distance,输出一个二进制串里任意一个0或1的位置 题解:极简单的二分,从最后一位先判断一个,然后二分 根据上次和本次的距离差是否等于二分长度判断在左端还是右端有需要寻找的值寻找另一个. 1 #define _CRT_SECURE_NO_DEPRECATE 2 #pragma comment(linker, "/STACK:102400000,10

[Codeforces]862F - Mahmoud and Ehab and the final stage

题目大意:n个字符串,支持修改一个位置上的字符串和查询一个区间的子区间中长度乘LCP的最大值,输入字符数和询问数不超过10^5. 做法:求出相邻的LCP长度,区间LCP等于区间最小值,查询分几种情况考虑,一种只有一个串,线段树维护长度最大值即可:若有若干个串,设一个阈值k,若答案的LCP<=k,对于小等k的每一个i,若一个位置的相邻LCP大等i,设为1,否则设为0,即求区间最长连续1,每种i开一棵线段树维护即可:若LCP>k,我们把相邻LCP长度超过k的位置存进set,查询的时候拿出来,直接建

codeforces 862B B. Mahmoud and Ehab and the bipartiteness

http://codeforces.com/problemset/problem/862/B 题意: 给出一个有n个点的二分图和n-1条边,问现在最多可以添加多少条边使得这个图中不存在自环,重边,并且此图还是一个二分图. 思路: 想得比较复杂了....其实既然已经给出了二分图并且有n-1条边,那么我们就一定可以用染色法对每一个点进行染色,从而将点划分为两个集合,然后从两个集合中分别任意选择一个点连边就行了. 一开始考虑二分图的基本属性是不存在奇数条边的环...并不需要这样,因为两个集合是分开的,

CodeForces - 862B Mahmoud and Ehab and the bipartiteness(二分图染色)

题意:给定一个n个点的树,该树同时也是一个二分图,问最多能添加多少条边,使添加后的图也是一个二分图. 分析: 1.通过二分图染色,将树中所有节点分成两个集合,大小分别为cnt1和cnt2. 2.两个集合间总共可以连cnt1*cnt2条边,给定的是一个树,因此已经连了n-1条边,所以最多能连cnt1*cnt2-(n-1)条边. 3.注意输出. #include<cstdio> #include<cstring> #include<cstdlib> #include<