結果

問題 No.2756 GCD Teleporter
ユーザー detteiuudetteiuu
提出日時 2024-09-02 22:12:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,824 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 82,536 KB
実行使用メモリ 123,748 KB
最終ジャッジ日時 2024-09-02 22:13:07
合計ジャッジ時間 11,112 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
66,120 KB
testcase_01 AC 61 ms
66,676 KB
testcase_02 AC 61 ms
67,088 KB
testcase_03 AC 82 ms
67,412 KB
testcase_04 AC 131 ms
80,872 KB
testcase_05 AC 187 ms
93,056 KB
testcase_06 AC 317 ms
118,416 KB
testcase_07 AC 274 ms
110,192 KB
testcase_08 AC 295 ms
108,136 KB
testcase_09 AC 242 ms
104,352 KB
testcase_10 AC 296 ms
113,956 KB
testcase_11 AC 343 ms
123,748 KB
testcase_12 AC 154 ms
86,024 KB
testcase_13 AC 355 ms
118,708 KB
testcase_14 AC 312 ms
118,144 KB
testcase_15 AC 163 ms
86,628 KB
testcase_16 AC 233 ms
102,108 KB
testcase_17 AC 280 ms
105,156 KB
testcase_18 AC 155 ms
85,904 KB
testcase_19 AC 344 ms
121,472 KB
testcase_20 AC 278 ms
109,044 KB
testcase_21 AC 178 ms
93,796 KB
testcase_22 AC 305 ms
112,160 KB
testcase_23 AC 122 ms
81,624 KB
testcase_24 AC 238 ms
103,472 KB
testcase_25 AC 249 ms
106,116 KB
testcase_26 AC 274 ms
109,388 KB
testcase_27 AC 296 ms
112,452 KB
testcase_28 AC 215 ms
100,588 KB
testcase_29 AC 268 ms
108,872 KB
testcase_30 AC 306 ms
118,824 KB
testcase_31 AC 215 ms
96,064 KB
testcase_32 AC 182 ms
109,460 KB
testcase_33 AC 123 ms
89,788 KB
testcase_34 AC 121 ms
81,548 KB
testcase_35 AC 221 ms
108,468 KB
testcase_36 AC 221 ms
107,808 KB
testcase_37 WA -
testcase_38 AC 323 ms
109,908 KB
testcase_39 AC 347 ms
109,828 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()))

if set(A) == {1}:
    exit(print(0))

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