結果
問題 | No.14 最小公倍数ソート |
ユーザー |
![]() |
提出日時 | 2022-02-17 13:21:23 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 6 ms / 5,000 ms |
コード長 | 1,435 bytes |
コンパイル時間 | 561 ms |
コンパイル使用メモリ | 67,968 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-29 07:38:43 |
合計ジャッジ時間 | 1,347 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 20 |
ソースコード
#include <iostream>#include <algorithm>using namespace std;int N, LIMIT, A[10240], cnt[10240], divpos[10240], divsep[10240], divs[94208]; bool canuse[10240];int main() {// step #1. read inputcin.tie(0);ios_base::sync_with_stdio(false);cin >> N;for (int i = 0; i < N; i++) {cin >> A[i];}// step #2. pre-calculationLIMIT = *max_element(A, A + N);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 elementint preval = A[0];for (int i = 1; i < N; i++) {canuse[A[i]] = true;}cout << 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++) {cout << ' ' << optval;}canuse[optval] = false;preval = optval;}cout << endl;return 0;}