結果
問題 | No.14 最小公倍数ソート |
ユーザー |
|
提出日時 | 2018-01-22 16:08:36 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,499 bytes |
コンパイル時間 | 2,424 ms |
コンパイル使用メモリ | 176,660 KB |
実行使用メモリ | 8,704 KB |
最終ジャッジ日時 | 2024-12-26 00:48:50 |
合計ジャッジ時間 | 4,646 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 3 WA * 17 |
ソースコード
#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) printf("%d%c", ans[i], " \n"[i==n-1]); return 0; }