結果

問題 No.2497 GCD of LCMs
ユーザー KumaTachiRenKumaTachiRen
提出日時 2023-09-06 14:22:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 369 ms / 2,000 ms
コード長 1,190 bytes
コンパイル時間 372 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 79,660 KB
最終ジャッジ日時 2023-09-06 14:22:37
合計ジャッジ時間 5,331 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 77 ms
71,408 KB
testcase_01 AC 85 ms
76,156 KB
testcase_02 AC 83 ms
76,264 KB
testcase_03 AC 86 ms
71,224 KB
testcase_04 AC 77 ms
71,400 KB
testcase_05 AC 78 ms
71,508 KB
testcase_06 AC 89 ms
76,436 KB
testcase_07 AC 178 ms
78,440 KB
testcase_08 AC 216 ms
79,220 KB
testcase_09 AC 209 ms
78,740 KB
testcase_10 AC 369 ms
79,660 KB
testcase_11 AC 237 ms
78,740 KB
testcase_12 AC 296 ms
78,996 KB
testcase_13 AC 361 ms
79,424 KB
testcase_14 AC 331 ms
79,304 KB
testcase_15 AC 289 ms
78,948 KB
testcase_16 AC 343 ms
79,200 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import heapq

MOD = 998244353
BIG = 100

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())
    u -= 1
    v -= 1
    g[u].append(v)
    g[v].append(u)

primes = set()

for i in range(n):
    x = a[i]
    p = 2
    while p * p <= x:
        if x % p == 0:
            primes.add(p)
            while x % p == 0:
                x //= p
        p += (p & 1) + 1
    if x > 1:
        primes.add(x)

ans = [1] * n
power = [1] * BIG
c = [0] * n
d = [0] * n

for p in primes:
    for i in range(n):
        c[i] = 0
        x = a[i]
        while x % p == 0:
            c[i] += 1
            x //= p
        d[i] = BIG
    d[0] = c[0]
    pq = [[d[0], 0]]
    heapq.heapify(pq)
    while len(pq) > 0:
        xd, x = heapq.heappop(pq)
        for y in g[x]:
            yd = max(c[y], xd)
            if d[y] > yd:
                d[y] = yd
                heapq.heappush(pq, [yd, y])
    power[0] = 1
    for i in range(1, BIG):
        power[i] = power[i - 1] * p % MOD
    for i in range(n):
        ans[i] = ans[i] * power[d[i]] % MOD

print("\n".join(map(str, ans)))
0