結果

問題 No.2497 GCD of LCMs
ユーザー rlangevinrlangevin
提出日時 2023-10-06 22:48:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 814 ms / 2,000 ms
コード長 1,908 bytes
コンパイル時間 485 ms
コンパイル使用メモリ 87,388 KB
実行使用メモリ 107,668 KB
最終ジャッジ日時 2023-10-06 22:48:28
合計ジャッジ時間 7,415 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
72,032 KB
testcase_01 AC 101 ms
77,088 KB
testcase_02 AC 105 ms
76,944 KB
testcase_03 AC 95 ms
72,288 KB
testcase_04 AC 94 ms
72,168 KB
testcase_05 AC 93 ms
71,840 KB
testcase_06 AC 112 ms
77,868 KB
testcase_07 AC 257 ms
80,716 KB
testcase_08 AC 428 ms
86,304 KB
testcase_09 AC 435 ms
87,316 KB
testcase_10 AC 607 ms
94,856 KB
testcase_11 AC 368 ms
84,096 KB
testcase_12 AC 737 ms
102,644 KB
testcase_13 AC 772 ms
104,496 KB
testcase_14 AC 398 ms
86,760 KB
testcase_15 AC 375 ms
86,716 KB
testcase_16 AC 814 ms
107,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from heapq import heappush, heappop
inf = float('inf')


def dijkstra(s, g, N, val):
    # ゴールがない場合はg=-1とする。

    def cost(v, m):
        return v * N + m

    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Q = [cost(val, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if m == g:
            return dist

        #------heapをアップデートする。--------
        for u, C in G[m]:
            if seen[u]:
                continue
            newdist = max(dist[m], C)

            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost(newdist, u))
    return dist



def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    return arr


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

from collections import *
D = [defaultdict(int) for _ in range(N)]
ma = defaultdict(int)
for i in range(N):
    for k, v in factorization(A[i]):
        ma[k] = max(ma[k], v)
        D[i][k] = v    

ans = 1
mod = 998244353
ans = [1] * N
for k, v in ma.items():
    G = [[] for i in range(N)]
    for a, b in Edge:
        G[a].append((b, D[b][k]))
        G[b].append((a, D[a][k]))
        
    DD = dijkstra(0, -1, N, D[0][k])
    for i in range(N):
        ans[i] *= pow(k, DD[i], mod)
        ans[i] %= mod
        
for a in ans:
    print(a)
0