結果

問題 No.1917 LCMST
ユーザー shinichishinichi
提出日時 2023-01-29 17:26:22
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,124 bytes
コンパイル時間 1,015 ms
コンパイル使用メモリ 85,680 KB
実行使用メモリ 83,808 KB
最終ジャッジ日時 2023-09-12 00:36:51
合計ジャッジ時間 13,272 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
82,128 KB
testcase_01 AC 145 ms
81,932 KB
testcase_02 AC 150 ms
82,132 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 147 ms
81,836 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from math import gcd

lcm = lambda x, y: x * y // gcd(x, y)

class Eratosthenes():
    def __init__(self, size):
        self.size = size
        self.isprime = [True] * size
        self.minfactor = [-1] * size
        self.mobius = [1] * size
        self.isprime[1] = False
        self.minfactor[1] = 1
        self.eratosthenes()

    # 初めに篩にかけて,minfactorとisprimeを生成
    def eratosthenes(self):
        for p in range(2, self.size):
            if not self.isprime[p]:
                continue
            self.minfactor[p] = p
            self.mobius[p] = -1
            for q in range(p+p, self.size, p):
                self.isprime[q] = False
                if self.minfactor[q] == -1:
                    self.minfactor[q] = p
                if (q//p) % p == 0:
                    self.mobius[q] = 0
                else:
                    self.mobius[q] = -self.mobius[q]

    # 高速素因数分解
    def prime_factorize(self, number):
        assert 1 <= number <= self.size
        factors = defaultdict(int)
        while number != 1:
            factors[self.minfactor[number]] += 1
            number //= self.minfactor[number]
        return factors

    # 高速約数列挙
    def divisors(self, number):
        res = [1]
        factors = self.prime_factorize(number)
        for p, cnt in factors.items():
            # 追加前の大きさを保存
            res_size = len(res)
            for i in range(res_size):
                pp = 1
                for j in range(cnt):
                    pp *= p
                    res.append(res[i]*pp)
        return res

class UnionFind(object):
    def __init__(self, size):
        self.parent = [-1] * size

    def union(self, x, y):
        x, y = self.root(x), self.root(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        return True

    def root(self, x):
        if self.parent[x] < 0:
            return x
        self.parent[x] = self.root(self.parent[x])
        return self.parent[x]

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

    def size(self, x):
        return - self.parent[self.root(x)]

    def group_count(self):
        return [p < 0 for p in self.parent].count(True)

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

edges = []
era = Eratosthenes(2 * 10 ** 5)
minA = min(A)
minA_idx = A.index(minA)
setA = set(A)
to_idx = defaultdict(list)
for i, a in enumerate(A):
    if to_idx[a]:
        edges.append((i, to_idx[a][-1], a))
    else:
        to_idx[a].append(i)

for i, a in enumerate(A):
    divisors = era.divisors(a)
    for d in divisors:
        if to_idx[d] and to_idx[d][0] != i:
            j = to_idx[d][0]
            edges.append((i, j, lcm(A[i], A[j])))
    edges.append((i, minA_idx, lcm(a, minA)))


cost = 0
uf = UnionFind(N)
edges.sort(key= lambda x:x[-1])
for u, v, w in edges:
    if uf.same(u, v):
        continue
    uf.union(u, v)
    cost += w

print(cost)

0