結果

問題 No.14 最小公倍数ソート
ユーザー ctyl_0ctyl_0
提出日時 2015-12-05 02:50:30
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 515 ms / 5,000 ms
コード長 956 bytes
コンパイル時間 1,819 ms
コンパイル使用メモリ 77,452 KB
実行使用メモリ 95,760 KB
最終ジャッジ日時 2023-10-12 15:30:36
合計ジャッジ時間 10,813 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
79,140 KB
testcase_01 AC 82 ms
78,916 KB
testcase_02 AC 83 ms
79,212 KB
testcase_03 AC 259 ms
83,340 KB
testcase_04 AC 514 ms
95,324 KB
testcase_05 AC 394 ms
90,032 KB
testcase_06 AC 411 ms
90,172 KB
testcase_07 AC 404 ms
91,316 KB
testcase_08 AC 463 ms
93,820 KB
testcase_09 AC 505 ms
95,760 KB
testcase_10 AC 470 ms
95,076 KB
testcase_11 AC 486 ms
94,092 KB
testcase_12 AC 508 ms
95,216 KB
testcase_13 AC 495 ms
95,264 KB
testcase_14 AC 498 ms
93,692 KB
testcase_15 AC 515 ms
94,520 KB
testcase_16 AC 365 ms
90,708 KB
testcase_17 AC 404 ms
88,856 KB
testcase_18 AC 342 ms
87,276 KB
testcase_19 AC 459 ms
93,416 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