結果

問題 No.1917 LCMST
ユーザー fumofumofunifumofumofuni
提出日時 2022-04-29 22:25:55
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 2,425 bytes
コンパイル時間 2,508 ms
コンパイル使用メモリ 215,740 KB
実行使用メモリ 814,568 KB
最終ジャッジ日時 2023-09-11 13:46:37
合計ジャッジ時間 10,579 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 88 ms
17,280 KB
testcase_04 AC 91 ms
17,484 KB
testcase_05 AC 90 ms
16,544 KB
testcase_06 AC 88 ms
16,440 KB
testcase_07 AC 89 ms
17,016 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 MLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=(n)-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[8]={1,0,-1,0,1,1,-1,-1};
const ll dx[8]={0,1,0,-1,1,-1,1,-1};
template <typename T> inline bool chmax(T &a, T b) {
  return ((a < b) ? (a = b, true) : (false));
}
template <typename T> inline bool chmin(T &a, T b) {
  return ((a > b) ? (a = b, true) : (false));
}

struct UnionFind {
    vector<int> par;
    vector<int> edge;
    
    UnionFind(int n) : par(n, -1),edge(n, 0) {}

    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool same(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) {
            edge[x]++;
            return false;
        }
        if (par[x] > par[y]) swap(x, y);
        par[x] += par[y];
        par[y] = x;
        edge[x] += edge[y]+1;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};


int main(){
  ll n;cin >> n;
  vl v(n);rep(i,n)cin >> v[i];
  sort(all(v));
  vl pls(100010);
  ll ans=0;
  rep(i,n){
    if(pls[v[i]])ans+=v[i];
    pls[v[i]]++;
  }
  for(ll k=1;k<100010;k++){
    if(!pls[k])continue;
    for(ll j=k*2;j<100010;j+=k){
      if(pls[j]){
        pls[j]=0;
        ans+=j;
      }
    }
  }
  //cout << ans << endl;
  vl pos;
  vector<pair<ll,pl>> eds;
  rep(i,100010)if(pls[i])pos.emplace_back(i);
  ll m=pos.size();
  UnionFind uf(m);
  //rep(i,100010)if(pls[i])cout << i <<" ";cout << endl;
  rep(i,m){
    rep(j,i){
      eds.emplace_back(lcm(pos[i],pos[j]),make_pair(i,j));
    }
  }
  sort(all(eds));
  for(auto [cost,p]:eds){
    if(uf.same(p.first,p.second))continue;
    uf.merge(p.first,p.second);ans+=cost;
    if(uf.size(0)==m)break;
  }
  cout << ans << endl;
} 
0