結果

問題 No.1917 LCMST
ユーザー ruthenruthen
提出日時 2022-04-30 02:07:20
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 267 ms / 4,000 ms
コード長 1,534 bytes
コンパイル時間 2,901 ms
コンパイル使用メモリ 218,648 KB
実行使用メモリ 47,868 KB
最終ジャッジ日時 2023-09-11 18:11:23
合計ジャッジ時間 15,037 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,424 KB
testcase_01 AC 4 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 5 ms
4,644 KB
testcase_04 AC 5 ms
4,612 KB
testcase_05 AC 5 ms
4,656 KB
testcase_06 AC 5 ms
4,736 KB
testcase_07 AC 5 ms
4,504 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 236 ms
31,988 KB
testcase_10 AC 238 ms
30,964 KB
testcase_11 AC 236 ms
31,212 KB
testcase_12 AC 212 ms
30,664 KB
testcase_13 AC 135 ms
22,056 KB
testcase_14 AC 220 ms
30,456 KB
testcase_15 AC 115 ms
16,804 KB
testcase_16 AC 136 ms
21,852 KB
testcase_17 AC 156 ms
29,856 KB
testcase_18 AC 176 ms
29,668 KB
testcase_19 AC 118 ms
16,764 KB
testcase_20 AC 108 ms
14,296 KB
testcase_21 AC 121 ms
17,236 KB
testcase_22 AC 106 ms
14,164 KB
testcase_23 AC 108 ms
14,300 KB
testcase_24 AC 119 ms
18,004 KB
testcase_25 AC 109 ms
14,364 KB
testcase_26 AC 112 ms
14,276 KB
testcase_27 AC 116 ms
17,936 KB
testcase_28 AC 117 ms
17,740 KB
testcase_29 AC 260 ms
47,488 KB
testcase_30 AC 263 ms
47,868 KB
testcase_31 AC 267 ms
47,328 KB
testcase_32 AC 263 ms
47,344 KB
testcase_33 AC 261 ms
47,304 KB
testcase_34 AC 94 ms
12,212 KB
testcase_35 AC 94 ms
12,024 KB
testcase_36 AC 93 ms
12,092 KB
testcase_37 AC 259 ms
47,612 KB
testcase_38 AC 253 ms
47,356 KB
testcase_39 AC 258 ms
47,404 KB
testcase_40 AC 255 ms
47,168 KB
testcase_41 AC 256 ms
47,668 KB
testcase_42 AC 251 ms
47,616 KB
testcase_43 AC 94 ms
12,412 KB
testcase_44 AC 94 ms
12,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#ifdef _RUTHEN
#include "debug.hpp"
#else
#define show(...) true
#endif

using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> using V = vector<T>;

#include <atcoder/dsu>

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int N;
    cin >> N;
    V<ll> A(N);
    rep(i, N) cin >> A[i];
    const int M = 100000;
    V<ll> cnt(M + 1, 0);
    rep(i, N) cnt[A[i]]++;
    ll ans = 0;
    V<ll> B;
    for (int i = 1; i <= M; i++) {
        if (cnt[i] > 0) {
            ans += i * (cnt[i] - 1);
            cnt[i] = 1;
            B.push_back(i);
        }
    }
    V<int> Bid(M + 1, -1);
    int N2 = B.size();
    rep(i, N2) Bid[B[i]] = i;
    V<tuple<ll, int, int>> es;
    for (int d = 1; d <= M; d++) {
        // gcd=dと決め打ってしまう(実際にはgcdがdより大きくなる場合、コストが小さくなるだけなので最小のものを考えているときには問題ない)
        // lcm(a,b)=a*b/d
        V<int> id;
        for (int j = d; j <= M; j += d) {
            if (cnt[j]) {
                id.push_back(Bid[j]);
            }
        }
        for (int i = 1; i < id.size(); i++) {
            es.push_back({B[id[0]] * B[id[i]] / d, id[0], id[i]});
        }
    }
    atcoder::dsu uf(N2);
    sort(es.begin(), es.end());
    for (auto [cost, a, b] : es) {
        if (!uf.same(a, b)) {
            uf.merge(a, b);
            ans += cost;
        }
    }
    cout << ans << '\n';
    return 0;
}
0