题意:要求构造一个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;
}