結果

問題 No.14 最小公倍数ソート
ユーザー masamasa
提出日時 2015-01-20 19:30:20
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 705 bytes
コンパイル時間 565 ms
コンパイル使用メモリ 69,452 KB
実行使用メモリ 8,012 KB
最終ジャッジ日時 2023-09-05 02:09:44
合計ジャッジ時間 7,602 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,388 KB
testcase_03 AC 71 ms
4,380 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 #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>

using namespace std;

int gcd(int a, int b) {
	if (b == 0) {
		return a;
	}
	return gcd(b, a % b);
}

long lcm(int a, int b) {
	return (long) a * b / gcd(a, b);
}

int main() {
	int n;

	cin >> n;
	vector< pair<long, int> > a(n);
	int input;
	for (int i = 0; i < n; i++) {
		cin >> input;
		a[i] = make_pair(0, input);
	}

	for (int i = 0; i < n - 1; i++) {
		int start = a[i].second;
		for (int j = i + 1; j < n; j++) {
			a[j].first = lcm(start, a[j].second);
		}
		sort(a.begin() + i + 1, a.end());
	}

	for (int i = 0; i < n; i++) {
		printf("%d%c", a[i].second, i != n - 1 ? ' ' : '\n');
	}
	return 0;
}
0