結果

問題 No.1917 LCMST
ユーザー 👑 rin204rin204
提出日時 2022-05-05 14:27:19
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,198 bytes
コンパイル時間 389 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 271,140 KB
最終ジャッジ日時 2023-09-17 22:08:15
合計ジャッジ時間 24,433 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,172 KB
testcase_01 AC 71 ms
71,180 KB
testcase_02 AC 73 ms
75,632 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 70 ms
70,796 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 602 ms
268,432 KB
testcase_30 AC 607 ms
268,612 KB
testcase_31 AC 598 ms
269,568 KB
testcase_32 AC 605 ms
268,964 KB
testcase_33 AC 590 ms
269,668 KB
testcase_34 AC 254 ms
235,048 KB
testcase_35 AC 257 ms
234,792 KB
testcase_36 AC 252 ms
235,860 KB
testcase_37 AC 604 ms
268,604 KB
testcase_38 AC 597 ms
269,744 KB
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

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

def f(g, i, j):
    return (g * l + i) * l + j

isprime = [True] * (ma + 1)
edges = set()
for i in range(1, ma + 1):
    if not isprime[i]:
        continue
    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(f(B[ii] * B[jj] // gcd(B[ii], B[jj]), ii, jj))
        if i != 1 and i != j:
            isprime[j] = False


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