結果

問題 No.623 fudan no modulus to tigau
ユーザー rlangevin
提出日時 2023-01-29 19:40:54
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 728 bytes
コンパイル時間 129 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,136 KB
最終ジャッジ日時 2024-06-29 15:23:46
合計ジャッジ時間 1,278 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**7)
from functools import lru_cache
mod = 998244353

@lru_cache(maxsize=None)
def f(t, i, a, b, x):
    if i == 0:
        return 1
    if i == 1:
        return x
    if t == 1:
        return (f(T[a], a, A[a], B[a], x) + f(T[b], b, A[b], B[b], x)) % mod
    elif t == 2:
        return a * f(T[b], b, A[b], B[b], x) % mod
    else:
        return (f(T[a], a, A[a], B[a], x) * f(T[b], b, A[b], B[b], x)) % mod
        

N = int(input())
T, A, B = [0] * 2, [0] * 2, [0] * 2
for i in range(N - 1):
    t, a, b = map(int, input().split())
    T.append(t)
    A.append(a)
    B.append(b)
    
Q  = int(input())
X = list(map(int, input().split()))
for x in X:
    print(f(T[N], N, A[N], B[N], x))
0