题解:P13720 [GCPC 2024] Even Odd Game

· · 题解

简单题

由于我们指关注奇偶性,所以考察 +,\times 对奇偶性的影响,用脚分析,可以将 + 刻画成 \oplus,\times 刻划到 \wedge,将其归约到 x\wedge 1,x\wedge 0,x\oplus 1,x\oplus 0,我们发现 \wedge 1,\oplus 0 是没有用的,因此可以分成三个等价类 (x\oplus 1,x\wedge 0,x),分别记其个数为 A,B,C,那么我们可以用 f_{A,B,C,o} 表示三个等价类分别剩下 A,B,C 个,当前 x 的奇偶性为 o 时先手是否必赢,简单博弈 \mathrm{dp} 即可。边界需要处理一下,即 f_{0,0,0,o} 处,分类讨论开始时持先手还是后手即可。于是我们可以通过这个 O(n^3) 的 \mathrm{dp} 得到每种状态的胜负情况。

考虑怎么输出方案,用三个 multiset 存一下每种等价类的剩余卡牌,用 f 判断往哪个方向走是赢的即可。

:::info[代码]

#include <bits/stdc++.h>
#define psb push_back

using namespace std;

const int N=305;
int n;
int ta,tb,tc;
bool f[N][N][N][2];
bool vis[N][N][N][2];

bool dp(int a,int b,int c,int o)
{
    if (vis[a][b][c][o]) return f[a][b][c][o];
    bool &F=f[a][b][c][o]; vis[a][b][c][o]=true;
    if (!a && !b && !c) return F=((!o) ? (n&1) : ((n&1)^1));
    if (a && !dp(a-1,b,c,o^1)) return F=1;
    if (b && !dp(a,b-1,c,0)) return F=1;
    if (c && !dp(a,b,c-1,o)) return F=1;
    return F=0;
}

typedef pair<string,int> Node;
multiset<Node> sa,sb,sc;

void get(int &ta,int &tb,int &tc,int &cur)
{
    string o; int x; 
    cin >> o >> x;
    if (sa.find({o,x})!=sa.end()) sa.erase({o,x}),ta--;
    else if (sb.find({o,x})!=sb.end()) sb.erase({o,x}),tb--;
    else sc.erase({o,x}),tc--;

    if (o=="+" && (x&1)) cur^=1;
    else if (o=="*" && !(x&1)) cur=0;
}

void draw(multiset<Node> &st)
{
    Node tp=*st.begin(); 
    cout << tp.first << " " << tp.second << "\n";
    st.erase(st.begin());
}

void find(int a,int b,int c,int o,int self)
{
    if (!a && !b && !c) return;
    if (!self) get(a,b,c,o),find(a,b,c,o,self^1);
    else
    {
        if (a && !f[a-1][b][c][o^1]) draw(sa),find(a-1,b,c,o^1,self^1);
        else if (b && !f[a][b-1][c][0]) draw(sb),find(a,b-1,c,0,self^1);
        else draw(sc),find(a,b,c-1,o,self^1);
    }
}

int main()
{
    cin >> n;
    for (int i=1;i<=n;i++) 
    {
        string o; int x; 
        cin >> o >> x;
        if (o=="+") 
        {
            if (x&1) sa.insert({o,x}),ta++;
            else sc.insert({o,x}),tc++;
        }
        else 
        {
            if (!(x&1)) sb.insert({o,x}),tb++;
            else sc.insert({o,x}),tc++;
        }
    }
    int st; cin >> st; st=st&1;

    if (dp(ta,tb,tc,st))
    {
        puts("me");
        find(ta,tb,tc,st,1);
    }
    else
    {
        puts("you");
        find(ta,tb,tc,st,0);
    }

    return 0;
}

:::