結果

問題 No.14 最小公倍数ソート
ユーザー square1001
提出日時 2022-02-17 13:27:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 5,000 ms
コード長 1,756 bytes
コンパイル時間 216 ms
コンパイル使用メモリ 33,664 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-29 07:38:45
合計ジャッジ時間 976 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
int readint() {
int x = 0;
char ch = getchar_unlocked();
while ('0' <= ch && ch <= '9') {
x = x * 10 + int(ch - '0');
ch = getchar_unlocked();
}
return x;
}
char buf[32];
void writeint(int x) {
// ensure that x >= 1
int pos = 0;
while (x != 0) {
buf[pos++] = x % 10 + '0';
x /= 10;
}
while (pos != 0) {
putchar_unlocked(buf[--pos]);
}
}
int N, LIMIT, A[10240], cnt[10240], divpos[10240], divsep[10240], divs[94208]; bool canuse[10240];
int main() {
// step #1. read input
N = readint();
for (int i = 0; i < N; i++) {
A[i] = readint();
if (LIMIT < A[i]) {
LIMIT = A[i];
}
}
// step #2. pre-calculation
for (int i = 1; i < N; i++) {
cnt[A[i]]++;
}
for (int i = 1; i <= LIMIT; i++) {
for (int j = i; j <= LIMIT; j += i) {
divsep[j + 2]++;
}
}
for (int i = 1; i <= LIMIT; i++) {
divsep[i + 2] += divsep[i + 1];
}
for (int i = 1; i <= LIMIT; i++) {
for (int j = i; j <= LIMIT; j += i) {
divs[divsep[j + 1]++] = i;
}
}
// step #3. calculate and output the answer from the leftmost element
int preval = A[0];
for (int i = 1; i < N; i++) {
canuse[A[i]] = true;
}
writeint(A[0]);
while (true) {
int optval = -1, optlcm = (1 << 30);
for (int i = divsep[preval]; i < divsep[preval + 1]; i++) {
int x = divs[i];
while (divpos[x] <= LIMIT && !canuse[divpos[x]]) {
divpos[x] += x;
}
if (divpos[x] <= LIMIT) {
int curlcm = preval / x * divpos[x];
if (optlcm > curlcm) {
optlcm = curlcm;
optval = divpos[x];
}
}
}
if (optval == -1) {
break;
}
for (int i = 0; i < cnt[optval]; i++) {
putchar_unlocked(' ');
writeint(optval);
}
canuse[optval] = false;
preval = optval;
}
putchar_unlocked('\n');
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0