結果

問題 No.14 最小公倍数ソート
ユーザー square1001square1001
提出日時 2022-02-17 13:05:47
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 112 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 10,952 KB
実行使用メモリ 11,972 KB
最終ジャッジ日時 2023-09-11 17:38:53
合計ジャッジ時間 3,300 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
7,784 KB
testcase_01 AC 16 ms
7,792 KB
testcase_02 AC 15 ms
7,976 KB
testcase_03 AC 66 ms
10,516 KB
testcase_04 AC 111 ms
11,716 KB
testcase_05 AC 69 ms
10,404 KB
testcase_06 AC 77 ms
10,624 KB
testcase_07 AC 88 ms
11,064 KB
testcase_08 AC 98 ms
11,492 KB
testcase_09 AC 106 ms
11,656 KB
testcase_10 AC 110 ms
11,820 KB
testcase_11 AC 111 ms
11,816 KB
testcase_12 AC 110 ms
11,724 KB
testcase_13 AC 112 ms
11,972 KB
testcase_14 AC 108 ms
11,824 KB
testcase_15 AC 109 ms
11,728 KB
testcase_16 AC 98 ms
11,220 KB
testcase_17 AC 90 ms
11,192 KB
testcase_18 AC 82 ms
10,936 KB
testcase_19 AC 104 ms
11,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# Time Complexity: O((N + A) log A)

# Input
N = int(input())
A = list(map(int, input().split()))

# Pre-calculation
LIMIT = max(A)
cnt = [ 0 ] * (LIMIT + 1)
for x in A[1:]:
	cnt[x] += 1
vals = list(filter(lambda x: cnt[x] != 0, range(1, LIMIT + 1)))
divs = [ list() for i in range(LIMIT + 1) ]
for i in range(1, LIMIT + 1):
	for j in range(i, LIMIT + 1, i):
		divs[j].append(i)

# Calculation
used = [ True ] * (LIMIT + 1)
divpos = [ 0 ] * (LIMIT + 1)
preval = A[0]
result = [ A[0] ]
for x in vals:
	used[x] = False
for i in range(len(vals)):
	optval, optlcm = -1, 10 ** 18
	for j in divs[preval]:
		while divpos[j] <= LIMIT and used[divpos[j]]:
			divpos[j] += j
		if divpos[j] <= LIMIT and optlcm > preval * divpos[j] // j:
			optval = divpos[j]
			optlcm = preval * divpos[j] // j
	result += [ optval ] * cnt[optval]
	used[optval] = True
	preval = optval

# Output
print(*result)
0