結果

問題 No.1677 mæx
ユーザー 👑 tamatotamato
提出日時 2021-09-10 22:08:12
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 118 ms / 2,000 ms
コード長 2,454 bytes
コンパイル時間 347 ms
コンパイル使用メモリ 87,428 KB
実行使用メモリ 79,284 KB
最終ジャッジ日時 2023-09-02 17:55:54
合計ジャッジ時間 3,604 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
71,632 KB
testcase_01 AC 69 ms
71,356 KB
testcase_02 AC 71 ms
71,760 KB
testcase_03 AC 69 ms
71,452 KB
testcase_04 AC 113 ms
78,660 KB
testcase_05 AC 115 ms
78,720 KB
testcase_06 AC 117 ms
78,904 KB
testcase_07 AC 112 ms
79,000 KB
testcase_08 AC 114 ms
78,600 KB
testcase_09 AC 113 ms
78,660 KB
testcase_10 AC 118 ms
78,848 KB
testcase_11 AC 115 ms
78,996 KB
testcase_12 AC 118 ms
78,740 KB
testcase_13 AC 114 ms
78,868 KB
testcase_14 AC 96 ms
78,732 KB
testcase_15 AC 94 ms
78,348 KB
testcase_16 AC 97 ms
78,420 KB
testcase_17 AC 98 ms
78,364 KB
testcase_18 AC 96 ms
78,936 KB
testcase_19 AC 67 ms
71,700 KB
testcase_20 AC 69 ms
71,564 KB
testcase_21 AC 96 ms
79,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
eps = 10**-9


def main():
    import sys
    input = sys.stdin.readline

    S = input().rstrip('\n')
    N = len(S)
    K = int(input())

    st = []
    ii = 0
    while ii < N:
        if S[ii] == "m":
            if S[ii+1] == "a":
                st.append("max")
            elif S[ii+1] == "e":
                st.append("mex")
            else:
                st.append("maex")
            ii += 4
        elif S[ii] == ",":
            ii += 1
        elif S[ii] == ")":
            L_new = [0] * 3
            L1 = st.pop()
            L2 = st.pop()
            T = st.pop()
            if T == "max":
                L_new[0] = (L1[0] * L2[0])%mod
                L_new[1] = (((L1[1] + L1[0])%mod * (L2[1] + L2[0])%mod)%mod - L_new[0])%mod
                L_new[2] = (((L1[2] + L1[1] + L1[0])%mod * (L2[2] + L2[1] + L2[0])%mod)%mod - L_new[0] - L_new[1])%mod
            elif T == "mex":
                for i in range(3):
                    for j in range(3):
                        if i != j:
                            L_new[3-i-j] = (L_new[3-i-j] + (L1[i] * L2[j])%mod)%mod
                        else:
                            if i:
                                k = 0
                            else:
                                k = 1
                            L_new[k] = (L_new[k] + (L1[i] * L2[j])%mod)%mod
            else:
                L_new[0] = (L1[0] * L2[0]) % mod
                L_new[1] = (((L1[1] + L1[0]) % mod * (L2[1] + L2[0]) % mod) % mod - L_new[0]) % mod
                L_new[2] = (((L1[2] + L1[1] + L1[0]) % mod * (L2[2] + L2[1] + L2[0]) % mod) % mod - L_new[0] - L_new[
                    1]) % mod
                for i in range(3):
                    for j in range(3):
                        if i != j:
                            L_new[3 - i - j] = (L_new[3 - i - j] + (L1[i] * L2[j]) % mod) % mod
                        else:
                            if i:
                                k = 0
                            else:
                                k = 1
                            L_new[k] = (L_new[k] + (L1[i] * L2[j]) % mod) % mod
            ii += 1
            st.append(L_new)
        else:
            if S[ii] == "?":
                st.append([1, 1, 1])
            else:
                L = [0, 0, 0]
                L[int(S[ii])] = 1
                st.append(L)
            ii += 1
    print(st[0][K])


if __name__ == '__main__':
    main()
0