結果

問題 No.3845 eval("G4M3")
コンテスト
ユーザー marc2825
提出日時 2026-10-11 12:59:45
結果
AC  
実行時間 307 ms / 2,000 ms
+ 764µs
コード長 2,967 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,578 ms
コンパイル使用メモリ 222,016 KB
実行使用メモリ 9,920 KB
最終ジャッジ日時 2026-12-02 23:30:22
合計ジャッジ時間 5,983 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 49
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

// 想定解法 O(N)
bool fast(const string& S) {
    int N = S.size();

    auto odd = [](char c) {
        return (c - '0') & 1;
    };

    bool adjacent = false;
    for (int i = 0; i + 1 < N; ++i) {
        if (odd(S[i]) && odd(S[i + 1])) {
            adjacent = true;
        }
    }

    bool L = odd(S.front());
    bool R = odd(S.back());

    if (N % 2 == 0) {
        return adjacent || L || R;
    } else {
        return !adjacent && (L != R);
    }
}

// 愚直全探索(小さい N 用)
bool brute(const string& S) {
    int N = S.size();
    int M = N - 1;

    // 0: 未確定, 1: +, 2: *
    vector<int> pow3(M + 1, 1);
    for (int i = 0; i < M; ++i) {
        pow3[i + 1] = pow3[i] * 3;
    }

    vector<int8_t> memo(pow3[M], -1);

    auto evaluate = [&](int state) -> bool {
        int val = 0;
        int cur = (S[0] - '0') & 1;

        for (int i = 0; i < M; ++i) {
            int op = (state / pow3[i]) % 3;
            int x = (S[i + 1] - '0') & 1;

            if (op == 1) { // +
                val ^= cur;
                cur = x;
            } else { // *
                cur &= x;
            }
        }

        val ^= cur;
        return val == 1;
    };

    auto dfs = [&](auto&& self, int state, int turn) -> bool {
        if (turn == M) {
            return evaluate(state);
        }

        if (memo[state] != -1) {
            return memo[state] == 1;
        }

        bool alice_turn = (turn % 2 == 0);

        for (int i = 0; i < M; ++i) {
            if ((state / pow3[i]) % 3 != 0) continue;

            for (int op = 1; op <= 2; ++op) {
                int nxt = state + op * pow3[i];
                bool awin = self(self, nxt, turn + 1);

                if (alice_turn && awin) {
                    memo[state] = 1;
                    return true;
                }
                if (!alice_turn && !awin) {
                    memo[state] = 0;
                    return false;
                }
            }
        }

        bool result = !alice_turn;
        memo[state] = result;
        return result;
    };

    return dfs(dfs, 0, 0);
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    constexpr int BRUTE_LIMIT = 9;

    int T;
    cin >> T;

    for (int tc = 1; tc <= T; ++tc) {
        int N;
        string S;
        cin >> N >> S;

        bool ans;

        if (N <= BRUTE_LIMIT) {
            bool b = brute(S);
            bool f = fast(S);

            if (b != f) {
                cerr << "Mismatch at case " << tc << '\n';
                cerr << "N = " << N << '\n';
                cerr << "S = " << S << '\n';
                cerr << "Brute = " << b << '\n';
                cerr << "Fast  = " << f << '\n';
                return 1;
            }

            ans = b;
        } else {
            ans = fast(S);
        }

        cout << (ans ? "Alice" : "Bob") << '\n';
    }

    return 0;
}
0