結果

問題 No.2756 GCD Teleporter
ユーザー hato336hato336
提出日時 2024-05-10 22:04:29
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,734 bytes
コンパイル時間 142 ms
コンパイル使用メモリ 82,608 KB
実行使用メモリ 154,976 KB
最終ジャッジ日時 2024-05-10 22:05:19
合計ジャッジ時間 42,737 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,344 KB
testcase_01 AC 41 ms
55,488 KB
testcase_02 AC 43 ms
55,024 KB
testcase_03 AC 40 ms
55,384 KB
testcase_04 AC 365 ms
81,772 KB
testcase_05 AC 743 ms
90,812 KB
testcase_06 AC 1,534 ms
111,492 KB
testcase_07 AC 1,309 ms
105,048 KB
testcase_08 AC 1,217 ms
103,316 KB
testcase_09 AC 1,078 ms
99,940 KB
testcase_10 AC 1,470 ms
108,812 KB
testcase_11 AC 1,779 ms
116,244 KB
testcase_12 AC 626 ms
86,760 KB
testcase_13 AC 1,971 ms
113,812 KB
testcase_14 AC 1,795 ms
110,640 KB
testcase_15 AC 606 ms
86,460 KB
testcase_16 AC 1,152 ms
97,912 KB
testcase_17 AC 1,315 ms
101,568 KB
testcase_18 AC 565 ms
86,428 KB
testcase_19 TLE -
testcase_20 AC 1,666 ms
109,432 KB
testcase_21 AC 872 ms
92,276 KB
testcase_22 AC 1,669 ms
109,608 KB
testcase_23 AC 394 ms
82,564 KB
testcase_24 AC 1,349 ms
102,936 KB
testcase_25 AC 1,414 ms
103,164 KB
testcase_26 AC 1,563 ms
106,900 KB
testcase_27 AC 1,615 ms
109,648 KB
testcase_28 AC 1,036 ms
92,504 KB
testcase_29 AC 1,399 ms
99,716 KB
testcase_30 AC 1,779 ms
109,872 KB
testcase_31 AC 874 ms
89,624 KB
testcase_32 AC 931 ms
109,744 KB
testcase_33 AC 426 ms
88,156 KB
testcase_34 AC 167 ms
79,432 KB
testcase_35 AC 620 ms
108,432 KB
testcase_36 AC 582 ms
107,764 KB
testcase_37 AC 39 ms
55,588 KB
testcase_38 AC 1,230 ms
111,436 KB
testcase_39 AC 1,922 ms
154,976 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys
def gcd(a, b):
    while a:
        a, b = b%a, a
    return b


def is_prime(n):
    if n == 2:
        return 1
    if n == 1 or n%2 == 0:
        return 0

    m = n - 1
    lsb = m & -m
    s = lsb.bit_length()-1
    d = m // lsb

    test_numbers = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37]

    for a in test_numbers:
        if a == n:
            continue
        x = pow(a,d,n)
        r = 0
        if x == 1:
            continue
        while x != m:
            x = pow(x,2,n)
            r += 1
            if x == 1 or r == s:
                return 0
    return 1


def find_prime_factor(n):
    if n%2 == 0:
        return 2

    m = int(n**0.125)+1

    for c in range(1,n):
        f = lambda a: (pow(a,2,n)+c)%n
        y = 0
        g = q = r = 1
        k = 0
        while g == 1:
            x = y
            while k < 3*r//4:
                y = f(y)
                k += 1
            while k < r and g == 1:
                ys = y
                for _ in range(min(m, r-k)):
                    y = f(y)
                    q = q*abs(x-y)%n
                g = gcd(q,n)
                k += m
            k = r
            r *= 2
        if g == n:
            g = 1
            y = ys
            while g == 1:
                y = f(y)
                g = gcd(abs(x-y),n)
        if g == n:
            continue
        if is_prime(g):
            return g
        elif is_prime(n//g):
            return n//g
        else:
            return find_prime_factor(g)


def factorize(n):
    res = {}
    while not is_prime(n) and n > 1:  # nが合成数である間nの素因数の探索を繰り返す
        p = find_prime_factor(n)
        s = 0
        while n%p == 0:  # nが素因数pで割れる間割り続け、出力に追加
            n //= p
            s += 1
        res[p] = s
    if n > 1:  # n>1であればnは素数なので出力に追加
        res[n] = 1
    return res

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
n = int(input())
a = list(map(int,input().split()))
check = [10**18 for i in range(n)]
uf = UnionFind(n)
c = collections.defaultdict(list)
for i in range(n):
    x = factorize(a[i])
    for j in x.keys():
        c[j].append(i)
for i in c:
    for j in range(len(c[i])-1):
        uf.union(c[i][j],c[i][j+1])
    check[uf.find(c[i][0])] = min(check[uf.find(c[i][0])],i)
ans = 10**18
for i in range(n):
    if uf.find(i) == i:
        ans = min(ans,check[i])
print(min(ans * uf.group_count() - ans,2 * uf.group_count()))
0