結果

問題 No.14 最小公倍数ソート
ユーザー ctyl_0ctyl_0
提出日時 2015-12-05 02:47:59
言語 Python2
(2.7.18)
結果
AC  
実行時間 544 ms / 5,000 ms
コード長 956 bytes
コンパイル時間 40 ms
コンパイル使用メモリ 6,744 KB
実行使用メモリ 21,568 KB
最終ジャッジ日時 2023-10-12 15:30:19
合計ジャッジ時間 7,748 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 15 ms
6,660 KB
testcase_01 AC 15 ms
6,532 KB
testcase_02 AC 15 ms
6,544 KB
testcase_03 AC 59 ms
8,344 KB
testcase_04 AC 544 ms
21,568 KB
testcase_05 AC 274 ms
15,172 KB
testcase_06 AC 309 ms
16,084 KB
testcase_07 AC 357 ms
17,544 KB
testcase_08 AC 431 ms
19,084 KB
testcase_09 AC 505 ms
20,924 KB
testcase_10 AC 502 ms
20,632 KB
testcase_11 AC 518 ms
21,316 KB
testcase_12 AC 522 ms
21,224 KB
testcase_13 AC 528 ms
21,340 KB
testcase_14 AC 519 ms
21,044 KB
testcase_15 AC 535 ms
21,452 KB
testcase_16 AC 312 ms
15,632 KB
testcase_17 AC 258 ms
14,236 KB
testcase_18 AC 170 ms
11,816 KB
testcase_19 AC 415 ms
18,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

inf = 1e9

def gcd(p, q):
    if q == 0:
        return p
    return gcd(q, p % q)
    
def lcm(p, q):
    return p / gcd(p, q) * q

N = input()
A = map(int, raw_input().split())

S = [[] for i in xrange(10001)]
div = [[] for i in xrange(N)]

for i in xrange(N):
    j = 1
    while j * j <= A[i]:
        if A[i] % j == 0:
            div[i].append(j)
            S[j].append([A[i], i])
            if j * j != A[i]:
                div[i].append(A[i] / j)
                S[A[i] / j].append([A[i], i])
        j += 1

for i in xrange(len(S)):
    S[i].sort()
    
vis = [0] * N
ans = [A[0]]
ind = 0

for i in xrange(N - 1):
    num = ans[i]
    vis[ind] = 1
    m = (inf, 0, 0)
    for j in div[ind]:
        while len(S[j]) and vis[S[j][0][1]]:
            S[j].pop(0)
        if len(S[j]):
            m = min(m, (lcm(num, S[j][0][0]), S[j][0][0], S[j][0][1]))
    ans.append(m[1])
    ind = m[2]

print " ".join(map(str, ans))
0