結果
問題 | No.1917 LCMST |
ユーザー | mkawa2 |
提出日時 | 2023-11-14 14:11:45 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 3,238 bytes |
コンパイル時間 | 269 ms |
コンパイル使用メモリ | 13,184 KB |
実行使用メモリ | 171,372 KB |
最終ジャッジ日時 | 2024-09-26 03:35:26 |
合計ジャッジ時間 | 9,975 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 42 ms
171,372 KB |
testcase_01 | AC | 45 ms
12,160 KB |
testcase_02 | AC | 46 ms
12,288 KB |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | AC | 42 ms
12,288 KB |
testcase_09 | TLE | - |
testcase_10 | TLE | - |
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 | -- | - |
ソースコード
import sys # sys.setrecursionlimit(1000005) # sys.set_int_max_str_digits(200005) int1 = lambda x: int(x)-1 pDB = lambda *x: print(*x, end="\n", file=sys.stderr) p2D = lambda x: print(*x, sep="\n", end="\n\n", file=sys.stderr) def II(): return int(sys.stdin.readline()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def LI1(): return list(map(int1, sys.stdin.readline().split())) def LLI1(rows_number): return [LI1() for _ in range(rows_number)] def SI(): return sys.stdin.readline().rstrip() # dij = [(0, 1), (-1, 0), (0, -1), (1, 0)] dij = [(0, 1), (-1, 0), (0, -1), (1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)] # inf = -1-(-1 << 31) inf = -1-(-1 << 63) # md = 10**9+7 md = 998244353 class Sieve: def __init__(self, n): self.plist = [2] min_prime_factor = [2, 0]*(n//2+1) for x in range(3, n+1, 2): if min_prime_factor[x] == 0: min_prime_factor[x] = x self.plist.append(x) if x**2 > n: continue for y in range(x**2, n+1, 2*x): if min_prime_factor[y] == 0: min_prime_factor[y] = x self.min_prime_factor = min_prime_factor def isprime(self, x): return self.min_prime_factor[x] == x def pf(self, x): pp, ee = [], [] while x > 1: mpf = self.min_prime_factor[x] if pp and mpf == pp[-1]: ee[-1] += 1 else: pp.append(mpf) ee.append(1) x //= mpf return pp, ee # unsorted def factor(self, a): ff = [1] pp, ee = self.pf(a) for p, e in zip(pp, ee): ff, gg = [], ff w = p for _ in range(e): for f in gg: ff.append(f*w) w *= p ff += gg return ff class UnionFind: def __init__(self, n): self._tree = [-1]*n # number of connected component self.cnt = n def root(self, u): stack = [] while self._tree[u] >= 0: stack.append(u) u = self._tree[u] for v in stack: self._tree[v] = u return u def same(self, u, v): return self.root(u) == self.root(v) def merge(self, u, v): u, v = self.root(u), self.root(v) if u == v: return False if self._tree[u] > self._tree[v]: u, v = v, u self._tree[u] += self._tree[v] self._tree[v] = u self.cnt -= 1 return True # size of connected component def size(self, u): return -self._tree[self.root(u)] from collections import defaultdict sv=Sieve(100000) n=II() aa=set() ans=0 for a in LI(): if a in aa: ans+=a else: aa.add(a) g2a=defaultdict(lambda :200000) aa=list(aa) a2i={} lab=[] for i,a in enumerate(aa): a2i[a]=i ff=sv.factor(a) for f in ff: if f in g2a: b=g2a[f] l=a*b//f lab.append((l,a,b)) g2a[f]=min(g2a[f],a) lab.sort(key=lambda x:x[0]) uf=UnionFind(len(aa)) for l,a,b in lab: i=a2i[a] j=a2i[b] if uf.merge(i,j):ans+=l print(ans)