結果

問題 No.14 最小公倍数ソート
ユーザー yuki2006yuki2006
提出日時 2015-02-09 15:42:01
言語 PyPy2
(7.3.15)
結果
TLE  
実行時間 -
コード長 966 bytes
コンパイル時間 917 ms
コンパイル使用メモリ 77,524 KB
実行使用メモリ 101,724 KB
最終ジャッジ日時 2023-09-05 20:58:50
合計ジャッジ時間 8,615 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
79,684 KB
testcase_01 AC 83 ms
79,340 KB
testcase_02 AC 83 ms
79,244 KB
testcase_03 AC 663 ms
89,776 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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


def solve(N, data):
    GC = 1000
    memo = [[None for i in xrange(GC)] for i in xrange(GC)]

    def gcd(a, b):
        if b == 0:
            return a
        if a < GC and b < GC:
            if not (memo[a][b] is None):
                return memo[a][b]
            memo[a][b] = gcd(b, a % b)
            return memo[a][b]
        return gcd(b, a % b)

    def bcd(a, b):
        return a * b / gcd(a, b)

    result = [0] * N
    result[0] = data[0]
    del data[0]
    ans_p = 1
    while ans_p < N:
        mn = -1
        index = 0
        for i, v in enumerate(data):
            a = bcd(result[ans_p - 1], v)
            if mn == -1 or mn > a or (mn == a and data[index] > v):
                mn = a
                index = i

        result[ans_p] = data[index]
        ans_p += 1
        del data[index]
    return result


N = int(raw_input())
data = map(int, raw_input().split(" "))

print " ".join(map(str, solve(N, data)))
0