結果

問題 No.3753 Certainly a Cretan
コンテスト
ユーザー marc2825
提出日時 2026-08-19 00:55:35
言語 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
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,490 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,226 ms
コンパイル使用メモリ 223,064 KB
実行使用メモリ 22,496 KB
最終ジャッジ日時 2026-10-02 20:57:23
合計ジャッジ時間 9,349 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge4_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 45 TLE * 1
権限があれば一括ダウンロードができます

ソースコード

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;
    };

    vector<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.push_back(i);
        }
    }

    ll P = 1;

    for (int i = 1; i < (int)T.size(); ++i) {
        P = P * cat((T[i] - T[i - 1]) / 2) % MOD;
    }

    auto insert_even = [&](int x) {
        int pos = lower_bound(T.begin(), T.end(), x) - T.begin();

        int l = T[pos - 1];

        if (pos < (int)T.size()) {
            int r = T[pos];

            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(T.begin() + pos, x); // O(N)
    };

    auto erase_even = [&](int x) {
        int pos = lower_bound(T.begin(), T.end(), x) - T.begin();

        int l = T[pos - 1];

        if (pos + 1 < (int)T.size()) {
            int r = T[pos + 1];

            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(T.begin() + pos); // O(N)
    };

    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.back();
            int L = N - B;
            int q = K - B / 2;

            ll ans = 0;

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

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