結果

問題 No.3753 Certainly a Cretan
コンテスト
ユーザー marc2825
提出日時 2026-08-19 00:57:23
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 3,396 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,203 ms
コンパイル使用メモリ 225,776 KB
実行使用メモリ 43,524 KB
最終ジャッジ日時 2026-10-02 20:57:28
合計ジャッジ時間 4,285 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1 WA * 1
other AC * 24 WA * 22
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

using ll = long long;
const int MOD = 998244353;

ll modpow(ll a, ll n) {
    ll r = 1;
    while (n) {
        if (n & 1) r = r * a % MOD;
        a = a * a % MOD;
        n >>= 1;
    }
    return r;
}

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

    int N, Q;
    cin >> N >> Q;

    string S;
    cin >> S;

    vector<ll> fact(N + 1), invfact(N + 1);

    fact[0] = 1;
    for (int i = 1; i <= N; ++i) {
        fact[i] = fact[i - 1] * i % MOD;
    }

    invfact[N] = modpow(fact[N], MOD - 2);
    for (int i = N; i >= 1; --i) {
        invfact[i - 1] = invfact[i] * i % MOD;
    }

    auto C = [&](int n, int k) -> ll {
        if (k < 0 || k > n) return 0;
        return fact[n] * invfact[k] % MOD * invfact[n - k] % MOD;
    };

    auto cat = [&](int r) -> ll {
        return fact[2 * r] * invfact[r] % MOD * invfact[r + 1] % MOD;
    };

    auto invcat = [&](int r) -> ll {
        return invfact[2 * r] * fact[r] % MOD * fact[r + 1] % MOD;
    };

    set<int> T = {0};
    int bad = 0;

    for (int i = 1; i < N; ++i) {
        if (S[i - 1] == S[i]) continue;

        if (i & 1) ++bad;
        else T.insert(i);
    }

    ll P = 1;
    int last = 0;

    for (int x : T) {
        if (x == 0) continue;
        P = P * cat((x - last) / 2) % MOD;
        last = x;
    }

    auto insert_even = [&](int x) {
        auto it = T.lower_bound(x);
        int l = *prev(it);

        if (it != T.end()) {
            int r = *it;
            P = P * invcat((r - l) / 2) % MOD;
            P = P * cat((x - l) / 2) % MOD;
            P = P * cat((r - x) / 2) % MOD;
        } else {
            P = P * cat((x - l) / 2) % MOD;
        }

        T.insert(x);
    };

    auto erase_even = [&](int x) {
        auto it = T.find(x);
        int l = *prev(it);
        auto jt = next(it);

        if (jt != T.end()) {
            int r = *jt;
            P = P * invcat((x - l) / 2) % MOD;
            P = P * invcat((r - x) / 2) % MOD;
            P = P * cat((r - l) / 2) % MOD;
        } else {
            P = P * invcat((x - l) / 2) % MOD;
        }

        T.erase(it);
    };

    auto change_boundary = [&](int x) {
        if (x <= 0 || x >= N) return;

        bool diff = S[x - 1] != S[x];

        if (x & 1) {
            bad += diff ? -1 : 1;
        } else {
            if (diff) erase_even(x);
            else insert_even(x);
        }
    };

    while (Q--) {
        int t, x;
        cin >> t >> x;

        if (t == 1) {
            change_boundary(x - 1);
            change_boundary(x);

            S[x - 1] = (S[x - 1] == 'Y' ? 'N' : 'Y');
        } else {
            int K = x;

            if (bad) {
                cout << 0 << '\n';
                continue;
            }

            int B = *T.rbegin();
            int L = N - B;
            int q = K - B / 2;

            ll ans = 0;

            if (S.back() == 'Y') {
                // BUG: q = L/2 を禁止
                if (0 <= q && 2 * q < L) {
                    ans = P * ((C(L, q) - C(L, q - 1) + MOD) % MOD) % MOD;
                }
            } else {
                // BUG: q = L/2 を禁止
                if (0 <= q && q <= L && 2 * q > L) {
                    ans = P * ((C(L, q) - C(L, q + 1) + MOD) % MOD) % MOD;
                }
            }

            cout << ans << '\n';
        }
    }
}
0