题目链接: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 }