結果

問題 No.14 最小公倍数ソート
ユーザー ctyl_0ctyl_0
提出日時 2015-12-05 02:47:37
言語 Python2
(2.7.18)
結果
RE  
実行時間 -
コード長 953 bytes
コンパイル時間 41 ms
コンパイル使用メモリ 6,616 KB
実行使用メモリ 7,360 KB
最終ジャッジ日時 2023-10-12 15:30:11
合計ジャッジ時間 1,318 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
6,124 KB
testcase_01 AC 12 ms
6,120 KB
testcase_02 AC 11 ms
5,976 KB
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
testcase_10 RE -
testcase_11 RE -
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
権限があれば一括ダウンロードができます

ソースコード

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(12)]
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