結果

問題 No.1677 mæx
ユーザー nebukuro09nebukuro09
提出日時 2021-09-10 23:46:34
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 2,427 bytes
コンパイル時間 656 ms
コンパイル使用メモリ 100,096 KB
実行使用メモリ 15,520 KB
最終ジャッジ日時 2023-09-04 14:05:32
合計ジャッジ時間 2,035 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 20 ms
13,052 KB
testcase_05 AC 20 ms
13,384 KB
testcase_06 AC 20 ms
12,392 KB
testcase_07 AC 20 ms
13,292 KB
testcase_08 AC 21 ms
13,780 KB
testcase_09 AC 21 ms
12,336 KB
testcase_10 AC 21 ms
12,244 KB
testcase_11 AC 21 ms
13,324 KB
testcase_12 AC 21 ms
13,308 KB
testcase_13 AC 21 ms
12,312 KB
testcase_14 AC 22 ms
13,860 KB
testcase_15 AC 21 ms
13,256 KB
testcase_16 AC 22 ms
12,232 KB
testcase_17 AC 22 ms
12,320 KB
testcase_18 AC 22 ms
12,340 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 22 ms
15,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable long MOD = 998244353;
enum Exp { MAX, MEX, UNKNOWN };

class Node {
    bool isLeaf;
    Exp exp;
    int num;
    long[3] cnt;
    Node left;
    Node right;
}

void main() {
    auto S = readln.chomp;
    auto K = readln.chomp.to!int;

    int idx = 0;

    Node parse() {
        Node node = new Node;
        if (S[idx] == ',') idx++;
        if (S[idx] == 'm') {
            node.isLeaf = false;
            if (S[idx..idx+3] == "max") {
                node.exp = Exp.MAX;
            } else if (S[idx..idx+3] == "mex") {
                node.exp = Exp.MEX;
            } else {
                node.exp = Exp.UNKNOWN;
            }
            idx += 4;
            node.left = parse();
            idx += 1;
            node.right = parse();
            idx += 1;
        } else {
            node.isLeaf = true;
            if (S[idx] != '?') {
                node.num = S[idx] - '0';
            } else  {
                node.num = -1;
            }
        }
        return node;
    }

    auto mex = new int[][](3, 3);

    int mex_(int i, int j) {
        foreach (k; 0..3) if (k != i && k != j) return k;
        assert(0);
    }

    foreach (i; 0..3) foreach (j; 0..3) mex[i][j] = mex_(i, j);

    void calc(Node node) {
        if (node.isLeaf) {
            if (node.num == -1) {
                node.cnt = [1, 1, 1];
            } else {
                node.cnt[node.num] = 1;
            }
            return;
        }

        calc(node.left);
        calc(node.right);

        if (node.exp == Exp.MAX) {
            foreach (i; 0..3) foreach (j; 0..3) {
                (node.cnt[max(i, j)] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD;
            }
        } else if (node.exp == Exp.MEX) {
            foreach (i; 0..3) foreach (j; 0..3) {
                (node.cnt[mex[i][j]] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD;
            }
        } else {
            foreach (i; 0..3) foreach (j; 0..3) {
                (node.cnt[max(i, j)] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD;
                (node.cnt[mex[i][j]] += node.left.cnt[i] * node.right.cnt[j] % MOD) %= MOD;
            }
        }
    }

    auto root = parse();
    calc(root);
    writeln(root.cnt[K]);
}
0