結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2022-02-28 13:40:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,942 ms / 4,000 ms
コード長 1,281 bytes
コンパイル時間 512 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 324,456 KB
最終ジャッジ日時 2023-09-10 16:54:18
合計ジャッジ時間 45,197 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 104 ms
80,184 KB
testcase_01 AC 103 ms
80,120 KB
testcase_02 AC 103 ms
80,088 KB
testcase_03 AC 127 ms
81,112 KB
testcase_04 AC 125 ms
80,872 KB
testcase_05 AC 127 ms
81,232 KB
testcase_06 AC 127 ms
81,252 KB
testcase_07 AC 126 ms
81,284 KB
testcase_08 AC 103 ms
80,056 KB
testcase_09 AC 1,668 ms
317,160 KB
testcase_10 AC 1,670 ms
321,532 KB
testcase_11 AC 1,601 ms
320,352 KB
testcase_12 AC 1,264 ms
304,432 KB
testcase_13 AC 582 ms
242,112 KB
testcase_14 AC 1,399 ms
315,280 KB
testcase_15 AC 469 ms
240,872 KB
testcase_16 AC 618 ms
242,112 KB
testcase_17 AC 764 ms
242,000 KB
testcase_18 AC 912 ms
259,752 KB
testcase_19 AC 517 ms
241,516 KB
testcase_20 AC 452 ms
233,340 KB
testcase_21 AC 544 ms
240,752 KB
testcase_22 AC 452 ms
233,832 KB
testcase_23 AC 458 ms
233,356 KB
testcase_24 AC 522 ms
241,404 KB
testcase_25 AC 462 ms
234,080 KB
testcase_26 AC 490 ms
238,736 KB
testcase_27 AC 497 ms
240,860 KB
testcase_28 AC 518 ms
241,396 KB
testcase_29 AC 1,829 ms
324,064 KB
testcase_30 AC 1,942 ms
323,820 KB
testcase_31 AC 1,751 ms
323,796 KB
testcase_32 AC 1,763 ms
324,132 KB
testcase_33 AC 1,752 ms
323,792 KB
testcase_34 AC 287 ms
217,012 KB
testcase_35 AC 289 ms
216,936 KB
testcase_36 AC 287 ms
216,852 KB
testcase_37 AC 1,732 ms
323,916 KB
testcase_38 AC 1,770 ms
323,988 KB
testcase_39 AC 1,762 ms
324,248 KB
testcase_40 AC 1,741 ms
324,168 KB
testcase_41 AC 1,730 ms
324,456 KB
testcase_42 AC 1,722 ms
324,152 KB
testcase_43 AC 305 ms
216,264 KB
testcase_44 AC 316 ms
220,128 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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 MST(n,edge):
  uf=UnionFind(n)
  ans=0
  for cost,u,v in edge:
    if not uf.same(u,v):
      ans+=cost
      uf.union(u,v)
  return ans

import math
import random
n=int(input())
a=list(map(int,input().split()))
#n=5*10**5
#a=[random.randint(50000,10**5) for i in range(n)]
m=10**5
d=[-1]*(m+1)
cnt=[0]*(m+1)

ans=0
for i in a:
  if cnt[i]==0:
    cnt[i]=1
  else:
    ans+=i

edge=[]
for i in range(1,m+1):
  for j in range(i,m+1,i):
    if cnt[j]==1:
      if d[i]==-1:
        d[i]=j
      else:
        edge.append((d[i]*j//i,d[i],j))

edge.sort(key=lambda x:x[0])
ans+=MST(m+1,edge)
print(ans)
0