結果
| 問題 | No.1917 LCMST |
| コンテスト | |
| ユーザー |
tamato
|
| 提出日時 | 2022-04-29 23:05:37 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,628 bytes |
| コンパイル時間 | 371 ms |
| コンパイル使用メモリ | 82,048 KB |
| 実行使用メモリ | 284,252 KB |
| 最終ジャッジ日時 | 2024-06-29 04:45:08 |
| 合計ジャッジ時間 | 11,175 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 1 WA * 5 TLE * 1 -- * 35 |
ソースコード
mod = 998244353
NN = 10 ** 5
def main():
import sys
from heapq import heappush, heappop
from collections import Counter, deque
from math import gcd
input = sys.stdin.readline
div = [[] for _ in range(NN + 1)]
for d in range(1, NN + 1):
for x in range(1, NN + 1):
if d * x > NN:
break
div[d * x].append(d)
N = int(input())
A = list(map(int, input().split()))
C = Counter(A)
ans = 0
mi = [deque() for _ in range(NN + 1)]
A_sorted = sorted(list(set(A)))
for i, a in enumerate(A_sorted):
ans += a * (C[a] - 1)
if i:
for d in div[a]:
mi[d].append(a)
if len(A_sorted) == 1:
print(ans)
exit()
pq = []
seen = [0] * (NN + 1)
a0 = A_sorted[0]
seen[a0] = 1
for d in div[a0]:
if mi[d]:
a = mi[d][0]
heappush(pq, (a0 * a // gcd(a0, a), a, a0, d))
cnt = len(A_sorted) - 1
while cnt:
lcm, a, b, d_prev = heappop(pq)
if seen[a]:
continue
ans += lcm
seen[a] = 1
cnt -= 1
for d in div[a]:
while mi[d]:
if seen[mi[d][0]] == 1:
mi[d].popleft()
else:
break
if mi[d]:
a_new = mi[d][0]
heappush(pq, (a * a_new // gcd(a, a_new), a_new, a, d))
if mi[d_prev]:
a_new = mi[d_prev][0]
heappush(pq, (b * a_new // gcd(b, a_new), a_new, b, d_prev))
print(ans)
if __name__ == '__main__':
main()
tamato