結果

問題 No.2756 GCD Teleporter
ユーザー detteiuudetteiuu
提出日時 2024-09-02 22:14:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 2,786 bytes
コンパイル時間 395 ms
コンパイル使用メモリ 82,456 KB
実行使用メモリ 109,948 KB
最終ジャッジ日時 2024-09-02 22:14:17
合計ジャッジ時間 10,990 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
66,744 KB
testcase_01 AC 61 ms
66,292 KB
testcase_02 AC 62 ms
66,388 KB
testcase_03 AC 63 ms
67,960 KB
testcase_04 AC 131 ms
80,584 KB
testcase_05 AC 197 ms
86,116 KB
testcase_06 AC 304 ms
102,380 KB
testcase_07 AC 262 ms
97,920 KB
testcase_08 AC 250 ms
95,252 KB
testcase_09 AC 231 ms
93,276 KB
testcase_10 AC 283 ms
100,064 KB
testcase_11 AC 320 ms
105,824 KB
testcase_12 AC 156 ms
83,384 KB
testcase_13 AC 347 ms
106,148 KB
testcase_14 AC 298 ms
105,916 KB
testcase_15 AC 160 ms
83,736 KB
testcase_16 AC 225 ms
93,116 KB
testcase_17 AC 249 ms
95,264 KB
testcase_18 AC 153 ms
83,040 KB
testcase_19 AC 331 ms
109,172 KB
testcase_20 AC 285 ms
106,224 KB
testcase_21 AC 180 ms
91,912 KB
testcase_22 AC 282 ms
109,204 KB
testcase_23 AC 125 ms
81,344 KB
testcase_24 AC 233 ms
100,272 KB
testcase_25 AC 255 ms
103,096 KB
testcase_26 AC 274 ms
105,592 KB
testcase_27 AC 274 ms
109,160 KB
testcase_28 AC 217 ms
93,084 KB
testcase_29 AC 266 ms
100,192 KB
testcase_30 AC 325 ms
109,392 KB
testcase_31 AC 205 ms
89,968 KB
testcase_32 AC 182 ms
109,864 KB
testcase_33 AC 124 ms
90,204 KB
testcase_34 AC 122 ms
81,768 KB
testcase_35 AC 220 ms
108,248 KB
testcase_36 AC 226 ms
107,756 KB
testcase_37 AC 63 ms
67,576 KB
testcase_38 AC 312 ms
109,652 KB
testcase_39 AC 357 ms
109,948 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self,n):
        self.n = n
        self.parent_size = [-1]*n
 
    def leader(self,a):
        if self.parent_size[a] < 0:
            return a
        self.parent_size[a] = self.leader(self.parent_size[a])
        return self.parent_size[a]
 
    def merge(self,a,b):
        x, y = self.leader(a), self.leader(b)
        if x == y:
            return 
        if abs(self.parent_size[x]) < abs(self.parent_size[y]):
            x, y = y, x
        self.parent_size[x] += self.parent_size[y]
        self.parent_size[y] = x
        return 
 
    def same(self,a,b):
        return self.leader(a) == self.leader(b)
 
    def size(self,a):
        return abs(self.parent_size[self.leader(a)])
 
    def groups(self):
        result = [[] for _ in range(self.n)]
        for i in range(self.n):
            result[self.leader(i)].append(i)
        return [r for r in result if r != []]

class Eratosthenes:
    def __init__(self, n):
        self.isPrime = [True]*(n+1)
        self.minfactor = [-1]*(n+1)
        self.isPrime[0], self.isPrime[1] = False, False
        self.minfactor[1] = 1
        for i in range(2, n+1):
            if self.isPrime[i]:
                self.minfactor[i] = i
                for j in range(i*2, n+1, i):
                    self.isPrime[j] = False
                    if self.minfactor[j] == -1:
                        self.minfactor[j] = i
    
    def factorize(self, n):
        factor = []
        while n > 1:
            p = self.minfactor[n]
            cnt = 0
            while self.minfactor[n] == p:
                n //= p
                cnt += 1
            factor.append((p, cnt))
        return factor
    
    def divisor(self, n):
        ans = [1]
        pf = self.factorize(n)
        for p, c in pf:
            L = len(ans)
            for i in range(L):
                v = 1
                for _ in range(c):
                    v *= p
                    ans.append(ans[i]*v)
        return sorted(ans)

N = int(input())
A = list(map(int, input().split()))

def compress(S):
    S = sorted(S)
    D = dict()
    for i in range(len(S)):
        D[S[i]] = i
    return D

E = Eratosthenes(10**5*2)
S = set()
for i in range(N):
    F = E.factorize(A[i])
    for n, c in F:
        S.add(n)
D = compress(S)

UF = UnionFind(N+len(D))
cnt2, cnt3 = False, False
for i in range(N):
    F = E.factorize(A[i])
    for n, c in F:
        UF.merge(i, N+D[n])
        if n == 2:
            cnt2 = True
        elif n == 3:
            cnt3 = True

S = set()
for i in range(N):
    S.add(UF.leader(i))
L = len(S)

if L == 1:
    exit(print(0))

ans = 10**18
if cnt2:
    ans = min(ans, 2*(L-1))
else:
    ans = min(ans, 2*L)
if cnt3:
    ans = min(ans, 3*(L-1))
else:
    ans = min(ans, 3*L)

print(ans)
0