結果

問題 No.14 最小公倍数ソート
ユーザー Ricky_ponRicky_pon
提出日時 2020-05-18 22:02:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 5,000 ms
コード長 2,650 bytes
コンパイル時間 2,598 ms
コンパイル使用メモリ 207,684 KB
実行使用メモリ 9,344 KB
最終ジャッジ日時 2024-04-09 21:37:35
合計ジャッジ時間 4,108 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 44 ms
9,344 KB
testcase_05 AC 23 ms
7,168 KB
testcase_06 AC 27 ms
7,424 KB
testcase_07 AC 31 ms
7,936 KB
testcase_08 AC 37 ms
8,320 KB
testcase_09 AC 44 ms
9,088 KB
testcase_10 AC 42 ms
8,960 KB
testcase_11 AC 44 ms
9,344 KB
testcase_12 AC 44 ms
9,088 KB
testcase_13 AC 45 ms
9,344 KB
testcase_14 AC 44 ms
9,216 KB
testcase_15 AC 45 ms
9,216 KB
testcase_16 AC 25 ms
7,296 KB
testcase_17 AC 21 ms
6,940 KB
testcase_18 AC 14 ms
6,940 KB
testcase_19 AC 34 ms
8,320 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define For(i, a, b) for(int (i)=(int)(a); (i)<(int)(b); ++(i))
#define rFor(i, a, b) for(int (i)=(int)(a)-1; (i)>=(int)(b); --(i))
#define rep(i, n) For((i), 0, (n))
#define rrep(i, n) rFor((i), (n), 0)
#define fi first
#define se second
using namespace std;
typedef long long lint;
typedef unsigned long long ulint;
typedef pair<int, int> pii;
typedef pair<lint, lint> pll;
template<class T> bool chmax(T &a, const T &b){if(a<b){a=b; return true;} return false;}
template<class T> bool chmin(T &a, const T &b){if(a>b){a=b; return true;} return false;}
template<class T> T div_floor(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>=0 ? a/b : (a+1)/b-1;
}
template<class T> T div_ceil(T a, T b){
    if(b < 0) a *= -1, b *= -1;
    return a>0 ? (a-1)/b+1 : a/b;
}

constexpr lint mod = 1e9+7;
constexpr lint INF = mod * mod;
constexpr int MAX = 10010;

vector<int> min_factor, prime;

void sieve(int n){
    min_factor.resize(n+1, 0);
    For(i, 2, n+1){
        if(min_factor[i] == 0){
            min_factor[i] = i;
            prime.push_back(i);
        }
        for(int x: prime){
            if(x*i > n || x > i) break;
            min_factor[x*i] = x;
        }
    }
}

vector<pii> prime_f(int n){
    vector<pii> ret;
    while(n > 1){
        if(ret.empty() || ret.rbegin()->fi != min_factor[n]){
            ret.emplace_back(min_factor[n], 1);
        }
        else ++ret.rbegin()->se;
        n /= min_factor[n];
    }
    return ret;
}

void divisor_dfs(vector<pii> &p, int t, int cur, vector<int> &ret){
    if(cur == p.size()){
        ret.push_back(t);
        return;
    }
    divisor_dfs(p, t, cur+1, ret);
    rep(i, p[cur].se){
        t *= p[cur].fi;
        divisor_dfs(p, t, cur+1, ret);
    }
}

vector<int> get_divisor(int n){
    vector<int> ret;
    auto p = prime_f(n);
    divisor_dfs(p, 1, 0, ret);
    //sort(ret.begin(), ret.end());
    return ret;
}

int n, a[MAX];
multiset<int> S[MAX];
vector<int> d[MAX];

void del(int t){
    for(auto x: d[t]) S[x].erase(S[x].find(t));
}

int main(){
    sieve(MAX);
    scanf("%d", &n);
    rep(i, n){
        scanf("%d", &a[i]);
        if(d[a[i]].empty()) d[a[i]] = get_divisor(a[i]);
        for(auto x: d[a[i]]) S[x].insert(a[i]);
    }

    printf("%d ", a[0]);
    int t = a[0];
    rep(i, n-1){
        del(t);
        int LCM = mod, nxt = MAX;
        for(auto x: d[t])if(!S[x].empty()){
            int tmp = *S[x].begin() / x * t;
            if(LCM > tmp) nxt = *S[x].begin(), LCM = tmp;
            else if(LCM == tmp) chmin(nxt, *S[x].begin());
        }
        printf("%d%c", nxt, i==n-2 ? '\n' : ' ');
        t = nxt;
    }
}
0