結果

問題 No.14 最小公倍数ソート
ユーザー tnodinotnodino
提出日時 2022-03-06 01:01:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 469 ms / 5,000 ms
コード長 984 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 86,816 KB
実行使用メモリ 91,076 KB
最終ジャッジ日時 2023-09-27 14:27:53
合計ジャッジ時間 8,353 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
75,424 KB
testcase_01 AC 67 ms
75,628 KB
testcase_02 AC 67 ms
75,348 KB
testcase_03 AC 190 ms
81,264 KB
testcase_04 AC 414 ms
88,252 KB
testcase_05 AC 408 ms
85,960 KB
testcase_06 AC 389 ms
86,680 KB
testcase_07 AC 378 ms
86,696 KB
testcase_08 AC 401 ms
87,924 KB
testcase_09 AC 404 ms
88,712 KB
testcase_10 AC 415 ms
88,312 KB
testcase_11 AC 469 ms
91,076 KB
testcase_12 AC 445 ms
89,728 KB
testcase_13 AC 447 ms
89,148 KB
testcase_14 AC 454 ms
89,032 KB
testcase_15 AC 440 ms
89,208 KB
testcase_16 AC 370 ms
85,368 KB
testcase_17 AC 387 ms
85,972 KB
testcase_18 AC 297 ms
83,696 KB
testcase_19 AC 434 ms
89,156 KB
権限があれば一括ダウンロードができます

ソースコード

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