結果

問題 No.1917 LCMST
ユーザー sotanishysotanishy
提出日時 2022-04-29 23:58:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 229 ms / 4,000 ms
コード長 1,780 bytes
コンパイル時間 2,237 ms
コンパイル使用メモリ 217,716 KB
実行使用メモリ 38,860 KB
最終ジャッジ日時 2024-06-29 05:50:33
合計ジャッジ時間 10,675 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,816 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 4 ms
6,944 KB
testcase_03 AC 6 ms
6,944 KB
testcase_04 AC 5 ms
6,944 KB
testcase_05 AC 4 ms
6,944 KB
testcase_06 AC 5 ms
6,940 KB
testcase_07 AC 5 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 198 ms
21,488 KB
testcase_10 AC 196 ms
21,120 KB
testcase_11 AC 195 ms
20,980 KB
testcase_12 AC 178 ms
21,892 KB
testcase_13 AC 118 ms
12,760 KB
testcase_14 AC 195 ms
21,828 KB
testcase_15 AC 97 ms
9,080 KB
testcase_16 AC 120 ms
12,672 KB
testcase_17 AC 139 ms
21,216 KB
testcase_18 AC 161 ms
21,700 KB
testcase_19 AC 102 ms
8,568 KB
testcase_20 AC 93 ms
6,944 KB
testcase_21 AC 107 ms
8,308 KB
testcase_22 AC 92 ms
6,940 KB
testcase_23 AC 91 ms
6,940 KB
testcase_24 AC 101 ms
8,184 KB
testcase_25 AC 92 ms
6,944 KB
testcase_26 AC 95 ms
6,940 KB
testcase_27 AC 101 ms
8,508 KB
testcase_28 AC 103 ms
9,276 KB
testcase_29 AC 220 ms
38,776 KB
testcase_30 AC 218 ms
38,104 KB
testcase_31 AC 220 ms
38,020 KB
testcase_32 AC 217 ms
37,432 KB
testcase_33 AC 229 ms
37,348 KB
testcase_34 AC 82 ms
6,940 KB
testcase_35 AC 79 ms
6,940 KB
testcase_36 AC 79 ms
6,940 KB
testcase_37 AC 216 ms
37,688 KB
testcase_38 AC 219 ms
37,440 KB
testcase_39 AC 215 ms
38,320 KB
testcase_40 AC 218 ms
37,364 KB
testcase_41 AC 222 ms
38,728 KB
testcase_42 AC 212 ms
38,860 KB
testcase_43 AC 82 ms
6,940 KB
testcase_44 AC 83 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

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<tuple<ll, int, int>> es;
    rep(i,1,M+1) {
        if (C[i] > 1) {
            ans += i*(C[i]-1);
        }
        vector<int> vs;
        for (int j = i; j <= M; j += i) {
            if (C[j] > 0) {
                vs.push_back(j);
            }
        }
        rep(j,1,vs.size()) es.push_back({(ll)vs[0]*vs[j]/i, vs[0], vs[j]});
    }
    sort(all(es));
    UnionFind uf(M+1);
    for (auto [v, i, j] : es) {
        if (!uf.same(i, j)) ans += v;
        uf.unite(i, j);
    }
    cout << ans << endl;
}
0