結果

問題 No.14 最小公倍数ソート
ユーザー ktgktg
提出日時 2016-04-27 22:22:43
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 55 ms / 5,000 ms
コード長 2,169 bytes
コンパイル時間 1,051 ms
コンパイル使用メモリ 91,144 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-15 04:36:04
合計ジャッジ時間 1,969 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 5 ms
6,940 KB
testcase_04 AC 53 ms
8,960 KB
testcase_05 AC 29 ms
7,040 KB
testcase_06 AC 33 ms
7,168 KB
testcase_07 AC 40 ms
7,680 KB
testcase_08 AC 45 ms
8,320 KB
testcase_09 AC 52 ms
8,960 KB
testcase_10 AC 51 ms
8,704 KB
testcase_11 AC 55 ms
8,960 KB
testcase_12 AC 54 ms
8,960 KB
testcase_13 AC 55 ms
9,088 KB
testcase_14 AC 52 ms
8,832 KB
testcase_15 AC 53 ms
9,088 KB
testcase_16 AC 30 ms
7,168 KB
testcase_17 AC 24 ms
6,940 KB
testcase_18 AC 16 ms
6,944 KB
testcase_19 AC 39 ms
8,192 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