結果

問題 No.14 最小公倍数ソート
ユーザー square1001square1001
提出日時 2022-02-17 13:07:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 157 ms / 5,000 ms
コード長 883 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 87,076 KB
実行使用メモリ 80,216 KB
最終ジャッジ日時 2023-09-11 17:38:57
合計ジャッジ時間 3,779 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,376 KB
testcase_01 AC 74 ms
70,992 KB
testcase_02 AC 74 ms
71,284 KB
testcase_03 AC 157 ms
79,404 KB
testcase_04 AC 132 ms
80,020 KB
testcase_05 AC 119 ms
78,860 KB
testcase_06 AC 124 ms
79,264 KB
testcase_07 AC 128 ms
79,468 KB
testcase_08 AC 125 ms
79,436 KB
testcase_09 AC 124 ms
79,832 KB
testcase_10 AC 124 ms
79,908 KB
testcase_11 AC 129 ms
80,076 KB
testcase_12 AC 131 ms
79,940 KB
testcase_13 AC 128 ms
80,020 KB
testcase_14 AC 131 ms
79,888 KB
testcase_15 AC 136 ms
80,216 KB
testcase_16 AC 125 ms
79,940 KB
testcase_17 AC 122 ms
80,064 KB
testcase_18 AC 120 ms
79,804 KB
testcase_19 AC 128 ms
79,788 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