题解:P13720 [GCPC 2024] Even Odd Game

· · 题解

分析

我们注意到题目只要求我们判断奇偶性,所以我们可以把数字转化为 0(偶数)或 1(奇数)两个数字。

我们在小学的时候学过一下几个式子:

在这之中,我们发现一个数加上一个奇数会改变这个数的奇偶性,一个数乘上一个偶数一定为偶数,其余的不会改变奇偶性。

所以我们可以将这几个式子分为三类:

求解

定义

明显地,这道题需要使用 dp 求解。所以我们可以定义 f_{x,y,z,p} = 0 \text{ 或 } 1 表示当此时奇偶性为 p,不变类操作剩 x 个,异或操作剩 y 个,与操作剩 z 个时,是 Eve 胜还是 Bob 胜。

初始状态

明显的,我们可以很容易写出以下初始状态:

转移方程

我们细想一下,可以得到一下转移方程:

但是,需要注意边界问题。

其他问题

首先,你需要维护卡牌是否出现过,可以使用 map<string, int> 进行维护。

其次,你可以使用三个变量表示各类的数量。

最后,你每次输出后需要 cout.flush() 或者直接使用 endl 进行换行。

Code

#include <bits/stdc++.h>
using namespace std;

const int N = 3e2+10;
const int Max = 3e2;

string s, out;
int n, a, b, c, x;
bool f[N][N][N][5];
map<string, int> mp[5];

int GetVal(int x, int y, int z, int p) {
    if (x < 0 || y < 0 || z < 0 || p < 0) return 1; // 判断不合法
    return f[x][y][z][p];
}

void insert(int k, string s) {
    if (!mp[k].count(s)) mp[k][s] = 0;
    mp[k][s]++;
}

void del(int k, string s) {
    mp[k][s]--;
    if (mp[k][s] <= 0) mp[k].erase(s);
}

signed main() {
    cin >> n;
    f[0][0][0][0] = n % 2, f[0][0][0][1] = !(n % 2);
    for (int i = 0;i<= Max;i++) {
        for (int j = 0;j<= Max;j++) {
            for (int k = 0;k<= Max;k++) {
                if (i == 0 && j == 0 && k == 0)
                    continue; // 判断初值 
                // 转移方程 
                f[i][j][k][0] = (!GetVal(i-1, j, k, 0)) | (!GetVal(i, j-1, k, 1)) | (!GetVal(i, j, k-1, 0));
                f[i][j][k][1] = (!GetVal(i-1, j, k, 1)) | (!GetVal(i, j-1, k, 0)) | (!GetVal(i, j, k-1, 0));
            }
        }
    } 
    for (int i = 1;i<= n;i++) {
        char ch; int x;
        cin >> ch >> x;
        if (ch == '+') {
            s = "+ " + to_string(x);
            if (x % 2 == 1) insert(2, s), b++;
            else insert(1, s), a++;
        } else {
            s = "* " + to_string(x);
            if (x % 2 == 0) insert(3, s), c++;
            else insert(1, s), a++;
        }
    } 
    cin >> x;
    x = x % 2;
    if (!f[a][b][c][x]) cout << "you" << endl; // 判断先手和后手 
    else {
        cout << "me" << endl;
        if (a >= 1 && !f[a-1][b][c][x]) {  // 选择出牌对象 
            out = (*mp[1].begin()).first;
            del(1, out), a--;
        } else if (b >= 1 && !f[a][b-1][c][x^1]) {
            out = (*mp[2].begin()).first;
            del(2, out), b--, x ^= 1;
        } else if (c >= 1 && !f[a][b][c-1][0]) {
            out = (*mp[3].begin()).first;
            del(3, out), c--, x = 0;
        } else {
            out = "No solution!";
        }
        cout << out << endl; // 进行出牌 
    }
    while (a > 0 || b > 0 || c > 0) {
        char ch; int k;
        cin >> ch >> k; // 交互库出牌
        if (ch == '+') {
            s = "+ " + to_string(k);
            if (k % 2 == 1) del(2, s), b--, x = x ^ 1;
            else del(1, s), a--;
        } else {
            s = "* " + to_string(k);
            if (k % 2 == 0) del(3, s), c--, x = 0;
            else del(1, s), a--;
        }
        if (a <= 0 && b <= 0 && c <= 0) break;
        // 与上面同理 
        if (a >= 1 && !f[a-1][b][c][x]) { 
            out = (*mp[1].begin()).first;
            del(1, out), a--;
        } else if (b >= 1 && !f[a][b-1][c][x^1]) {
            out = (*mp[2].begin()).first;
            del(2, out), b--, x ^= 1;
        } else if (c >= 1 && !f[a][b][c-1][0]) {
            out = (*mp[3].begin()).first;
            del(3, out), c--, x = 0;
        } else {
            out = "No solution!";
        }
        cout << out << endl; 
    }
}