結果

問題 No.1917 LCMST
ユーザー inksamuraiinksamurai
提出日時 2022-04-30 21:28:02
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 397 ms / 4,000 ms
コード長 1,797 bytes
コンパイル時間 2,643 ms
コンパイル使用メモリ 212,584 KB
実行使用メモリ 51,356 KB
最終ジャッジ日時 2023-09-12 13:10:39
合計ジャッジ時間 15,594 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
5,704 KB
testcase_01 AC 6 ms
5,728 KB
testcase_02 AC 7 ms
5,788 KB
testcase_03 AC 9 ms
5,896 KB
testcase_04 AC 8 ms
5,980 KB
testcase_05 AC 8 ms
5,888 KB
testcase_06 AC 8 ms
5,892 KB
testcase_07 AC 8 ms
5,896 KB
testcase_08 AC 6 ms
5,732 KB
testcase_09 AC 383 ms
50,672 KB
testcase_10 AC 374 ms
50,172 KB
testcase_11 AC 371 ms
50,684 KB
testcase_12 AC 331 ms
40,284 KB
testcase_13 AC 201 ms
31,688 KB
testcase_14 AC 353 ms
50,528 KB
testcase_15 AC 167 ms
26,980 KB
testcase_16 AC 205 ms
31,208 KB
testcase_17 AC 258 ms
35,064 KB
testcase_18 AC 279 ms
38,004 KB
testcase_19 AC 167 ms
27,352 KB
testcase_20 AC 149 ms
24,948 KB
testcase_21 AC 171 ms
26,796 KB
testcase_22 AC 147 ms
24,992 KB
testcase_23 AC 148 ms
24,960 KB
testcase_24 AC 166 ms
26,804 KB
testcase_25 AC 148 ms
25,232 KB
testcase_26 AC 163 ms
27,440 KB
testcase_27 AC 161 ms
27,492 KB
testcase_28 AC 164 ms
27,024 KB
testcase_29 AC 396 ms
51,032 KB
testcase_30 AC 372 ms
50,224 KB
testcase_31 AC 377 ms
50,708 KB
testcase_32 AC 392 ms
50,388 KB
testcase_33 AC 397 ms
50,284 KB
testcase_34 AC 104 ms
21,220 KB
testcase_35 AC 107 ms
21,184 KB
testcase_36 AC 103 ms
21,076 KB
testcase_37 AC 387 ms
51,152 KB
testcase_38 AC 389 ms
50,500 KB
testcase_39 AC 388 ms
50,316 KB
testcase_40 AC 381 ms
51,356 KB
testcase_41 AC 381 ms
50,452 KB
testcase_42 AC 393 ms
50,332 KB
testcase_43 AC 106 ms
21,740 KB
testcase_44 AC 107 ms
21,692 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3k5NXAD ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e

struct dsu{
	int n,cmps;
	vector<int> par,siz;
	dsu(int m){init(m);}
	void init(int m){
		n=m;
		cmps=m;
		par.resize(n,0);
		siz.resize(n,0);
		for(int i=0;i<n;i++){
			par[i]=i;
			siz[i]=1;
		}
	}
	void merge(int v,int u){
		v=parent(v),u=parent(u);
		if(v==u)return;
		if(siz[v]<siz[u])swap(v,u);
		cmps-=1;
		siz[v]+=siz[u];
		par[u]=v;
	}
	int parent(int v){
		return par[v]==v?v:parent(par[v]);
	}
	bool same(int v, int u){
		return parent(v)==parent(u);
	}
	int size(int v=-1){
		return (v==-1?cmps:siz[parent(v)]);
	}
};

const int nax=100001;
vi rbts[nax];

signed main(){
_3k5NXAD;
	int n;
	cin>>n;
	vi a(n);
	rep(i,n){
		cin>>a[i];
	}
	rep(i,n){
		rbts[a[i]].pb(i);
	}
	using tup=tuple<ll,int,int>;
	vec(tup) edges;
	rng(i,1,nax){
		int rt=-1,irt=-1;
		for(int j=i;j<nax;j+=i){
			if(sz(rbts[j])){
				rt=j;
				irt=rbts[j][0];
				break;
			}
		}
		if(rt==-1) continue;
		for(int j=rt;j<nax;j+=i){
			if(sz(rbts[j])) edges.pb({(1ll*rt*j)/i,rbts[j][0],irt});
		}
	}
	sort(edges.begin(),edges.end());
	dsu uf(n);
	ll ans=0;
	rep(i,nax){
		for(auto &u:rbts[i]){
			if(u==rbts[i][0]) continue;
			ans+=i;
			uf.merge(rbts[i][0],u);
		}
	}
	for(auto &[cosu,u,v]:edges){
		if(uf.same(u,v)) continue;
		ans+=cosu;
		uf.merge(u,v);
	}
	print(ans);
//
	return 0;
}
0