結果

問題 No.2756 GCD Teleporter
ユーザー mkawa2mkawa2
提出日時 2024-05-10 23:23:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 439 ms / 2,000 ms
コード長 3,037 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 146,080 KB
最終ジャッジ日時 2024-12-20 07:42:54
合計ジャッジ時間 11,064 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
66,100 KB
testcase_01 AC 59 ms
66,864 KB
testcase_02 AC 56 ms
66,044 KB
testcase_03 AC 56 ms
65,100 KB
testcase_04 AC 114 ms
79,872 KB
testcase_05 AC 180 ms
84,860 KB
testcase_06 AC 325 ms
100,280 KB
testcase_07 AC 274 ms
95,352 KB
testcase_08 AC 266 ms
93,384 KB
testcase_09 AC 248 ms
91,288 KB
testcase_10 AC 318 ms
97,664 KB
testcase_11 AC 366 ms
108,624 KB
testcase_12 AC 152 ms
81,948 KB
testcase_13 AC 335 ms
106,660 KB
testcase_14 AC 322 ms
103,796 KB
testcase_15 AC 162 ms
83,104 KB
testcase_16 AC 224 ms
91,500 KB
testcase_17 AC 253 ms
93,580 KB
testcase_18 AC 156 ms
81,888 KB
testcase_19 AC 348 ms
110,756 KB
testcase_20 AC 292 ms
105,248 KB
testcase_21 AC 188 ms
89,724 KB
testcase_22 AC 298 ms
105,932 KB
testcase_23 AC 131 ms
80,716 KB
testcase_24 AC 247 ms
97,712 KB
testcase_25 AC 261 ms
99,400 KB
testcase_26 AC 286 ms
101,892 KB
testcase_27 AC 291 ms
106,268 KB
testcase_28 AC 205 ms
91,448 KB
testcase_29 AC 274 ms
97,996 KB
testcase_30 AC 326 ms
106,092 KB
testcase_31 AC 196 ms
88,328 KB
testcase_32 AC 177 ms
104,568 KB
testcase_33 AC 114 ms
88,124 KB
testcase_34 AC 115 ms
80,636 KB
testcase_35 AC 229 ms
101,552 KB
testcase_36 AC 232 ms
102,416 KB
testcase_37 AC 56 ms
65,668 KB
testcase_38 AC 289 ms
114,096 KB
testcase_39 AC 439 ms
146,080 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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=2*uf.cnt
if p2i:ans=min(ans,min(p2i)*(uf.cnt-1))
print(ans)
0