結果
問題 | No.2497 GCD of LCMs |
ユーザー | mkawa2 |
提出日時 | 2023-10-08 16:29:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 354 ms / 2,000 ms |
コード長 | 2,934 bytes |
コンパイル時間 | 332 ms |
コンパイル使用メモリ | 82,520 KB |
実行使用メモリ | 79,200 KB |
最終ジャッジ日時 | 2024-07-26 18:08:31 |
合計ジャッジ時間 | 3,944 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 51 ms
63,360 KB |
testcase_01 | AC | 51 ms
63,104 KB |
testcase_02 | AC | 51 ms
63,744 KB |
testcase_03 | AC | 49 ms
63,104 KB |
testcase_04 | AC | 51 ms
63,360 KB |
testcase_05 | AC | 51 ms
63,232 KB |
testcase_06 | AC | 61 ms
66,432 KB |
testcase_07 | AC | 145 ms
77,952 KB |
testcase_08 | AC | 204 ms
77,952 KB |
testcase_09 | AC | 229 ms
78,592 KB |
testcase_10 | AC | 278 ms
78,668 KB |
testcase_11 | AC | 207 ms
78,592 KB |
testcase_12 | AC | 343 ms
78,588 KB |
testcase_13 | AC | 351 ms
78,828 KB |
testcase_14 | AC | 204 ms
78,648 KB |
testcase_15 | AC | 200 ms
79,200 KB |
testcase_16 | AC | 354 ms
79,020 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] # dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 from collections import Counter class Sieve: def __init__(self, n): self.plist = [2] min_prime_factor = [2, 0]*(n//2+1) for x in range(3, n+1, 2): if min_prime_factor[x] == 0: min_prime_factor[x] = x self.plist.append(x) if x**2 > n: continue for y in range(x**2, n+1, 2*x): if min_prime_factor[y] == 0: min_prime_factor[y] = x self.min_prime_factor = min_prime_factor def isprime(self, x): return self.min_prime_factor[x] == x def pf(self, x): pp, ee = [], [] while x > 1: mpf = self.min_prime_factor[x] if pp and mpf == pp[-1]: ee[-1] += 1 else: pp.append(mpf) ee.append(1) x //= mpf return pp, ee # unsorted def factor(self, a): ff = [1] pp, ee = self.pf(a) for p, e in zip(pp, ee): ff, gg = [], ff w = p for _ in range(e): for f in gg: ff.append(f*w) w *= p ff += gg return ff sv = Sieve(31596) primes = sv.plist def prime_factorization(a): res = Counter() for p in primes: while a%p == 0: a //= p res[p] += 1 if a > 1: res[a] = 1 return res from heapq import * def dijkstra(p, root=0): n = len(to) dist = [inf]*n dist[root] = cnt[root][p] hp = [(dist[root], root)] while hp: d, u = heappop(hp) if d > dist[u]: continue for v in to[u]: nd = max(d,cnt[v][p]) if dist[v] <= nd: continue dist[v] = nd heappush(hp, (nd, v)) return dist n,m=LI() aa=LI() to=[[] for _ in range(n)] for _ in range(m): u,v=LI1() to[u].append(v) to[v].append(u) cnt=[] pp=set() for a in aa: c=prime_factorization(a) for p in c:pp.add(p) cnt.append(c) dp=[1]*n for p in pp: dist=dijkstra(p,0) for u in range(n): dp[u]*=pow(p,dist[u],md) dp[u]%=md print(*dp,sep="\n")