結果

問題 No.1917 LCMST
ユーザー 👑 kmjpkmjp
提出日時 2022-04-29 22:38:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 351 ms / 4,000 ms
コード長 2,159 bytes
コンパイル時間 2,263 ms
コンパイル使用メモリ 211,364 KB
実行使用メモリ 17,712 KB
最終ジャッジ日時 2023-09-11 14:00:55
合計ジャッジ時間 13,152 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,148 KB
testcase_01 AC 5 ms
8,188 KB
testcase_02 AC 5 ms
8,144 KB
testcase_03 AC 8 ms
8,768 KB
testcase_04 AC 8 ms
8,696 KB
testcase_05 AC 8 ms
8,836 KB
testcase_06 AC 8 ms
8,844 KB
testcase_07 AC 8 ms
8,652 KB
testcase_08 AC 6 ms
8,060 KB
testcase_09 AC 240 ms
17,464 KB
testcase_10 AC 247 ms
17,508 KB
testcase_11 AC 233 ms
17,264 KB
testcase_12 AC 196 ms
16,780 KB
testcase_13 AC 126 ms
13,056 KB
testcase_14 AC 215 ms
17,120 KB
testcase_15 AC 105 ms
11,284 KB
testcase_16 AC 125 ms
13,392 KB
testcase_17 AC 151 ms
14,940 KB
testcase_18 AC 165 ms
15,604 KB
testcase_19 AC 112 ms
11,004 KB
testcase_20 AC 104 ms
10,352 KB
testcase_21 AC 120 ms
11,160 KB
testcase_22 AC 104 ms
10,304 KB
testcase_23 AC 102 ms
10,124 KB
testcase_24 AC 116 ms
11,064 KB
testcase_25 AC 106 ms
10,560 KB
testcase_26 AC 111 ms
10,512 KB
testcase_27 AC 110 ms
10,736 KB
testcase_28 AC 111 ms
10,724 KB
testcase_29 AC 349 ms
17,600 KB
testcase_30 AC 351 ms
17,516 KB
testcase_31 AC 351 ms
17,532 KB
testcase_32 AC 349 ms
17,656 KB
testcase_33 AC 350 ms
17,712 KB
testcase_34 AC 82 ms
8,076 KB
testcase_35 AC 85 ms
8,144 KB
testcase_36 AC 84 ms
7,992 KB
testcase_37 AC 341 ms
17,608 KB
testcase_38 AC 343 ms
17,456 KB
testcase_39 AC 338 ms
17,668 KB
testcase_40 AC 338 ms
17,472 KB
testcase_41 AC 327 ms
17,656 KB
testcase_42 AC 308 ms
17,520 KB
testcase_43 AC 86 ms
8,764 KB
testcase_44 AC 87 ms
8,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------


int N;
int C[1010101];

ll mi[101010];
ll base[101010];
vector<int> cand[101010];

template<int um> class UF {
	public:
	vector<int> par,rank,cnt;
	UF() {par=rank=vector<int>(um,0); cnt=vector<int>(um,1); for(int i=0;i<um;i++) par[i]=i;}
	void reinit(int num=um) {int i; FOR(i,num) rank[i]=0,cnt[i]=1,par[i]=i;}
	int operator[](int x) {return (par[x]==x)?(x):(par[x] = operator[](par[x]));}
	int count(int x) { return cnt[operator[](x)];}
	int operator()(int x,int y) {
		if((x=operator[](x))==(y=operator[](y))) return x;
		cnt[y]=cnt[x]=cnt[x]+cnt[y];
		if(rank[x]>rank[y]) return par[x]=y;
		rank[x]+=rank[x]==rank[y]; return par[y]=x;
	}
};
UF<101010> uf;

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N;
	ll ret=0;
	FOR(i,N) {
		cin>>x;
		if(C[x]) ret+=x;
		else C[x]=1;
	}
	
	for(i=1;i<=100000;i++) {
		for(j=i;j<=100000;j+=i) if(C[j]) cand[i].push_back(j);
	}
	
	priority_queue<pair<ll,int>> Q;
	for(i=1;i<=100000;i++) if(cand[i].size()>1) {
		reverse(ALL(cand[i]));
		base[i]=cand[i].back();
		cand[i].pop_back();
		Q.push({-cand[i].back()*base[i]/i,i});
	}
	
	while(Q.size()) {
		ll co=-Q.top().first;
		i=Q.top().second;
		Q.pop();
		if(uf[base[i]]!=uf[cand[i].back()]) {
			ret+=co;
			uf(base[i],cand[i].back());
		}
		cand[i].pop_back();
		if(cand[i].size()) {
			Q.push({-cand[i].back()*base[i]/i,i});
		}
	}
	
	
	cout<<ret<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0