結果

問題 No.1917 LCMST
ユーザー 👑 H20H20
提出日時 2022-04-29 22:23:40
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,888 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 235,816 KB
最終ジャッジ日時 2023-09-11 13:44:25
合計ジャッジ時間 20,589 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,848 KB
testcase_01 AC 94 ms
71,552 KB
testcase_02 AC 102 ms
77,496 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 96 ms
71,756 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 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections
import math
def my_lcm(x, y):
    return (x * y) // math.gcd(x, y)


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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

        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 len(self.roots())

    def all_group_members(self):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

N =int(input())
A = list(map(int,input().split()))
CA = collections.Counter(A)
ans = N - len(CA)
ma = max(A)
mi = min(A)
uf = UnionFind(ma+1)
for i in reversed(range(2,ma+1)):
    jx = 0
    for j in range(i,ma+1,i):
        if CA[j]==1:
            if jx==0:
                jx = j
            else:
                if not uf.same(jx,j):
                    ans+=my_lcm(jx,j)
                    uf.union(jx,j)
for i in range(ma+1):
    if CA[i]==1 and not uf.same(mi,i):
        ans+=my_lcm(mi,i)
        uf.union(mi,i)
print(ans)
0