結果

問題 No.14 最小公倍数ソート
ユーザー szkhtsszkhts
提出日時 2022-08-14 07:43:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 444 ms / 5,000 ms
コード長 987 bytes
コンパイル時間 111 ms
コンパイル使用メモリ 11,940 KB
実行使用メモリ 22,068 KB
最終ジャッジ日時 2023-10-26 01:17:33
合計ジャッジ時間 6,922 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 34 ms
11,484 KB
testcase_01 AC 33 ms
11,484 KB
testcase_02 AC 35 ms
11,484 KB
testcase_03 AC 62 ms
12,768 KB
testcase_04 AC 421 ms
22,068 KB
testcase_05 AC 237 ms
17,540 KB
testcase_06 AC 255 ms
17,940 KB
testcase_07 AC 295 ms
19,116 KB
testcase_08 AC 336 ms
20,164 KB
testcase_09 AC 389 ms
21,520 KB
testcase_10 AC 383 ms
21,324 KB
testcase_11 AC 404 ms
21,696 KB
testcase_12 AC 400 ms
21,844 KB
testcase_13 AC 428 ms
21,800 KB
testcase_14 AC 427 ms
21,524 KB
testcase_15 AC 444 ms
21,852 KB
testcase_16 AC 259 ms
17,876 KB
testcase_17 AC 214 ms
16,944 KB
testcase_18 AC 151 ms
15,256 KB
testcase_19 AC 341 ms
19,824 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