結果

問題 No.1059 素敵な集合
ユーザー brthyyjpbrthyyjp
提出日時 2020-05-24 06:10:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,077 ms / 2,000 ms
コード長 1,234 bytes
コンパイル時間 262 ms
コンパイル使用メモリ 87,016 KB
実行使用メモリ 160,112 KB
最終ジャッジ日時 2023-09-30 15:37:52
合計ジャッジ時間 8,817 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 79 ms
71,580 KB
testcase_01 AC 72 ms
71,360 KB
testcase_02 AC 375 ms
94,944 KB
testcase_03 AC 78 ms
71,252 KB
testcase_04 AC 77 ms
71,568 KB
testcase_05 AC 76 ms
71,420 KB
testcase_06 AC 320 ms
89,868 KB
testcase_07 AC 308 ms
89,432 KB
testcase_08 AC 340 ms
94,844 KB
testcase_09 AC 219 ms
81,932 KB
testcase_10 AC 385 ms
100,356 KB
testcase_11 AC 290 ms
90,064 KB
testcase_12 AC 232 ms
85,028 KB
testcase_13 AC 429 ms
106,572 KB
testcase_14 AC 148 ms
79,112 KB
testcase_15 AC 646 ms
122,332 KB
testcase_16 AC 329 ms
93,532 KB
testcase_17 AC 341 ms
92,952 KB
testcase_18 AC 113 ms
92,068 KB
testcase_19 AC 1,077 ms
160,112 KB
testcase_20 AC 1,015 ms
156,420 KB
testcase_21 AC 642 ms
120,840 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def main():
    l, r = map(int, input().split())

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

    n = r-l+1

    edge = [[] for _ in range(n)]
    used = set()
    for i in range(l, r+1):
        x = i-l
        j = i
        if i not in used:
            while j <= r:
                y = j-l
                if y != x:
                    edge[x].append((0, y))
                    edge[y].append((0, x))
                    used.add(j)
                j += i
        if i != r:
            if i+1 not in used:
                y = x+1
                edge[x].append((1, y))
                #edge[y].append((1, x))

    import heapq
    def prim_heap():
        used = [True]*n
        edgelist = []
        for e in edge[0]:
            heapq.heappush(edgelist, e)
            used[0] = False
        res = 0
        while len(edgelist) != 0:
            minedge = heapq.heappop(edgelist)
            if not  used[minedge[1]]:
                continue
            v = minedge[1]
            used[v] = False
            for e in edge[v]:
                if used[e[1]]:
                    heapq.heappush(edgelist, e)
            res += minedge[0]
        return res

    print(prim_heap())

if __name__ == '__main__':
    main()
0