結果

問題 No.2756 GCD Teleporter
ユーザー mkawa2mkawa2
提出日時 2024-05-10 23:19:09
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,022 bytes
コンパイル時間 133 ms
コンパイル使用メモリ 82,524 KB
実行使用メモリ 146,072 KB
最終ジャッジ日時 2024-05-10 23:19:20
合計ジャッジ時間 9,240 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
65,368 KB
testcase_01 AC 49 ms
66,608 KB
testcase_02 AC 48 ms
66,664 KB
testcase_03 AC 50 ms
66,536 KB
testcase_04 AC 101 ms
80,004 KB
testcase_05 AC 162 ms
85,236 KB
testcase_06 AC 310 ms
100,448 KB
testcase_07 AC 229 ms
95,232 KB
testcase_08 AC 221 ms
93,492 KB
testcase_09 AC 199 ms
91,576 KB
testcase_10 AC 253 ms
98,100 KB
testcase_11 AC 324 ms
108,604 KB
testcase_12 AC 132 ms
82,628 KB
testcase_13 AC 289 ms
106,664 KB
testcase_14 AC 267 ms
103,888 KB
testcase_15 AC 139 ms
83,088 KB
testcase_16 AC 191 ms
91,508 KB
testcase_17 AC 232 ms
93,504 KB
testcase_18 AC 132 ms
82,160 KB
testcase_19 AC 295 ms
110,888 KB
testcase_20 AC 248 ms
104,284 KB
testcase_21 AC 156 ms
89,540 KB
testcase_22 AC 272 ms
105,624 KB
testcase_23 AC 109 ms
80,720 KB
testcase_24 AC 213 ms
97,852 KB
testcase_25 AC 218 ms
99,136 KB
testcase_26 AC 238 ms
102,024 KB
testcase_27 AC 270 ms
106,520 KB
testcase_28 AC 171 ms
91,652 KB
testcase_29 AC 230 ms
97,712 KB
testcase_30 AC 269 ms
104,448 KB
testcase_31 AC 162 ms
88,276 KB
testcase_32 AC 148 ms
104,448 KB
testcase_33 AC 99 ms
88,380 KB
testcase_34 AC 107 ms
80,416 KB
testcase_35 AC 189 ms
102,040 KB
testcase_36 AC 200 ms
102,436 KB
testcase_37 RE -
testcase_38 AC 249 ms
113,848 KB
testcase_39 AC 391 ms
146,072 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=min(2*uf.cnt,min(p2i)*(uf.cnt-1))
print(ans)
0