結果

問題 No.14 最小公倍数ソート
ユーザー warashiwarashi
提出日時 2015-11-02 20:10:45
言語 Go
(1.22.1)
結果
TLE  
実行時間 -
コード長 750 bytes
コンパイル時間 12,268 ms
コンパイル使用メモリ 230,068 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-04-19 03:29:28
合計ジャッジ時間 19,818 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
7,552 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1,410 ms
5,376 KB
testcase_04 TLE -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"fmt"
	"sort"
)

var key int

type ByLCM []int

func (a ByLCM) Len() int      { return len(a) }
func (a ByLCM) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByLCM) Less(i, j int) bool {
	lcmi := lcm(key, a[i])
	lcmj := lcm(key, a[j])
	if lcmi == lcmj {
		return a[i] < a[j]
	}
	return lcmi < lcmj
}

func main() {
	var N int
	fmt.Scan(&N)
	a := make(ByLCM, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&a[i])
	}

	key = a[0]
	for i := 1; i < N; i++ {
		sort.Sort(a[i:])
		key = a[i]
	}

	ans := fmt.Sprintln(a)
	fmt.Println(ans[1 : len(ans)-2])
}

func lcm(a, b int) int {
	g := gcd(a, b)
	return (a / g) * (b / g) * g
}

func gcd(a, b int) int {
	if a > b {
		a, b = b, a
	}
	if a == 0 {
		return b
	}

	return gcd(b%a, a)
}
0