結果
| 問題 |
No.1917 LCMST
|
| コンテスト | |
| ユーザー |
titia
|
| 提出日時 | 2022-10-12 04:20:13 |
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
| 結果 |
AC
|
| 実行時間 | 3,566 ms / 4,000 ms |
| コード長 | 1,206 bytes |
| コンパイル時間 | 192 ms |
| コンパイル使用メモリ | 12,800 KB |
| 実行使用メモリ | 228,572 KB |
| 最終ジャッジ日時 | 2024-06-25 22:16:58 |
| 合計ジャッジ時間 | 80,059 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 42 |
ソースコード
import sys
input = sys.stdin.readline
from collections import Counter
from math import gcd
from operator import itemgetter
def lcm(x,y):
return x*y//gcd(x,y)
N=int(input())
A=list(map(int,input().split()))
C=Counter(A)
ANS=0
LIST=[0]*(10**5+1)
for c in C:
LIST[c]=1
ANS+=c*(C[c]-1)
E=[]
for i in range(1,10**5+1):
MIN=-1
for j in range(i,10**5+1,i):
if LIST[j]==1:
if MIN==-1:
MIN=j
else:
E.append((lcm(MIN,j),j,MIN))
# UnionFind
Group = [i for i in range(10**5+1)] # グループ分け
Nodes = [1]*(10**5+1) # 各グループのノードの数
def find(x):
while Group[x] != x:
x=Group[x]
return x
def Union(x,y):
if find(x) != find(y):
if Nodes[find(x)] < Nodes[find(y)]:
Nodes[find(y)] += Nodes[find(x)]
Nodes[find(x)] = 0
Group[find(x)] = find(y)
else:
Nodes[find(x)] += Nodes[find(y)]
Nodes[find(y)] = 0
Group[find(y)] = find(x)
E.sort(key=itemgetter(0))
for k,x,y in E:
if find(x)==find(y):
continue
else:
ANS+=k
Union(x,y)
print(ANS)
titia