[qaswed12 OI R1] Xor is Mex 题解
又是一道简单构造题。
考虑第一问,本质上是简单的。
- 当
t 为偶数时,令序列中所有数均为同一个非0 数即可。 - 当
t 为奇数时,-
-
那么这个问题就被转化成了 $t$ 为偶数的情况。
-
还有一些其他做法均可通过。
第二问,也很简单。
首先,我们的序列中至少要有
我们设
当
接下来我们有以下几种情况,
边界情况可能需要特判。
:::success[code]
#include <bits/stdc++.h>
using namespace std;
vector<int> v;
int main(){
int n,xr=0;
cin >>n;
if(n==1){
cout <<"-1\n3 0 2 3\n";
return 0;
}
else if(n&1){
for(int i=1;i<=n-3;i++) cout <<1<<" ";
cout <<"1 2 3\n";
}
else {
for(int i=1;i<=n;i++) cout <<1<<" ";
cout <<"\n";
}
if(n==1000000){
cout <<-1<<"\n";
return 0;
}
for(int i=0;i<n;i++) xr^=i,v.push_back(i);
if(xr!=n){
xr^=n;
if(xr==n) v.push_back(xr+1),v.push_back(1);
else if(xr<1000000) v.push_back(xr);
else {
int x=1;
while((x<<1)<=xr) x<<=1;
v.push_back(x),v.push_back(x^xr);
}
}
int len=v.size();
if(len>=1000000) cout <<-1<<"\n";
else {
cout <<len<<" ";
for(int i=0;i<len;i++) cout <<v[i]<<" ";
cout <<"\n";
}
return 0;
}
:::