結果

問題 No.14 最小公倍数ソート
ユーザー imulanimulan
提出日時 2018-01-22 16:21:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,493 bytes
コンパイル時間 1,892 ms
コンパイル使用メモリ 174,456 KB
実行使用メモリ 8,664 KB
最終ジャッジ日時 2023-08-26 21:55:40
合計ジャッジ時間 3,642 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

int lcm(int x, int y){
    return x/__gcd(x,y)*y;
}

vector<int> divisor(int n){
    vector<int> ret;
    for(int i=1; i*i<=n; ++i){
        if(n%i==0){
            ret.pb(i);
            if(n/i!=i) ret.pb(n/i);
        }
    }
    sort(all(ret));
    return ret;
}

const int N = 10010;
multiset<int> d[N];

int main(){
    int n;
    cin >>n;
    vector<int> a(n);
    rep(i,n) cin >>a[i];

    for(int i=1; i<n; ++i){
        for(int j:divisor(a[i])) d[j].insert(a[i]);
    }

    vector<int> ans;
    ans.pb(a[0]);
    for(int i=1; i<n; ++i){
        int m = 100000000;
        int nx = -1;

        int now = ans.back();
        for(int j:divisor(now))if(!d[j].empty()){
            int tmp = *d[j].begin();
            if(lcm(now,tmp) < m){
                m = lcm(now,tmp);
                nx = tmp;
            }
        }

        assert(nx!=-1);
        for(int j:divisor(nx)) d[j].erase(d[j].find(nx));
        ans.pb(nx);
    }

    rep(i,n) cout << ans[i] << " \n"[i==n-1];
    return 0;
}
0