結果

問題 No.1917 LCMST
ユーザー 👑 rin204rin204
提出日時 2022-05-05 14:24:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,173 ms / 4,000 ms
コード長 1,957 bytes
コンパイル時間 334 ms
コンパイル使用メモリ 86,988 KB
実行使用メモリ 326,612 KB
最終ジャッジ日時 2023-09-17 22:05:38
合計ジャッジ時間 63,732 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,116 KB
testcase_01 AC 74 ms
71,620 KB
testcase_02 AC 77 ms
76,228 KB
testcase_03 AC 122 ms
79,112 KB
testcase_04 AC 115 ms
79,116 KB
testcase_05 AC 117 ms
78,820 KB
testcase_06 AC 113 ms
79,156 KB
testcase_07 AC 113 ms
79,020 KB
testcase_08 AC 72 ms
71,128 KB
testcase_09 AC 2,413 ms
294,544 KB
testcase_10 AC 2,448 ms
306,184 KB
testcase_11 AC 2,424 ms
316,832 KB
testcase_12 AC 1,987 ms
264,260 KB
testcase_13 AC 918 ms
253,640 KB
testcase_14 AC 2,129 ms
273,356 KB
testcase_15 AC 704 ms
251,508 KB
testcase_16 AC 921 ms
252,368 KB
testcase_17 AC 1,228 ms
252,000 KB
testcase_18 AC 1,446 ms
253,952 KB
testcase_19 AC 735 ms
252,024 KB
testcase_20 AC 627 ms
252,468 KB
testcase_21 AC 772 ms
253,200 KB
testcase_22 AC 613 ms
249,888 KB
testcase_23 AC 614 ms
250,584 KB
testcase_24 AC 747 ms
253,308 KB
testcase_25 AC 618 ms
252,240 KB
testcase_26 AC 663 ms
250,784 KB
testcase_27 AC 687 ms
252,108 KB
testcase_28 AC 712 ms
250,340 KB
testcase_29 AC 3,173 ms
296,356 KB
testcase_30 AC 3,037 ms
296,408 KB
testcase_31 AC 3,023 ms
296,572 KB
testcase_32 AC 3,062 ms
296,768 KB
testcase_33 AC 3,032 ms
296,208 KB
testcase_34 AC 260 ms
235,556 KB
testcase_35 AC 257 ms
235,612 KB
testcase_36 AC 260 ms
236,916 KB
testcase_37 AC 3,112 ms
295,744 KB
testcase_38 AC 3,132 ms
295,480 KB
testcase_39 AC 3,069 ms
295,208 KB
testcase_40 AC 3,141 ms
296,080 KB
testcase_41 AC 3,009 ms
284,632 KB
testcase_42 AC 2,958 ms
326,612 KB
testcase_43 AC 316 ms
249,860 KB
testcase_44 AC 311 ms
249,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n = int(input())
A = list(map(int, input().split()))
A.sort()
ma = A[-1]
B = []
bef = -1
ans = 0
ind = [-1] * (ma + 1)
l = 0
for a in A:
    if a == bef:
        ans += a
    else:
        B.append(a)
        bef = a
        ind[a] = l
        l += 1

edges = set()
for i in range(1, ma + 1):
    ii = -1
    for j in range(i, ma + 1, i):
        if ind[j] != -1:
            if ii == -1:
                ii = ind[j]
            else:
                jj = ind[j]
                edges.add((B[ii] * B[jj] // gcd(B[ii], B[jj]), ii, jj))


edges = sorted(edges)
UF = UnionFind(n)
for c, i, j in edges:
    if not UF.same(i, j):
        ans += c
        UF.union(i, j)
print(ans)
0