結果

問題 No.1917 LCMST
ユーザー KKT89KKT89
提出日時 2022-04-30 17:42:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 329 ms / 4,000 ms
コード長 2,300 bytes
コンパイル時間 1,901 ms
コンパイル使用メモリ 147,240 KB
実行使用メモリ 42,484 KB
最終ジャッジ日時 2023-09-12 10:06:42
合計ジャッジ時間 13,884 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 6 ms
4,376 KB
testcase_04 AC 6 ms
4,376 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 6 ms
4,376 KB
testcase_07 AC 6 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 310 ms
24,384 KB
testcase_10 AC 304 ms
24,656 KB
testcase_11 AC 303 ms
24,532 KB
testcase_12 AC 277 ms
24,584 KB
testcase_13 AC 198 ms
16,576 KB
testcase_14 AC 286 ms
24,308 KB
testcase_15 AC 176 ms
12,256 KB
testcase_16 AC 202 ms
18,036 KB
testcase_17 AC 222 ms
24,564 KB
testcase_18 AC 247 ms
25,716 KB
testcase_19 AC 179 ms
12,200 KB
testcase_20 AC 167 ms
9,792 KB
testcase_21 AC 184 ms
12,484 KB
testcase_22 AC 167 ms
9,868 KB
testcase_23 AC 168 ms
9,880 KB
testcase_24 AC 181 ms
12,736 KB
testcase_25 AC 169 ms
9,892 KB
testcase_26 AC 172 ms
9,880 KB
testcase_27 AC 176 ms
13,316 KB
testcase_28 AC 179 ms
12,356 KB
testcase_29 AC 327 ms
41,784 KB
testcase_30 AC 329 ms
41,388 KB
testcase_31 AC 325 ms
41,084 KB
testcase_32 AC 323 ms
41,024 KB
testcase_33 AC 327 ms
42,220 KB
testcase_34 AC 99 ms
7,504 KB
testcase_35 AC 99 ms
7,580 KB
testcase_36 AC 100 ms
7,424 KB
testcase_37 AC 327 ms
40,908 KB
testcase_38 AC 328 ms
40,684 KB
testcase_39 AC 327 ms
41,648 KB
testcase_40 AC 323 ms
40,872 KB
testcase_41 AC 328 ms
41,592 KB
testcase_42 AC 323 ms
42,484 KB
testcase_43 AC 101 ms
8,048 KB
testcase_44 AC 103 ms
8,132 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <chrono>
#include <random>
#include <numeric>
#include <set>
#include <deque>
#include <stack>
#include <sstream>
#include <utility>
#include <cstring>
#include <unordered_map>
#include <unordered_set>
#include <tuple>
#include <array>
#include <bitset>
using namespace std;
typedef long long int ll;
typedef unsigned long long ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

struct UnionFind{
    vector<int> par,num;
    UnionFind(int n):par(n),num(n,1){
        iota(par.begin(),par.end(),0);  //include<numeric>
    }
    int find(int v){
        return (par[v]==v)?v:(par[v]=find(par[v]));
    }
    void unite(int u,int v){
        u=find(u),v=find(v);
        if(u==v)return;
        if(num[u]<num[v])swap(u,v);
        num[u]+=num[v];
        par[v]=u;
    }
    bool same(int u,int v){
        return find(u) == find(v);
    }
    int size(int v){
        return num[find(v)];
    }
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<int> a(n);
    for(int i=0;i<n;i++){
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    int m = a.back();
    vector<bool> ex(m+1);
    ll res = 0;
    for(int i=0;i<n;i++){
        ex[a[i]] = 1;
        if(i and a[i] == a[i-1]){
            res += a[i];
        }
    }
    UnionFind uf(m+1);
    vector<pair<ll,pair<int,int>>> v;
    for(int g=1;g<=m;g++){
        int f = -1;
        for(int i=g;i<=m;i+=g){
            if(ex[i]){
                if(f == -1){
                    f = i;
                }
                else{
                    v.push_back({(ll)f*(ll)i/g, {f,i}});
                }
            }
        }
    }
    sort(v.begin(), v.end());
    for(auto p:v){
        int x = p.second.first, y = p.second.second;
        if(!uf.same(x,y)){
            uf.unite(x,y);
            res += p.first;
        }
    }
    cout << res << endl;
}

0