結果

問題 No.1917 LCMST
ユーザー ruthen71
提出日時 2022-04-29 23:22:53
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,463 bytes
コンパイル時間 2,386 ms
コンパイル使用メモリ 211,856 KB
最終ジャッジ日時 2025-01-29 00:08:05
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 16 TLE * 1 MLE * 25
権限があれば一括ダウンロードができます

ソースコード

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];
    ll m = *min_element(A.begin(), A.end());
    const int M = 100000;
    V<ll> cnt(M + 1, 0);
    rep(i, N) cnt[A[i]]++;
    V<V<int>> G(M + 1);
    for (int i = 1; i <= M; i++) {
        for (int j = 2 * i; j <= M; j += i) {
            G[j].push_back(i);
        }
    }
    ll ans = 0;
    V<ll> B;
    for (int i = M; i >= 1; i--) {
        if (cnt[i] == 0) continue;
        ans += i * (cnt[i] - 1);
        int ok = 0;
        for (auto d : G[i]) {
            if (cnt[d] != 0) {
                ok = 1;
                break;
            }
        }
        if (ok) {
            ans += i;
        } else {
            B.push_back(i);
        }
    }
    show(B, ans);
    int N2 = B.size();
    V<pair<ll, pair<int, int>>> es;
    rep(i, N2) {
        rep(j, i) { es.push_back({lcm(B[i], B[j]), {i, j}}); }
    }
    sort(es.begin(), es.end());
    atcoder::dsu uf(N2);
    for (auto [cost, p] : es) {
        if (!uf.same(p.first, p.second)) {
            uf.merge(p.first, p.second);
            ans += cost;
        }
    }
    cout << ans << '\n';
    return 0;
}
0