結果

問題 No.1917 LCMST
ユーザー inksamuraiinksamurai
提出日時 2022-04-30 21:26:21
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,797 bytes
コンパイル時間 2,515 ms
コンパイル使用メモリ 212,412 KB
実行使用メモリ 52,128 KB
最終ジャッジ日時 2023-09-12 13:09:39
合計ジャッジ時間 18,375 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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+=u;
			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