結果

問題 No.1917 LCMST
ユーザー tamatotamato
提出日時 2022-04-29 23:05:37
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,628 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 291,548 KB
最終ジャッジ日時 2023-09-11 14:33:07
合計ジャッジ時間 11,671 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 300 ms
154,860 KB
testcase_01 AC 293 ms
154,908 KB
testcase_02 AC 292 ms
154,904 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 310 ms
154,708 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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()
0