题解:P14936 「FAOI-R10」别样的 mex 大战

· · 题解

sub1 做法

此时异或和一定为 0,而 \operatorname{mex} 数值一定也为 0,所以啥也不用添加。

sub2

先预处理出异或和以及 \operatorname{mex} 数值,还有对于额外添加了 \operatorname{mex} 的数组 a 的新 \operatorname{mex} 值(记为 \operatorname{mex2})。

先判断,如果异或和本来就等于 \operatorname{mex},那啥也不用添加。

否则,枚举 [0,n],如果枚举到的添加数恰好是 \operatorname{mex},那么使用 \operatorname{mex2} 作为此次枚举的 \operatorname{mex} 值。

sub3

接 sub2,其实不用找,直接用 \operatorname{mex} \oplus \operatorname{xorsum} 即可,但如果此值等于 \operatorname{mex},那就改为添加 \operatorname{mex2} \oplus \operatorname{xorsum}。

sub4

其实个人认为比 sub2 和 sub3 简单,不管 n 为多少,其去重数组和异或和都是一样的,所以都和样例的第一组一样,添加 \{1,3\} 即可。

sub5(正确解法)

先测试用 sub3 做法能否成功达成条件,如果能,那就按 sub3 做法;如果不能,那就说明 k > 1,考虑 k = 2,显然还是要使得添加的两数异或结果为 \operatorname{mex},但是两数又都不能是 \operatorname{mex},可以想到用一个非 \operatorname{mex} 的数和这个数异或上 \operatorname{mex} 的结果。

AC 代码:

#include<bits/stdc++.h>
const int N = 1000 + 5;
int a[N];
int main(){
    std::cin.tie(0)->sync_with_stdio(0);
    int T;
    for (std::cin >> T;T--;){
        int n;std::cin >> n;
        bool sub1 = 1,sub4 = 1;
        for (int i = 1;i <= n;i++){
            std::cin >> a[i];
            if (a[i] != a[1]) sub1 = 0;
            if (a[i] != 0) sub4 = 0;
        }
        if (sub4){
            std::cout << "2\n1 3\n";
            continue;
        }
        if (n == 1000 && sub1){
            std::cout << 0 << '\n' << '\n';
            continue;
        }
        int xora = 0,mexa = -1,mexa2 = -1;
        std::set<int> se;
        for (int i = 1;i <= n;i++) xora ^= a[i],se.insert(a[i]);
        int i = 0;
        for (int j : se){
            if (j != i){
                mexa = i;
                se.insert(i);
                break;
            }
            ++i;
        }
        if (mexa == -1) mexa = i,se.insert(i);
        i = 0;
        for (int j : se){
            if (j != i){
                mexa2 = i;
                se.insert(i);
                break;
            }
            ++i;
        }
        if (mexa2 == -1) mexa2 = i,se.insert(i);
        if (mexa == xora){
            std::cout << 0 << '\n' << '\n';
            continue;
        }
        if ((mexa ^ xora) != mexa){
            std::cout << 1 << '\n';
            std::cout << (mexa ^ xora) << '\n';
            continue;
        }
        if ((mexa2 ^ xora) == mexa){
            std::cout << 1 << '\n';
            std::cout << (mexa2 ^ xora) << '\n';
            continue;
        }
        if (mexa != 2026){
            std::cout << 2 << '\n';
            std::cout << 2026 << ' ' << (2026 ^ mexa) << '\n';
            continue;
        }
        std::cout << 2 << '\n';
        std::cout << 2025 << ' ' << (2025 ^ mexa) << '\n';
    }
    return 0;
}