結果

問題 No.14 最小公倍数ソート
ユーザー tnodino
提出日時 2022-03-06 01:01:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 523 ms / 5,000 ms
コード長 984 bytes
コンパイル時間 269 ms
コンパイル使用メモリ 82,136 KB
実行使用メモリ 87,348 KB
最終ジャッジ日時 2024-07-20 08:31:53
合計ジャッジ時間 9,266 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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