結果

問題 No.1917 LCMST
ユーザー sotanishysotanishy
提出日時 2022-04-29 23:48:58
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,861 bytes
コンパイル時間 2,877 ms
コンパイル使用メモリ 213,384 KB
実行使用メモリ 814,396 KB
最終ジャッジ日時 2023-09-11 15:31:31
合計ジャッジ時間 11,052 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 89 ms
13,780 KB
testcase_04 AC 88 ms
12,824 KB
testcase_05 AC 90 ms
13,824 KB
testcase_06 AC 89 ms
13,956 KB
testcase_07 AC 88 ms
13,004 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 MLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, t) for (int i = (int)(s); i < (int)(t); ++i)
#define revrep(i, t, s) for (int i = (int)(t)-1; i >= (int)(s); --i)
#define all(x) begin(x), end(x)
template <typename T> bool chmax(T& a, const T& b) { return a < b ? (a = b, 1) : 0; }
template <typename T> bool chmin(T& a, const T& b) { return a > b ? (a = b, 1) : 0; }

class UnionFind {
public:
    UnionFind() = default;
    explicit UnionFind(int n) : data(n, -1) {}

    int find(int x) {
        if (data[x] < 0) return x;
        return data[x] = find(data[x]);
    }

    void unite(int x, int y) {
        x = find(x);
        y = find(y);
        if (x == y) return;
        if (data[x] > data[y]) std::swap(x, y);
        data[x] += data[y];
        data[y] = x;
    }

    bool same(int x, int y) {
        return find(x) == find(y);
    }

    int size(int x) {
        return -data[find(x)];
    }

private:
    std::vector<int> data;
};

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15);

    int N;
    cin >> N;
    const int M = 1e5;
    vector<ll> C(M+1);
    rep(i,0,N) {
        int x;
        cin >> x;
        ++C[x];
    }
    ll ans = 0;
    vector<ll> vs;
    rep(i,1,M+1) {
        if (C[i] == 0) continue;
        if (C[i] > 1) {
            ans += i*(C[i]-1);
            C[i] = 1;
        }
        vs.push_back(i);
        for (int j = 2*i; j <= M; j += i) {
            ans += j*C[j];
            C[j] = 0;
        }
    }
    vector<tuple<ll, int, int>> es;
    rep(i,0,vs.size()) rep(j,i+1,vs.size()) es.push_back({(ll)vs[i]*vs[j]/gcd(vs[i],vs[j]), i, j});
    sort(all(es));
    UnionFind uf(vs.size());
    for (auto [v, i, j] : es) {
        if (!uf.same(i, j)) ans += v;
        uf.unite(i, j);
    }
    cout << ans << endl;
}
0