結果
問題 | No.2756 GCD Teleporter |
ユーザー | mkawa2 |
提出日時 | 2024-05-10 23:19:09 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 3,022 bytes |
コンパイル時間 | 179 ms |
コンパイル使用メモリ | 82,972 KB |
実行使用メモリ | 145,944 KB |
最終ジャッジ日時 | 2024-12-20 07:35:22 |
合計ジャッジ時間 | 10,015 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 57 ms
65,184 KB |
testcase_01 | AC | 55 ms
65,492 KB |
testcase_02 | AC | 57 ms
66,520 KB |
testcase_03 | AC | 54 ms
65,816 KB |
testcase_04 | AC | 111 ms
79,732 KB |
testcase_05 | AC | 168 ms
85,108 KB |
testcase_06 | AC | 297 ms
100,508 KB |
testcase_07 | AC | 252 ms
95,400 KB |
testcase_08 | AC | 239 ms
93,524 KB |
testcase_09 | AC | 231 ms
91,160 KB |
testcase_10 | AC | 288 ms
97,944 KB |
testcase_11 | AC | 318 ms
108,500 KB |
testcase_12 | AC | 149 ms
82,348 KB |
testcase_13 | AC | 330 ms
106,888 KB |
testcase_14 | AC | 303 ms
103,932 KB |
testcase_15 | AC | 156 ms
82,816 KB |
testcase_16 | AC | 214 ms
91,512 KB |
testcase_17 | AC | 234 ms
93,392 KB |
testcase_18 | AC | 146 ms
82,132 KB |
testcase_19 | AC | 320 ms
111,016 KB |
testcase_20 | AC | 262 ms
103,792 KB |
testcase_21 | AC | 183 ms
89,580 KB |
testcase_22 | AC | 270 ms
105,908 KB |
testcase_23 | AC | 121 ms
80,268 KB |
testcase_24 | AC | 227 ms
97,952 KB |
testcase_25 | AC | 242 ms
99,140 KB |
testcase_26 | AC | 252 ms
102,508 KB |
testcase_27 | AC | 275 ms
106,164 KB |
testcase_28 | AC | 202 ms
91,404 KB |
testcase_29 | AC | 263 ms
97,648 KB |
testcase_30 | AC | 303 ms
106,388 KB |
testcase_31 | AC | 182 ms
88,432 KB |
testcase_32 | AC | 164 ms
104,580 KB |
testcase_33 | AC | 107 ms
88,004 KB |
testcase_34 | AC | 109 ms
80,844 KB |
testcase_35 | AC | 215 ms
101,428 KB |
testcase_36 | AC | 228 ms
102,340 KB |
testcase_37 | RE | - |
testcase_38 | AC | 271 ms
113,712 KB |
testcase_39 | AC | 407 ms
145,944 KB |
ソースコード
import sys # sys.setrecursionlimit(200005) # 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 << 62) # 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 sv=Sieve(200005) # pp=sv.plist from collections import defaultdict 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)] n=II() aa=LI() p2i=defaultdict(list) for i,a in enumerate(aa): pp,ee=sv.pf(a) for p in pp:p2i[p].append(i) uf=UnionFind(n) ans=0 for ii in p2i.values(): for i,j in zip(ii,ii[1:]):uf.merge(i,j) ans=min(2*uf.cnt,min(p2i)*(uf.cnt-1)) print(ans)