結果
問題 |
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 %= a if b == 0: return a a %= b return b class 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] = x return x def unite(self,x,y): x,y = self.find(x),self.find(y) if x == y: return if self.data[x] > self.data[y]: x,y = y,x self.data[x] += self.data[y] self.data[y] = x return def 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 = 100000 B = [0] * (M + 1) m = min(A) for a in A: B[a] += 1 ans = 0 for a in range(1,M + 1): if B[a] == 0: continue ans += a * (B[a] - 1) B[a] = 1 A = [] 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: break if B[i * j]: X.append(i * j) if len(X) == 0: continue x = 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): continue uf.unite(u,v) ans += c print(ans)