跳到正文

题解

[qaswed12 OI R1] Xor is Mex 题解

Gcend约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年8月18日
收录
2026年10月6日
更新
2026年11月9日
篇幅
约 3 分钟
题目
P17621

版本

  1. v1当前

又是一道简单构造题。


考虑第一问,本质上是简单的。

  • 当 tt 为偶数时,令序列中所有数均为同一个非 00 数即可。
  • 当 tt 为奇数时,
    • t=1t=1 时,显然无解。
    • t≠1t \ne 1 时,注意到 1⊕2⊕3=01 \oplus 2 \oplus 3 = 0,考虑直接往序列中加入这 33 个数, 那么这个问题就被转化成了 tt 为偶数的情况。

还有一些其他做法均可通过。


第二问,也很简单。

首先,我们的序列中至少要有 0∼t−10 \sim t-1 每个数各一个。

我们设 xr=⨁i=0txr=\bigoplus\limits_{i=0}^{t}。
当 t≠1t \ne 1 时,xrxr 一定为偶数,反之,特判掉即可。

接下来我们有以下几种情况,

  • xr=txr=t,将 t+1t+1 和 11 加入序列,(t+1)⊕1=t(t+1 )\oplus 1=t,正好抵消。
  • xr<106xr < 10^6 且 xr≠txr \ne t,直接将 xrxr 加入序列。
  • xr≥106xr \ge 10^6,将 xrxr 拆成二进制下最高位和剩下的位的和,这两个数一定小于 10610^6。

边界情况可能需要特判。


code
C++
#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;
}
原文