結果

問題 No.14 最小公倍数ソート
ユーザー dangodango
提出日時 2023-07-08 09:54:03
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 152 ms / 5,000 ms
コード長 1,531 bytes
コンパイル時間 309 ms
コンパイル使用メモリ 87,240 KB
実行使用メモリ 79,860 KB
最終ジャッジ日時 2023-09-29 11:17:16
合計ジャッジ時間 4,081 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
72,136 KB
testcase_01 AC 72 ms
72,064 KB
testcase_02 AC 71 ms
72,240 KB
testcase_03 AC 123 ms
79,336 KB
testcase_04 AC 147 ms
79,696 KB
testcase_05 AC 135 ms
79,860 KB
testcase_06 AC 138 ms
79,448 KB
testcase_07 AC 134 ms
79,616 KB
testcase_08 AC 137 ms
79,616 KB
testcase_09 AC 146 ms
79,464 KB
testcase_10 AC 138 ms
79,300 KB
testcase_11 AC 145 ms
79,312 KB
testcase_12 AC 144 ms
79,820 KB
testcase_13 AC 139 ms
79,608 KB
testcase_14 AC 147 ms
79,596 KB
testcase_15 AC 152 ms
79,464 KB
testcase_16 AC 137 ms
79,604 KB
testcase_17 AC 141 ms
79,644 KB
testcase_18 AC 131 ms
79,044 KB
testcase_19 AC 139 ms
79,780 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
def readint():
    x = 0
    ch = sys.stdin.read(1)
    while '0' <= ch <= '9':
        x = x * 10 + int(ch)
        ch = sys.stdin.read(1)
    return x
def writeint(x):
    buf = []
    while x != 0:
        buf.append(x % 10)
        x //= 10
    while len(buf) != 0:
        sys.stdout.write(str(buf.pop()))
N = readint()
LIMIT = 0
A = [0] * 10240
cnt = [0] * 10240
divpos = [0] * 10240
divsep = [0] * 10240
divs = [0] * 94208
canuse = [False] * 10240
for i in range(N):
    A[i] = readint()
    if LIMIT < A[i]:
        LIMIT = A[i]
for i in range(1, N):
    cnt[A[i]] += 1
for i in range(1, LIMIT + 1):
    for j in range(i, LIMIT + 1, i):
        divsep[j + 2] += 1
for i in range(1, LIMIT + 1):
    divsep[i + 2] += divsep[i + 1]
for i in range(1, LIMIT + 1):
    for j in range(i, LIMIT + 1, i):
        divs[divsep[j + 1]] = i
        divsep[j + 1] += 1
preval = A[0]
for i in range(1, N):
    canuse[A[i]] = True
writeint(A[0])
while True:
    optval = -1
    optlcm = (1 << 30)
    for i in range(divsep[preval], divsep[preval + 1]):
        x = divs[i]
        while divpos[x] <= LIMIT and not canuse[divpos[x]]:
            divpos[x] += x
        if divpos[x] <= LIMIT:
            curlcm = preval // x * divpos[x]
            if optlcm > curlcm:
                optlcm = curlcm
                optval = divpos[x]
    if optval == -1:
        break
    for i in range(cnt[optval]):
        sys.stdout.write(' ')
        writeint(optval)
    canuse[optval] = False
    preval = optval
sys.stdout.write('\n')
0