結果

問題 No.14 最小公倍数ソート
ユーザー szkhts
提出日時 2022-08-14 07:43:56
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 437 ms / 5,000 ms
コード長 987 bytes
コンパイル時間 87 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 22,784 KB
最終ジャッジ日時 2024-09-25 09:17:23
合計ジャッジ時間 6,863 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import sqrt, gcd
from heapq import heappop, heappush
def Divisor(N):
    ret = []
    for i in range(1,int(sqrt(N))+1):
        if N % i == 0:
            ret.append(i)
            if N // i != i:
                ret.append(N//i)
    ret.sort()
    return ret

M = 20000
N = int(input())
a = list(map(int,input().split()))
D = []
H = [[] for _ in range(M+1)]

for i in range(N):
    D.append(Divisor(a[i]))
    for k in D[i]:
        heappush(H[k], (a[i], i))
flg = [True] * N
flg[0] = False
ans = [a[0]]
idx = 0

for _ in range(N-1):
    lcm = 1<<32
    nxt = idx
    ret = []
    for k in D[idx]:
        while H[k]:
            A,P = heappop(H[k])
            if flg[P]:
                heappush(H[k], (A, P))
                break
        if not H[k]:
            continue
        L = A * a[idx] // gcd(A, a[idx])
        if L < lcm or (L == lcm and A < a[nxt]):
            lcm = L
            nxt = P
    flg[nxt] = False
    ans.append(a[nxt])
    idx = nxt

print(*ans)
0