結果

問題 No.3250 最小公倍数
ユーザー かみのさちほ
提出日時 2025-08-29 22:51:36
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
実行時間 -
コード長 894 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 12,288 KB
実行使用メモリ 137,568 KB
最終ジャッジ日時 2025-08-29 22:52:04
合計ジャッジ時間 26,313 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample WA * 1
other WA * 17 TLE * 2 -- * 2
権限があれば一括ダウンロードができます

ソースコード

diff #

import math

MOD = 998244353

N = int(input())
a_list = list(map(int, input().split()))

graph = [[] for n in range(N + 1)]

for n in range(N - 1):
    u, v = map(int, input().split())
    graph[u].append(v)
    graph[v].append(u)

used = [False]*(N + 1)
res = [0]*(N + 1)


def dfs(now):
    a = a_list[now - 1]
    neibors = graph[now]
    used[now] = True
    if len(neibors) == 1:
        res[now] = a 
        # print(f"{now}:{res}")
        return a
    else:
        a_s = [a]
        for neibor in neibors:
            if used[neibor] is True:
                continue
            else:
                used[neibor] = True
                c = dfs(neibor)
                a_s.append(c)
        # print(f"{now}:{a_s}")
        r = math.lcm(*a_s) % MOD
        res[now] = r
        # print(f"{now}:{res}")
        return r


dfs(1)

# print(res)
for n in range(1, N + 1):
    print(res[n])
0