結果

問題 No.1917 LCMST
ユーザー ぷらぷら
提出日時 2022-04-29 21:59:09
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 509 ms / 4,000 ms
コード長 1,982 bytes
コンパイル時間 3,511 ms
コンパイル使用メモリ 210,540 KB
実行使用メモリ 42,528 KB
最終ジャッジ日時 2023-09-11 13:06:48
合計ジャッジ時間 21,360 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
4,380 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 7 ms
4,712 KB
testcase_04 AC 7 ms
4,448 KB
testcase_05 AC 7 ms
4,704 KB
testcase_06 AC 7 ms
4,528 KB
testcase_07 AC 7 ms
4,704 KB
testcase_08 AC 4 ms
4,380 KB
testcase_09 AC 509 ms
42,528 KB
testcase_10 AC 505 ms
42,160 KB
testcase_11 AC 504 ms
41,496 KB
testcase_12 AC 486 ms
26,312 KB
testcase_13 AC 349 ms
16,640 KB
testcase_14 AC 507 ms
41,756 KB
testcase_15 AC 307 ms
12,336 KB
testcase_16 AC 353 ms
16,444 KB
testcase_17 AC 403 ms
24,844 KB
testcase_18 AC 432 ms
26,300 KB
testcase_19 AC 309 ms
12,872 KB
testcase_20 AC 288 ms
10,116 KB
testcase_21 AC 313 ms
13,032 KB
testcase_22 AC 290 ms
10,312 KB
testcase_23 AC 290 ms
10,044 KB
testcase_24 AC 312 ms
12,548 KB
testcase_25 AC 290 ms
10,212 KB
testcase_26 AC 298 ms
13,060 KB
testcase_27 AC 301 ms
13,408 KB
testcase_28 AC 305 ms
12,804 KB
testcase_29 AC 458 ms
41,696 KB
testcase_30 AC 458 ms
41,796 KB
testcase_31 AC 458 ms
41,284 KB
testcase_32 AC 459 ms
42,044 KB
testcase_33 AC 458 ms
41,800 KB
testcase_34 AC 259 ms
7,952 KB
testcase_35 AC 259 ms
7,892 KB
testcase_36 AC 258 ms
7,840 KB
testcase_37 AC 461 ms
41,696 KB
testcase_38 AC 467 ms
42,180 KB
testcase_39 AC 462 ms
40,976 KB
testcase_40 AC 462 ms
41,240 KB
testcase_41 AC 465 ms
42,104 KB
testcase_42 AC 471 ms
41,240 KB
testcase_43 AC 262 ms
8,472 KB
testcase_44 AC 262 ms
8,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

long long gcd2(long long a,long long b) {
    if(a == 0 || b == 0) {
        return max(a,b);
    }
    if(a % b == 0) {
        return b;
    }
    else {
        return gcd2(b,a % b);
    }
}

struct UnionFind {
    vector<int> par;
    vector<int> size;
    UnionFind(int n) {
        par.resize(n);
        size.resize(n,1);
        for(int i = 0; i < n; i++) {
            par[i] = i;
        }
    }
    int find(int x) {
        if(par[x] == x) {
            return x;
        }
        return par[x] = find(par[x]);
    }
    bool same(int x, int y) {
        return find(x) == find(y);
    }
    int consize(int x) {
        return size[find(x)];
    }
    bool unite(int x, int y) {
        x = find(x);
        y = find(y);
        if(x == y) {
            return false;
        }
        if(size[x] > size[y]) {
            swap(x,y);
        }
        par[x] = y;
        size[y] += size[x];
        return true;
    }
};

int main() {
    int N;
    cin >> N;
    vector<int>A(N),B(100001);
    long long ans = 0;
    int cnt = 0;
    for(int i = 0; i < N; i++) {
        cin >> A[i];
        if(B[A[i]]) {
            ans += A[i];
            continue;
        }
        cnt++;
        B[A[i]] = 1;
    }
    UnionFind uf(100001);
    vector<pair<long long,pair<int,int>>>tmp;
    for(int i = 1; i <= 100000; i++) {
        int mi = -1;
        for(int j = 1; i*j <= 100000; j++) {
            if(B[i*j]) {
                mi = i*j;
                break;
            }
        }
        if(mi == -1) {
            continue;
        }
        for(int j = 1; i*j <= 100000; j++) {
            if(B[i*j]) {
                tmp.push_back({1ll*mi/gcd2(mi,i*j)*(i*j),{mi,i*j}});
            }
        }
    }
    sort(tmp.begin(),tmp.end());
    for(int i = 0; i < tmp.size(); i++) {
        if(uf.unite(tmp[i].second.first,tmp[i].second.second)) {
            ans += tmp[i].first;
        }
    }
    cout << ans << endl;
}
0