結果

問題 No.1917 LCMST
ユーザー ytftytft
提出日時 2022-04-30 14:07:44
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,400 bytes
コンパイル時間 790 ms
コンパイル使用メモリ 86,784 KB
実行使用メモリ 88,088 KB
最終ジャッジ日時 2023-09-12 06:50:26
合計ジャッジ時間 13,344 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,004 KB
testcase_01 AC 69 ms
71,360 KB
testcase_02 AC 71 ms
71,280 KB
testcase_03 AC 237 ms
87,708 KB
testcase_04 AC 261 ms
88,088 KB
testcase_05 AC 272 ms
87,640 KB
testcase_06 AC 266 ms
87,248 KB
testcase_07 AC 258 ms
87,392 KB
testcase_08 AC 78 ms
71,440 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class unionFind:
    def __init__(self,N):
        self.N=N
        self.parent=[-1 for i in range(N)]
        self.size=[1 for i in range(N)]
    def find(self,x):
        path=[x]
        while self.parent[path[-1]]!=-1:
            path.append(self.parent[path[-1]])
        for i in path[:-1]:
            self.parent[i]=path[-1]
        return path[-1]
    def unite(self,x,y):
        roots=sorted([self.find(x),self.find(y)],key=lambda _:self.parent[_])
        if roots[0]!=roots[1]:
            self.parent[roots[0]]=roots[1]
            self.size[roots[1]]+=self.size[roots[0]]
def gcd(a,b):
    return gcd(a%b,b%a) if min(a,b)*(a-b) else max(a,b)
def lcm(a,b):
	return a*b//gcd(a,b)
def divisors(n):
    ans=[]
    for i in range(1,n+1):
        if n%i==0:
            ans.append(i)
        if i**2>n:
            break
    k=len(ans)
    for i in range(k-1,-1,-1):
        if n//ans[i]>ans[k-1]:
            ans.append(n//ans[i])
    return ans
N=int(input())
A=list(map(int,input().split()))
M=max(A)
dp=[[] for i in range(M)]
for i in range(N):
	for j in divisors(A[i]):
		dp[j-1].append(i)
cand=[]
for i in range(M):
	dp[i]=sorted(dp[i],key=lambda x:A[x])
	for j in dp[i][1:]:
		cand.append([dp[i][0],j])
cand=sorted(cand,key=lambda x:lcm(A[x[0]],A[x[1]]))
U=unionFind(N)
ans=0
for i in cand:
	if U.find(i[0])!=U.find(i[1]):
		U.unite(i[0],i[1])
		ans+=lcm(A[i[0]],A[i[1]])
print(ans)
0