結果
問題 | No.1917 LCMST |
ユーザー |
![]() |
提出日時 | 2024-08-29 19:55:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 3,964 ms / 4,000 ms |
コード長 | 1,369 bytes |
コンパイル時間 | 595 ms |
コンパイル使用メモリ | 82,424 KB |
実行使用メモリ | 302,988 KB |
最終ジャッジ日時 | 2024-08-29 19:56:38 |
合計ジャッジ時間 | 73,519 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
def gcd(a, b):while a != 0:b %= aif b == 0: return aa %= breturn bclass UnionFind():def __init__(self,n):self.data = [-1 for i in range(n)]def find(self,x):stack = []while self.data[x] >= 0:stack.append(x)x = self.data[x]while len(stack):y = stack.pop()self.data[y] = xreturn xdef unite(self,x,y):x,y = self.find(x),self.find(y)if x == y:returnif self.data[x] > self.data[y]:x,y = y,xself.data[x] += self.data[y]self.data[y] = xreturndef same(self,x,y):return self.find(x) == self.find(y)def size(self,x):return -self.data[self.find(x)]N = int(input())A = list(map(int,input().split()))M = 100000B = [0] * (M + 1)m = min(A)for a in A:B[a] += 1ans = 0for a in range(1,M + 1):if B[a] == 0:continueans += a * (B[a] - 1)B[a] = 1A = []for a in range(1,M + 1):if B[a]:A.append(a)N = len(A)E = []for i in range(1,M + 1):X = []for j in range(1,M + 1):if i * j > M:breakif B[i * j]:X.append(i * j)if len(X) == 0:continuex = X[0]for i in range(1,len(X)):l = (x * X[i]) // gcd(x,X[i])E.append((l,x - 1,X[i] - 1))E.sort()uf = UnionFind(M)for c,u,v in E:if uf.same(u,v):continueuf.unite(u,v)ans += cprint(ans)