結果

問題 No.2497 GCD of LCMs
ユーザー pitPpitP
提出日時 2023-10-06 22:21:41
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 794 bytes
コンパイル時間 975 ms
コンパイル使用メモリ 87,296 KB
実行使用メモリ 82,316 KB
最終ジャッジ日時 2023-10-06 22:21:49
合計ジャッジ時間 6,114 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,636 KB
testcase_01 AC 70 ms
71,232 KB
testcase_02 AC 95 ms
76,444 KB
testcase_03 AC 73 ms
71,228 KB
testcase_04 AC 70 ms
71,436 KB
testcase_05 AC 71 ms
71,380 KB
testcase_06 AC 1,486 ms
81,448 KB
testcase_07 TLE -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

N, M = map(int, input().split())
A = list(map(int,input().split()))
g = [[] for _ in range(N)]
for _ in range(M):
    u, v = map(int, input().split())
    g[u - 1].append(v - 1)
    g[v - 1].append(u - 1)

visited = [False] * N
ans = [-1] * N
def dfs(now, goal, num):
    if now == goal:
        if ans[now] == -1:
            ans[now] = num
        else:
            ans[now] = gcd(ans[now], num)
        return 
    
    visited[now] = True
    for nxt in g[now]:
        if visited[nxt] : 
            continue
        dfs(nxt, goal, lcm(num, A[nxt]))
    visited[now] = False
    return 

for i in range(N):
    visited[0] = True
    dfs(0, i, A[0])
    visited[0] = False

for i in range(N):
    print(ans[i] % 998244353)
0