跳到正文

题解

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

asd890123约 5 分钟
原文
文章信息与版本

文章信息

发布
2026年1月1日
收录
2026年7月25日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
2
题目
P14936

版本

  1. v1当前

sub1 做法

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

sub2

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

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

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

sub3

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

sub4

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

sub5(正确解法)

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

AC 代码:

C++
#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;
}
原文
存档评论1 条抓取于