結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
66,100 KB
testcase_01 AC 61 ms
67,520 KB
testcase_02 AC 63 ms
66,968 KB
testcase_03 AC 61 ms
66,940 KB
testcase_04 AC 129 ms
80,712 KB
testcase_05 AC 178 ms
86,476 KB
testcase_06 AC 328 ms
102,652 KB
testcase_07 AC 260 ms
97,408 KB
testcase_08 AC 246 ms
95,304 KB
testcase_09 AC 230 ms
93,148 KB
testcase_10 AC 303 ms
100,180 KB
testcase_11 AC 316 ms
105,584 KB
testcase_12 AC 147 ms
83,368 KB
testcase_13 AC 320 ms
106,060 KB
testcase_14 AC 321 ms
105,704 KB
testcase_15 AC 158 ms
84,240 KB
testcase_16 AC 217 ms
93,432 KB
testcase_17 AC 240 ms
95,476 KB
testcase_18 AC 150 ms
83,344 KB
testcase_19 AC 314 ms
109,372 KB
testcase_20 AC 288 ms
106,080 KB
testcase_21 AC 175 ms
91,340 KB
testcase_22 AC 283 ms
108,944 KB
testcase_23 AC 123 ms
81,252 KB
testcase_24 AC 228 ms
100,580 KB
testcase_25 AC 254 ms
103,072 KB
testcase_26 AC 265 ms
106,040 KB
testcase_27 AC 271 ms
109,680 KB
testcase_28 AC 206 ms
93,280 KB
testcase_29 AC 254 ms
99,896 KB
testcase_30 AC 309 ms
109,152 KB
testcase_31 AC 200 ms
89,832 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 118 ms
82,080 KB
testcase_35 AC 221 ms
108,208 KB
testcase_36 AC 226 ms
107,688 KB
testcase_37 AC 63 ms
67,224 KB
testcase_38 AC 312 ms
110,032 KB
testcase_39 AC 345 ms
110,260 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)

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