結果

問題 No.1917 LCMST
ユーザー mkawa2mkawa2
提出日時 2023-11-14 14:28:57
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,014 ms / 4,000 ms
コード長 3,240 bytes
コンパイル時間 207 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 292,812 KB
最終ジャッジ日時 2024-09-26 03:36:24
合計ジャッジ時間 50,194 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
63,616 KB
testcase_01 AC 56 ms
63,360 KB
testcase_02 AC 55 ms
63,744 KB
testcase_03 AC 116 ms
78,336 KB
testcase_04 AC 120 ms
78,276 KB
testcase_05 AC 116 ms
78,440 KB
testcase_06 AC 110 ms
78,432 KB
testcase_07 AC 114 ms
78,288 KB
testcase_08 AC 56 ms
63,360 KB
testcase_09 AC 2,846 ms
288,288 KB
testcase_10 AC 3,006 ms
288,116 KB
testcase_11 AC 2,946 ms
290,308 KB
testcase_12 AC 3,014 ms
292,812 KB
testcase_13 AC 1,106 ms
245,288 KB
testcase_14 AC 2,855 ms
290,388 KB
testcase_15 AC 677 ms
242,356 KB
testcase_16 AC 1,110 ms
245,020 KB
testcase_17 AC 1,568 ms
249,256 KB
testcase_18 AC 2,295 ms
269,948 KB
testcase_19 AC 687 ms
241,960 KB
testcase_20 AC 538 ms
241,168 KB
testcase_21 AC 747 ms
242,484 KB
testcase_22 AC 574 ms
240,928 KB
testcase_23 AC 579 ms
240,944 KB
testcase_24 AC 755 ms
242,336 KB
testcase_25 AC 580 ms
240,800 KB
testcase_26 AC 629 ms
241,180 KB
testcase_27 AC 683 ms
241,840 KB
testcase_28 AC 654 ms
242,220 KB
testcase_29 AC 1,142 ms
286,728 KB
testcase_30 AC 1,225 ms
286,648 KB
testcase_31 AC 1,254 ms
286,528 KB
testcase_32 AC 1,086 ms
287,132 KB
testcase_33 AC 1,153 ms
286,768 KB
testcase_34 AC 274 ms
222,752 KB
testcase_35 AC 255 ms
222,512 KB
testcase_36 AC 266 ms
222,624 KB
testcase_37 AC 1,438 ms
286,724 KB
testcase_38 AC 1,485 ms
286,524 KB
testcase_39 AC 1,527 ms
287,492 KB
testcase_40 AC 1,590 ms
286,956 KB
testcase_41 AC 1,572 ms
287,120 KB
testcase_42 AC 1,906 ms
287,360 KB
testcase_43 AC 323 ms
240,792 KB
testcase_44 AC 347 ms
240,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

# sys.setrecursionlimit(1000005)
# sys.set_int_max_str_digits(200005)
int1 = lambda x: int(x)-1
pDB = lambda *x: print(*x, end="\n", file=sys.stderr)
p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr)
def II(): return int(sys.stdin.readline())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def SI(): return sys.stdin.readline().rstrip()

# dij = [(0, 1), (-1, 0), (0, -1), (1, 0)]
dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
# inf = -1-(-1 << 31)
inf = -1-(-1 << 63)
# md = 10**9+7
md = 998244353

class Sieve:
    def __init__(self, n):
        self.plist = [2]
        min_prime_factor = [2, 0]*(n//2+1)
        for x in range(3, n+1, 2):
            if min_prime_factor[x] == 0:
                min_prime_factor[x] = x
                self.plist.append(x)
                if x**2 > n: continue
                for y in range(x**2, n+1, 2*x):
                    if min_prime_factor[y] == 0:
                        min_prime_factor[y] = x
        self.min_prime_factor = min_prime_factor

    def isprime(self, x):
        return self.min_prime_factor[x] == x

    def pf(self, x):
        pp, ee = [], []
        while x > 1:
            mpf = self.min_prime_factor[x]
            if pp and mpf == pp[-1]:
                ee[-1] += 1
            else:
                pp.append(mpf)
                ee.append(1)
            x //= mpf
        return pp, ee

    # unsorted
    def factor(self, a):
        ff = [1]
        pp, ee = self.pf(a)
        for p, e in zip(pp, ee):
            ff, gg = [], ff
            w = p
            for _ in range(e):
                for f in gg: ff.append(f*w)
                w *= p
            ff += gg
        return ff

class UnionFind:
    def __init__(self, n):
        self._tree = [-1]*n
        # number of connected component
        self.cnt = n

    def root(self, u):
        stack = []
        while self._tree[u] >= 0:
            stack.append(u)
            u = self._tree[u]
        for v in stack: self._tree[v] = u
        return u

    def same(self, u, v):
        return self.root(u) == self.root(v)

    def merge(self, u, v):
        u, v = self.root(u), self.root(v)
        if u == v: return False
        if self._tree[u] > self._tree[v]: u, v = v, u
        self._tree[u] += self._tree[v]
        self._tree[v] = u
        self.cnt -= 1
        return True

    # size of connected component
    def size(self, u):
        return -self._tree[self.root(u)]

from collections import defaultdict

sv=Sieve(100000)

n=II()
aa=set()
ans=0
for a in LI():
    if a in aa:
        ans+=a
    else:
        aa.add(a)

g2a=defaultdict(lambda :200000)
aa=sorted(aa)
a2i={}
lab=[]
for i,a in enumerate(aa):
    a2i[a]=i
    ff=sv.factor(a)
    for f in ff:
        if f in g2a:
            b=g2a[f]
            l=a*b//f
            lab.append((l,a,b))
        g2a[f]=min(g2a[f],a)

lab.sort(key=lambda x:x[0])
uf=UnionFind(len(aa))
for l,a,b in lab:
    i=a2i[a]
    j=a2i[b]
    if uf.merge(i,j):ans+=l

print(ans)
0