結果

問題 No.1917 LCMST
ユーザー titiatitia
提出日時 2022-10-12 04:20:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 2,856 ms / 4,000 ms
コード長 1,206 bytes
コンパイル時間 220 ms
コンパイル使用メモリ 11,088 KB
実行使用メモリ 229,056 KB
最終ジャッジ日時 2023-09-08 05:02:34
合計ジャッジ時間 70,530 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
14,236 KB
testcase_01 AC 185 ms
14,136 KB
testcase_02 AC 180 ms
14,240 KB
testcase_03 AC 189 ms
15,220 KB
testcase_04 AC 193 ms
15,124 KB
testcase_05 AC 191 ms
15,200 KB
testcase_06 AC 195 ms
15,288 KB
testcase_07 AC 196 ms
15,312 KB
testcase_08 AC 179 ms
14,104 KB
testcase_09 AC 2,856 ms
219,796 KB
testcase_10 AC 2,665 ms
222,340 KB
testcase_11 AC 2,729 ms
220,036 KB
testcase_12 AC 2,550 ms
190,364 KB
testcase_13 AC 1,223 ms
120,156 KB
testcase_14 AC 2,659 ms
206,536 KB
testcase_15 AC 927 ms
119,960 KB
testcase_16 AC 1,355 ms
119,824 KB
testcase_17 AC 1,684 ms
135,956 KB
testcase_18 AC 2,004 ms
154,608 KB
testcase_19 AC 912 ms
124,268 KB
testcase_20 AC 768 ms
121,740 KB
testcase_21 AC 978 ms
119,232 KB
testcase_22 AC 791 ms
119,956 KB
testcase_23 AC 756 ms
120,632 KB
testcase_24 AC 911 ms
119,492 KB
testcase_25 AC 766 ms
118,764 KB
testcase_26 AC 825 ms
119,956 KB
testcase_27 AC 858 ms
119,504 KB
testcase_28 AC 877 ms
118,768 KB
testcase_29 AC 2,576 ms
228,596 KB
testcase_30 AC 2,491 ms
228,908 KB
testcase_31 AC 2,524 ms
228,736 KB
testcase_32 AC 2,545 ms
229,056 KB
testcase_33 AC 2,474 ms
228,844 KB
testcase_34 AC 534 ms
119,756 KB
testcase_35 AC 547 ms
119,124 KB
testcase_36 AC 545 ms
125,788 KB
testcase_37 AC 2,580 ms
228,892 KB
testcase_38 AC 2,413 ms
228,700 KB
testcase_39 AC 2,511 ms
228,740 KB
testcase_40 AC 2,557 ms
228,800 KB
testcase_41 AC 2,541 ms
228,916 KB
testcase_42 AC 2,601 ms
227,048 KB
testcase_43 AC 582 ms
119,152 KB
testcase_44 AC 578 ms
124,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
0