結果

問題 No.1677 mæx
ユーザー Manuel1024Manuel1024
提出日時 2022-09-07 03:15:28
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,214 bytes
コンパイル時間 987 ms
コンパイル使用メモリ 78,580 KB
実行使用メモリ 10,364 KB
最終ジャッジ日時 2023-08-14 08:32:43
合計ジャッジ時間 2,773 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 20 ms
9,408 KB
testcase_05 AC 21 ms
9,456 KB
testcase_06 AC 21 ms
9,440 KB
testcase_07 AC 21 ms
9,400 KB
testcase_08 AC 21 ms
9,548 KB
testcase_09 AC 20 ms
9,420 KB
testcase_10 AC 20 ms
9,452 KB
testcase_11 AC 20 ms
9,488 KB
testcase_12 AC 21 ms
9,464 KB
testcase_13 AC 20 ms
9,520 KB
testcase_14 AC 22 ms
9,460 KB
testcase_15 AC 21 ms
9,564 KB
testcase_16 AC 22 ms
9,448 KB
testcase_17 AC 22 ms
9,596 KB
testcase_18 AC 21 ms
9,768 KB
testcase_19 AC 1 ms
4,384 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 21 ms
10,364 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
using namespace std;
using ll = long long;
using P = pair<int, vector<int>>;
constexpr ll MOD = 998244353;

vector<P> G;
vector<vector<ll>> dp;

int expr(const string &s, int &i){
    // cerr << i << " " << s[i] << ", ";
    int ret = G.size();
    if(s[i] == 'm'){
        G.emplace_back(0, vector<int>{});
        if(s[i+1] == 'a'){// max
            G[ret].first = 4;
        }else if(s[i+1] == 'e'){// mex
            G[ret].first = 5;
        }else{// m?x
            G[ret].first = 6;
        }
        i += 4;
        int x = expr(s, i);
        G[ret].second.emplace_back(x);
        // cerr << "add ok" << endl;
        i++;
        int y = expr(s, i);
        G[ret].second.emplace_back(y);
        i++;
    }else{
        G.emplace_back(s[i] == '?' ? 3 : s[i]-'0', vector<int>{});
        i++;
    }
    // cerr << "ret: " << ret << ", ";
    return ret;
}

ll f(int v, int x){
    if(dp[x][v] != -1) return dp[x][v];
    auto mex = [](int a, int b){
        for(int res = 0; ; res++){
            if(res != a && res != b) return res;
        }
    };
    dp[x][v] = 0;
    if(G[v].first < 3) dp[x][v] = x == G[v].first ? 1 : 0;
    else if(G[v].first == 3) dp[x][v] = 1;
    else{
        if(G[v].first == 4 || G[v].first == 6){
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    if(max(i, j) == x){
                        dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD;
                        dp[x][v] %= MOD;
                    }
                }
            }
        }
        if(G[v].first == 5 || G[v].first == 6){
            for(int i = 0; i < 3; i++){
                for(int j = 0; j < 3; j++){
                    if(mex(i, j) == x){
                        dp[x][v] += f(G[v].second[0], i)*f(G[v].second[1], j)%MOD;
                        dp[x][v] %= MOD;
                    }
                }
            }
        }
    }
    return dp[x][v];
}

int main(){
    string s; cin >> s;
    int k; cin >> k;
    {
        int i = 0;
        expr(s, i);
    }
    
    dp = vector<vector<ll>>(3, vector<ll>(G.size(), -1));
    cout << f(0, k) << endl;
    return 0;
}
0