結果

問題 No.14 最小公倍数ソート
ユーザー ktgktg
提出日時 2016-04-27 22:22:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 66 ms / 5,000 ms
コード長 2,169 bytes
コンパイル時間 861 ms
コンパイル使用メモリ 85,400 KB
実行使用メモリ 9,132 KB
最終ジャッジ日時 2023-07-27 17:29:29
合計ジャッジ時間 2,906 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 6 ms
4,556 KB
testcase_04 AC 65 ms
9,132 KB
testcase_05 AC 33 ms
6,972 KB
testcase_06 AC 36 ms
7,192 KB
testcase_07 AC 43 ms
7,648 KB
testcase_08 AC 51 ms
8,260 KB
testcase_09 AC 59 ms
8,796 KB
testcase_10 AC 60 ms
8,744 KB
testcase_11 AC 64 ms
8,948 KB
testcase_12 AC 62 ms
8,996 KB
testcase_13 AC 63 ms
8,996 KB
testcase_14 AC 64 ms
8,904 KB
testcase_15 AC 66 ms
9,060 KB
testcase_16 AC 37 ms
7,116 KB
testcase_17 AC 29 ms
6,652 KB
testcase_18 AC 19 ms
5,760 KB
testcase_19 AC 48 ms
8,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <iterator>

//#define pb push_back
//#define puts(x) cout << #x << " : " << x << endl;
//#pragma GCC diagnostic ignored "-Wconversion"
//#define REP(i,n) for (int i=0;i<(n);i++)
//#define REPE(i,n) for (int i=0;i<=(n);i++)
//#define init(a,b) memset((a), (b), (sizeof(a)));
//#define PI 3.14159265
//#define EPS (1e-10)
//#define EQ(a,b) (abs((a)-(b)) < EPS)

using namespace std;

typedef long long ll;
//#define int long long

int dxy[] = {0, 1, 0, -1, 0};

typedef pair<int, int> P;
ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

ll lcm(ll a , ll b) {
    return a / (gcd(a, b)) * b;
}

int a[10005];
vector<int> divs[10005]; // i, a,b,c,d,...
set<pair<int, int> > s[10005]; // div, a[i], i

signed main() {
    int n;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];

    for(int k=0;k<n;k++){
        for(int i=1;i*i<=a[k];i++){
            if(a[k] % i == 0){
                divs[k].push_back(i);
                s[i].insert({a[k], k});
                if(i*i != a[k]){
                    divs[k].push_back(a[k]/i);
                    s[a[k]/i].insert({a[k], k});
                }
            }
        }
    }
    int id = 0;
    for(int i=0;i<n-1;i++){
        ll min_t = -1;
        cout<<a[id]<<' ';
        int next_id = id;
        for(int j = 0;j < divs[id].size(); j++){
            int div = divs[id][j];
            s[div].erase({a[id], id});
            if(s[div].empty())continue;
            int a_index = s[div].begin()->first;
            int index = s[div].begin()->second;

//            cout<<i<<':'<<j<<':'<<div<<':'<<a_index<<':'<<index<<endl;

            ll lcm_res = lcm(a[id], a_index);
            if(min_t == -1ll || min_t > lcm_res || (min_t == lcm_res && a_index < a[next_id])){
                min_t = lcm_res;
                next_id = index;
            }
        }
//        printf("next_id = %d\n", next_id);
        id = next_id;
    }
    cout<<a[id]<<endl;
    return 0;
}
0