結果

問題 No.14 最小公倍数ソート
コンテスト
ユーザー bal4u
提出日時 2019-04-30 21:45:54
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
RE  
実行時間 -
コード長 1,240 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 295 ms
コンパイル使用メモリ 39,656 KB
最終ジャッジ日時 2026-02-22 03:14:14
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 WA * 16 RE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.14 最小公倍数ソート
// 2019.4.30 bal4u

#include <stdio.h>
#include <stdlib.h>

#if 1
#define gc() getchar_unlocked()
#define pc(c) putchar_unlocked(c)
#else
#define gc() getchar()
#define pc(c) putchar(c)
#endif
int in()    // 非負整数の入力
{
	int n = 0, c = gc();
	do n = 10 * n + (c & 0xf), c = gc(); while (c >= '0');
	return n;
}

void out(int n, int sp)  // 非負整数の表示(出力)
{
	int i;
	char b[20];

	if (sp) pc(' ');
	if (!n) pc('0');
	else {
		i = 0; while (n) b[i++] = n % 10 + '0', n /= 10;
		while (i--) pc(b[i]);
	}
}

int N;
int a[10005];
int f[10005];

int cmp(const void *a, const void *b) { return *(int *)a - *(int *)b; }

int main()
{
	int i, j, k, N;

	N = in();
	for (i = 0; i < N; i++) a[i] = in(), f[a[i]]++;
	qsort(a+1, N-1, sizeof(int), cmp);
	out(a[0], 0), f[a[0]]--;
	i = 1, j = N-1;
	while (a[i] == 1) out(1, 1), f[1]--, i++, j--;
	while (j > 0) {
		while (a[i] == a[i-1]) out(a[i], 1), f[a[i]]--, i++, j--;
		if (j > 0) {
			int t = i-1;
			while (!f[a[i]]) i++;		
			for (k = 2; k < a[i]; k++) {
				if (f[k*a[t]]) {
					out(k*a[t], 1), f[k*a[t]]--, j--;
					break;
				}
			}
			if (k == a[i]) out(a[i], 1), f[a[i]]--, i++, j--;
		}
	}
	pc('\n');
	return 0;
}
0