結果

問題 No.2497 GCD of LCMs
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-09-21 05:39:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 512 ms / 2,000 ms
コード長 2,380 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 89,580 KB
最終ジャッジ日時 2024-09-21 05:40:02
合計ジャッジ時間 4,493 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 43 ms
54,240 KB
testcase_01 AC 48 ms
59,896 KB
testcase_02 AC 49 ms
60,400 KB
testcase_03 AC 43 ms
55,164 KB
testcase_04 AC 42 ms
54,792 KB
testcase_05 AC 43 ms
56,120 KB
testcase_06 AC 54 ms
63,056 KB
testcase_07 AC 141 ms
77,256 KB
testcase_08 AC 201 ms
80,584 KB
testcase_09 AC 214 ms
81,428 KB
testcase_10 AC 316 ms
80,588 KB
testcase_11 AC 213 ms
78,944 KB
testcase_12 AC 390 ms
87,004 KB
testcase_13 AC 454 ms
88,364 KB
testcase_14 AC 204 ms
77,884 KB
testcase_15 AC 194 ms
77,536 KB
testcase_16 AC 512 ms
89,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

## https://yukicoder.me/problems/no/2497
import math
from collections import deque
MOD = 998244353
MAX_INT = 10 ** 18
def main():
N, M = map(int, input().split())
A = list(map(int, input().split()))
edges = []
for _ in range(M):
u, v = map(int, input().split())
edges.append((u - 1, v - 1))
#
prime_maps = [{} for _ in range(N)]
primes = set()
for i in range(N):
a = A[i]
sqrt_a = int(math.sqrt(a))
for p in range(2, sqrt_a + 1):
if a % p == 0:
prime_maps[i][p] = 0
primes.add(p)
while a % p == 0:
a //= p
prime_maps[i][p] += 1
if a > 1:
prime_maps[i][a] = 1
primes.add(a)
#
def dijkstra(N, prime_maps, p, edges):
next_nodes = [ [] for _ in range(2 * N)]
for u, v in edges:
next_nodes[u].append((v + N, 0))
next_nodes[v].append((u + N, 0))
value_map = {0:deque()}
for i in range(N):
if p in prime_maps[i]:
next_nodes[i + N].append((i, prime_maps[i][p]))
if prime_maps[i][p] not in value_map:
value_map[prime_maps[i][p]] = deque()
else:
next_nodes[i + N].append((i, 0))
values = list(value_map.keys())
values.sort()
dists = [MAX_INT] * (2 * N)
value_map[0].append(N)
for k in values:
q = value_map[k]
while len(q) > 0:
v = q.popleft()
if dists[v] < MAX_INT:
continue
dists[v] = k
for w, c in next_nodes[v]:
if dists[w] < MAX_INT:
continue
if k < c:
value_map[c].append(w)
else:
q.append(w)
return dists
answers = [1] * N
for p in primes:
dists = dijkstra(N, prime_maps, p, edges)
for i in range(N):
ans = pow(p, dists[i], MOD)
answers[i] *= ans
answers[i] %= MOD
for i in range(N):
print(answers[i])
if __name__ == "__main__":
main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0