結果

問題 No.14 最小公倍数ソート
ユーザー 👑 colognecologne
提出日時 2022-01-21 12:26:34
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,165 bytes
コンパイル時間 233 ms
コンパイル使用メモリ 82,448 KB
実行使用メモリ 71,252 KB
最終ジャッジ日時 2024-05-04 01:11:26
合計ジャッジ時間 2,104 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
59,696 KB
testcase_01 AC 35 ms
57,192 KB
testcase_02 AC 36 ms
58,980 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 #

import itertools
import sys
import itertools

input = sys.stdin.readline


def main():
    N = int(input())
    *A, = map(int, input().split())

    MAX = 10000
    cnt = [0]*(MAX+1)
    for a in A[1:]:
        cnt[a] += 1

    ptr = list(range(MAX+1))

    def getnxt(x):
        while ptr[x] <= MAX and cnt[ptr[x]] == 0:
            ptr[x] += x

        return ptr[x]

    def getmin(v):
        maxv = ans = MAX * MAX
        for i in itertools.count(1):
            if v % i == 0:

                n = getnxt(i)
                l = n * (v // i)
                if maxv > l:
                    maxv = l
                    ans = n
                elif maxv == l:
                    ans = min(ans, n)

                n = getnxt(v // i)
                l = n * i
                if maxv > l:
                    maxv = l
                    ans = n
                elif maxv == l:
                    ans = min(ans, n)

            if i*i > v:
                return ans

    ans = [A[0]]
    while len(ans) != N:
        ret = getmin(ans[-1])
        ans += [ret]*cnt[ret]
        cnt[ret] = 0

    print(*ans, sep=' ')


if __name__ == '__main__':
    main()
0