結果

問題 No.1677 mæx
ユーザー norikamenorikame
提出日時 2021-09-10 23:17:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 385 ms / 2,000 ms
コード長 2,614 bytes
コンパイル時間 2,232 ms
コンパイル使用メモリ 209,972 KB
実行使用メモリ 17,292 KB
最終ジャッジ日時 2023-09-02 21:41:08
合計ジャッジ時間 6,311 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,372 KB
testcase_01 AC 1 ms
4,368 KB
testcase_02 AC 2 ms
4,372 KB
testcase_03 AC 2 ms
4,368 KB
testcase_04 AC 164 ms
16,172 KB
testcase_05 AC 163 ms
16,124 KB
testcase_06 AC 162 ms
16,096 KB
testcase_07 AC 164 ms
16,104 KB
testcase_08 AC 165 ms
16,168 KB
testcase_09 AC 163 ms
16,208 KB
testcase_10 AC 164 ms
16,224 KB
testcase_11 AC 163 ms
16,288 KB
testcase_12 AC 163 ms
16,096 KB
testcase_13 AC 164 ms
16,204 KB
testcase_14 AC 201 ms
16,164 KB
testcase_15 AC 198 ms
16,260 KB
testcase_16 AC 200 ms
16,292 KB
testcase_17 AC 204 ms
16,332 KB
testcase_18 AC 205 ms
16,308 KB
testcase_19 AC 1 ms
4,368 KB
testcase_20 AC 2 ms
4,368 KB
testcase_21 AC 385 ms
17,292 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using ll = long long;

#define rep(i, n) for (int i=0; i<(int)(n); ++(i))
#define rep3(i, m, n) for (int i=(m); (i)<(int)(n); ++(i))
#define repr(i, n) for (int i=(int)(n)-1; (i)>=0; --(i))
#define rep3r(i, m, n) for (int i=(int)(n)-1; (i)>=(int)(m); --(i))
#define all(x) (x).begin(), (x).end()

const ll mod = 998244353LL;
struct mint {
    ll x;
    mint(ll x=0) : x((x%mod+mod)%mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod-a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this;}
    mint operator+(const mint a) const { return mint(*this) += a;}
    mint operator-(const mint a) const { return mint(*this) -= a;}
    mint operator*(const mint a) const { return mint(*this) *= a;}
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t>>1);
        a *= a;
        if (t&1) a *= *this;
        return a;
    }
    mint inv() const { return pow(mod-2);}
    mint& operator/=(const mint a) { return *this *= a.inv();}
    mint operator/(const mint a) const { return mint(*this) /= a;}
};
istream& operator>>(istream& is, mint& a) { return is >> a.x; }
ostream& operator<<(ostream& os, const mint& a) { return os << a.x; }

inline int mex(int a, int b) {
    int res = 0;
    while (res==a || res==b) ++res;
    return res;
}

string s;

map<pair<int, int>, pair<mint, int>> memo;
pair<mint, int> calc(int id, int k) {
    if (memo.find({id,k}) != memo.end()) return memo[{id,k}];
    pair<mint, int> res = { mint(0), id };
    if (id >= (int)(s.length())) return (memo[{id,k}] = res);
    if (s[id] == 'm') {
        int nid1 = id + 4;
        pair<mint, int> rzero1 = { mint(0), id }, rzero2 = { mint(0), id };
        rzero1 = calc(nid1, 0);
        int nid2 = rzero1.second + 2;
        rzero2 = calc(nid2, 0);
        res.second = rzero2.second + 1;
        if (s[id+1]=='a' || s[id+1]=='?') rep(i, 3) rep(j, 3) if (max(i, j) == k) res.first += calc(nid1, i).first * calc(nid2, j).first;
        if (s[id+1]=='e' || s[id+1]=='?') rep(i, 3) rep(j, 3) if (mex(i, j) == k) res.first += calc(nid1, i).first * calc(nid2, j).first;
    }
    else {
        if (s[id] == '?') res.first = 1;
        else if (s[id]-'0' == k) res.first = 1;
        else res.first = 0;
    }
    return (memo[{id,k}] = res);
}

int main() {
    int k;
    cin >> s >> k;
    cout << calc(0, k).first << endl;
    return 0;
}
0