結果
問題 | No.1917 LCMST |
ユーザー |
|
提出日時 | 2023-02-11 02:39:42 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 185 ms / 4,000 ms |
コード長 | 981 bytes |
コンパイル時間 | 2,545 ms |
コンパイル使用メモリ | 203,328 KB |
最終ジャッジ日時 | 2025-02-10 14:01:10 |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 42 |
ソースコード
#include<bits/stdc++.h> using namespace std; using ll=long long; using pii=pair<int,int>; #define all(a) a.begin(),a.end() #define pb push_back #define sz(a) ((int)a.size()) const int maxn=100005; int par[maxn]; int Find(int x){return x==par[x]?x:par[x]=Find(par[x]);} signed main(){ ios_base::sync_with_stdio(0),cin.tie(0); int n; cin >> n; vector<int> a(n),cnt(maxn); vector<array<ll,3>> e; for(int i=0; i<maxn; ++i) par[i]=i; for(auto& i: a) cin >> i,cnt[i]++; ll res=0; for(int i=1; i<maxn; ++i) if(cnt[i]){ res+=1ll*(cnt[i]-1)*i,cnt[i]=1; for(int j=i*2; j<maxn; j+=i) res+=1ll*cnt[j]*j,cnt[j]=0; } for(int i=1; i<maxn; ++i){ vector<int> vec; for(int j=i; j<maxn; j+=i) if(cnt[j]) vec.pb(j); for(int j=1; j<vec.size(); ++j) e.pb({1ll*vec[0]*vec[j]/i,vec[0],vec[j]}); } sort(all(e)); for(auto& [w,u,v]: e) if(Find(u)!=Find(v)) par[Find(u)]=Find(v),res+=w; cout << res << "\n"; }