题解:P17621 [qaswed12 OI R1] Xor is Mex
Moonstruck · · 题解
话说这题的细节还是蛮多的。
首先作答第一问
注意到
然后就是第二问的重头戏了
想要满足条件,必须先满足
0.对于每种情况,如果最小的
1.如果
2.如果
3.令
然后根据上述结论模拟即可,代码比较冗余,这里做个参考
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
//Q1
if(n <= 2) cout << -1 << endl;
else {
for(int i = 1; i <= n - 2; i++) {
cout << 0 << " ";
}
cout << 1 << " " << 2 << endl;
}
//Q2
int sum = 0;
for(int i = 0; i < n; i++) {
sum ^= i;
}
if(sum == n) {
if(n >= 1000000) {
cout << -1 << endl;
return 0;
}
cout << n << " ";
for(int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
}
if(sum == 0) {
if(n == 1) {
cout << n + 2 << " " << 2 << " " << 3 << " ";
for(int i = 0; i < n; i++) cout << i << " ";
cout << endl;
return 0;
}
int b = n ^ 1;
if(b >= 1000000 || n + 2 >= 1000000) {
cout << -1 << endl;
return 0;
}
cout << n + 2 << " ";
cout << 1 << " " << b << " ";
for(int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
} else {
int x = sum ^ n;
if(x < 1000000) {
if(n + 1 >= 1000000) {
cout << -1 << endl;
return 0;
}
cout << n + 1 << " " << x << " ";
for(int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
return 0;
} else {
int lg = log2(x);
int a = 1 << lg;
int b = x ^ a;
if(a >= 1000000 || n + 2 >= 1000000) {
cout << -1 << endl;
return 0;
}
cout << n + 2 << " ";
cout << a << " " << b << " ";
for(int i = 0; i < n; i++) {
cout << i << " ";
}
cout << endl;
}
}
return 0;
}