結果

問題 No.14 最小公倍数ソート
ユーザー antaanta
提出日時 2014-10-31 03:16:01
言語 Python2
(2.7.18)
結果
RE  
実行時間 -
コード長 2,462 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 6,752 KB
実行使用メモリ 6,684 KB
最終ジャッジ日時 2023-08-28 23:36:22
合計ジャッジ時間 1,332 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
5,936 KB
testcase_01 AC 11 ms
6,016 KB
testcase_02 AC 11 ms
5,944 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 #

# <http://yukicoder.me/submissions/1252>をPythonに移植
# Pythonには慣れてないのでそのまま移植した感なコードになってるかと…
# ちなみに↑の"シンプルに…"のところは嘘だった。正しくはO(N X^\epsilon + X^{1+\epsilon})とかそんなん(まだ自信ない)
# ちなみに数xの約数の数は"最悪"O(x^\epsilon)で抑えられる。"divisor bound"と言うらしい。平均はΘ(log x)。

def getSmallestPrimeFactors(n):
	res = [0] * (n+1)
	res[1] = 1
	for i in xrange(2, n+1):
		if res[i] == 0:
			res[i] = i
			for j in xrange(i * i, n+1, i):
				if res[j] == 0:
					res[j] = i
	return res

def primeFactors(x, smallestPrimeFactor):
	res = []
	while x != 1:
		p, k = smallestPrimeFactor[x], 1
		x //= p
		while x % p == 0:
			x //= p
			k += 1
		res.append((p, k))
	return res

def getDivisors(x, smallestPrimeFactor):
	fs = primeFactors(x, smallestPrimeFactor)
	res = [1]
	for p, k in fs:
		for x in res[:]:
			for _ in xrange(k):
				x *= p
				res.append(x)
	return res

class Node:
	def __init__(self, i):
		self.i = i
		self.prev, self.next = None, None

def insertToList(head, t):
	t.next = head
	if head:
		head.prev = t
	return t

def removeFromList(head, t):
	if t.prev:
		t.prev.next = t.next
	if t.next:
		t.next.prev = t.prev
	if head is t:
		head = t.next
	t.prev, t.next = None, None
	return head

N = int(raw_input())
a = map(int, raw_input().split(" "))
X = 10
smallestPrimeFactor = getSmallestPrimeFactors(X)
divisors = [ getDivisors(x, smallestPrimeFactor) for x in a ]

indices = [ [] for _ in xrange(X+1) ]
for i, x in enumerate(a):
	indices[x].append(i)

offsets = [None] * N
numTotalDivisors = 0
for i in xrange(N):
	offsets[i] = numTotalDivisors
	numTotalDivisors += len(divisors[i])

nodes = [ Node(i) for i in xrange(N) for _ in xrange(len(divisors[i])) ]
heads = [ None ] * (X+1)

for x in xrange(X, 0, -1):
	for i in indices[x]:
		for j, d in enumerate(divisors[i]):
			heads[d] = insertToList(heads[d], nodes[offsets[i] + j])

curi = 0
ans = [ a[curi] ]
for k in xrange(N-1):
	for j, d in enumerate(divisors[curi]):
		heads[d] = removeFromList(heads[d], nodes[offsets[curi] + j])
	p = (X+1, X+1, -1)
	for j, d in enumerate(divisors[curi]):
		t = heads[d]
		if t:
			i = t.i
			p = min(p, (a[i] // d, a[i], i))
	curi = p[2]
	ans.append(a[curi])

print " ".join(map(str, ans))
0