結果

問題 No.1917 LCMST
ユーザー sotanishysotanishy
提出日時 2022-04-29 23:58:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 254 ms / 4,000 ms
コード長 1,780 bytes
コンパイル時間 2,210 ms
コンパイル使用メモリ 213,408 KB
実行使用メモリ 38,544 KB
最終ジャッジ日時 2023-09-11 15:43:55
合計ジャッジ時間 12,124 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 3 ms
4,380 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 5 ms
4,376 KB
testcase_05 AC 5 ms
4,380 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,380 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 228 ms
20,748 KB
testcase_10 AC 225 ms
21,280 KB
testcase_11 AC 224 ms
22,284 KB
testcase_12 AC 200 ms
20,840 KB
testcase_13 AC 122 ms
12,840 KB
testcase_14 AC 209 ms
21,444 KB
testcase_15 AC 100 ms
9,236 KB
testcase_16 AC 126 ms
12,848 KB
testcase_17 AC 146 ms
20,828 KB
testcase_18 AC 169 ms
21,028 KB
testcase_19 AC 109 ms
9,044 KB
testcase_20 AC 95 ms
6,128 KB
testcase_21 AC 110 ms
8,164 KB
testcase_22 AC 98 ms
5,924 KB
testcase_23 AC 97 ms
5,920 KB
testcase_24 AC 108 ms
8,504 KB
testcase_25 AC 97 ms
6,056 KB
testcase_26 AC 102 ms
6,172 KB
testcase_27 AC 105 ms
8,548 KB
testcase_28 AC 106 ms
9,248 KB
testcase_29 AC 252 ms
37,844 KB
testcase_30 AC 254 ms
37,928 KB
testcase_31 AC 252 ms
38,544 KB
testcase_32 AC 250 ms
37,756 KB
testcase_33 AC 249 ms
37,356 KB
testcase_34 AC 82 ms
4,484 KB
testcase_35 AC 81 ms
4,380 KB
testcase_36 AC 79 ms
4,380 KB
testcase_37 AC 252 ms
37,592 KB
testcase_38 AC 243 ms
37,276 KB
testcase_39 AC 251 ms
37,348 KB
testcase_40 AC 245 ms
37,340 KB
testcase_41 AC 247 ms
38,520 KB
testcase_42 AC 240 ms
37,580 KB
testcase_43 AC 83 ms
4,448 KB
testcase_44 AC 84 ms
4,376 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