結果
問題 | No.1917 LCMST |
ユーザー |
|
提出日時 | 2024-03-13 00:27:40 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,820 bytes |
コンパイル時間 | 1,774 ms |
コンパイル使用メモリ | 179,452 KB |
実行使用メモリ | 46,576 KB |
最終ジャッジ日時 | 2024-09-29 22:32:49 |
合計ジャッジ時間 | 12,748 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 WA * 1 |
other | AC * 1 WA * 41 |
ソースコード
#include<bits/stdc++.h>using namespace std;#define pb push_back#define fi first#define se second#define for1(i,j,k) for(int i=j;i<=k;i++)#define for2(i,j,k) for(int i=j;i>=k;i--)#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)#define bit(n,i) ((n>>i)&1)#define all(x) x.begin(),x.end()#define int long longtypedef long long ll;typedef pair<int,int> pii;typedef double ld;typedef pair<ld,ld> pdd;typedef pair<ll,ll> pll;const ll maxn=1e6+5;const ll offset=2e5;const ll blk=317;const ll inf=1e9;const int base=350;const ll mod=998244353;int n;int a[maxn],b[maxn];int par[maxn];int cnt[maxn];int Find(int u){return par[u]<0?u:par[u]=Find(par[u]);}bool Union(int u,int v){if ((u=Find(u))==(v=Find(v))) return false;if (par[u]> par[v]) swap(u,v);par[u]+=par[v];par[v]=u;return true;}vector<int> L;vector<pair<int,pii>> Edge;int lcm(int a,int b){return a * b / __gcd(a,b);}void sol() {cin >> n;for1(i,1,n) par[i]=-1;for1(i,1,n){cin >> a[i];cnt[a[i]]++;}sort(a+1,a+1+n);for1(i,1,n){if (b[a[i]]) Union(b[a[i]],i);else b[a[i]]=i;}for1(i,1,1e5){if(cnt[i] > 1) continue;L.clear();for3(j,i,1e5,i){if (b[j]) L.pb(j);}if (L.size()<2) continue;for1(j,1,L.size()-1) Edge.pb({lcm(L[0],L[j]),{L[0],L[j]}});}sort(all(Edge));int res=0;for(auto x:Edge){if (Union(b[x.se.fi],b[x.se.se])) res+=x.fi;}cout << res<<'\n';}int32_t main() {ios_base::sync_with_stdio(false);cin.tie(NULL);// freopen("paleta.inp","r",stdin);// freopen("paleta.out","w",stdout);int t=1;//cin >> t;while (t--) {sol();}}/*15 34 1 2 3 1*/