結果

問題 No.1917 LCMST
ユーザー Hamza MontherHamza Monther
提出日時 2023-02-10 21:50:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,728 bytes
コンパイル時間 3,257 ms
コンパイル使用メモリ 205,360 KB
実行使用メモリ 5,328 KB
最終ジャッジ日時 2023-09-21 22:59:48
合計ジャッジ時間 9,026 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
5,060 KB
testcase_01 AC 4 ms
4,844 KB
testcase_02 AC 3 ms
4,828 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 3 ms
4,856 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 AC 86 ms
4,872 KB
testcase_30 AC 87 ms
4,784 KB
testcase_31 AC 90 ms
4,872 KB
testcase_32 AC 86 ms
4,856 KB
testcase_33 AC 88 ms
4,776 KB
testcase_34 AC 82 ms
4,848 KB
testcase_35 AC 82 ms
4,796 KB
testcase_36 AC 82 ms
4,872 KB
testcase_37 AC 86 ms
4,848 KB
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct DSU {
    vector<int> Rank, Root, mn;
    DSU(int n) {
        Rank.resize(n + 1, 1);
        Root.resize(n + 1);
        iota(Root.begin(), Root.end(), 0);
        mn = Root;
    }
    int find(int Node) {
        return Root[Node] = (Node == Root[Node] ? Node : find(Root[Node]));
    }
    bool check(int a, int b) {
        return (find(a) == find(b));
    }
    void merge(int a, int b) {
        a = find(a);
        b = find(b);
        if (check(a, b)) return;
        if (Rank[a] > Rank[b]) swap(a, b);
        Root[a] = b;
        Rank[b] += Rank[a];
        mn[b] = min(mn[b], mn[a]);
    }
};
int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    const int N = 1e5 + 5;
    vector<int> mx(N, 0);
    vector<int> counter(N, 0);
    int n;
    cin >> n;
    for (int i = 0, x; i < n; ++i) {
        cin >> x;
        counter[x]++;
    }
    ll ans = 0;
    int mn = 1e9;
    for (int i = 1; i < N; ++i) {
        if (counter[i] == 0) {
            continue;
        }
        for (int j = i + i; j < N; j += i) {
            if (counter[j] > 0) {
                mx[j] = i;
            }
        }
        ans += 1ll * (counter[i] - 1) * i;
        if (mn == 1e9) {
            mn = i;
        }
    }
    assert(mn != 1e9);
    DSU dsu(N);
    for (int i = 1; i < N; ++i) {
        if (counter[i] != 0 && mx[i] != 0) {
            dsu.merge(i, mx[i]);
            ans += i;
        }
    }
    for (int i = 1; i < N; ++i) {
        if (counter[i] > 0 && dsu.find(i) == i && i != mn) {
            int x = dsu.mn[dsu.find(i)];
            ans += 1ll * x * mn / __gcd(x, mn);
        }
    }
    cout << ans << '\n';
    return 0;
}
0