結果

問題 No.1917 LCMST
ユーザー rabimearabimea
提出日時 2023-01-27 01:22:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,596 bytes
コンパイル時間 2,894 ms
コンパイル使用メモリ 207,904 KB
実行使用メモリ 56,760 KB
最終ジャッジ日時 2023-09-09 22:42:08
合計ジャッジ時間 17,018 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 50 ms
16,548 KB
testcase_01 AC 48 ms
16,536 KB
testcase_02 AC 48 ms
16,500 KB
testcase_03 AC 52 ms
17,104 KB
testcase_04 AC 52 ms
17,104 KB
testcase_05 AC 54 ms
17,104 KB
testcase_06 AC 53 ms
17,108 KB
testcase_07 AC 53 ms
17,156 KB
testcase_08 AC 48 ms
16,532 KB
testcase_09 AC 332 ms
56,448 KB
testcase_10 AC 343 ms
56,552 KB
testcase_11 AC 338 ms
56,488 KB
testcase_12 AC 306 ms
39,164 KB
testcase_13 WA -
testcase_14 AC 321 ms
55,940 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 185 ms
21,700 KB
testcase_20 AC 171 ms
19,184 KB
testcase_21 AC 188 ms
22,048 KB
testcase_22 AC 173 ms
19,064 KB
testcase_23 AC 167 ms
19,048 KB
testcase_24 AC 185 ms
21,848 KB
testcase_25 AC 170 ms
19,552 KB
testcase_26 AC 178 ms
21,552 KB
testcase_27 AC 181 ms
21,532 KB
testcase_28 AC 185 ms
21,648 KB
testcase_29 AC 322 ms
56,748 KB
testcase_30 AC 320 ms
56,692 KB
testcase_31 AC 323 ms
56,752 KB
testcase_32 AC 324 ms
56,512 KB
testcase_33 AC 324 ms
56,760 KB
testcase_34 AC 143 ms
16,500 KB
testcase_35 AC 140 ms
16,476 KB
testcase_36 AC 140 ms
16,488 KB
testcase_37 AC 317 ms
56,548 KB
testcase_38 AC 320 ms
56,604 KB
testcase_39 AC 318 ms
56,748 KB
testcase_40 AC 323 ms
56,752 KB
testcase_41 AC 325 ms
56,760 KB
testcase_42 AC 325 ms
56,752 KB
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
 
#define fi first
#define se second
#define pb push_back
using vi = vector <int>;
using ll = long long;
using pii = pair <int, int>;
//~ const ll mod = 998244353;
const ll mod = 1e9 + 7;
ll qpow(ll a, ll b, ll m = mod) { ll r = 1, t = a;
	for(; b; b /= 2) { if(b & 1) r = r * t % m; t = t * t % m; } return r; }

const int N = 1e5 + 11;
vi divs[N];
vi muls[N];

int a[N];

int p[N];
int find(int x) {
	if(p[x] == x) return x;
	return p[x] = find(p[x]);
}

struct edge {
	int u, v;
	ll w;
	bool operator <(const edge &e) const {
		return w < e.w;
	}
};

ll lcm(ll a, ll b) {
	return a / __gcd(a, b) * b;
}

int main() {
	ios :: sync_with_stdio(false);
	
	int n, m; cin >> n;
	
	ll ans = 0;
	for(int i = 1; i <= n; i ++) {
		int x; cin >> x;
		if(a[x]) ans += x;
		a[x] = 1;
	}
	
	for(int i = 1; i < N; i ++)
		for(int j = i; j < N; j += i) {
			divs[j].pb(i);
			if(a[j]) muls[i].pb(j);
		}
	for(int i = 1; i < N; i ++)
		sort(muls[i].begin(), muls[i].end());
	
	vector <edge> edges;
	//~ for(int i = 1; i < N; i ++) if(a[i]) {
		//~ for(int x : divs[i]) {
			//~ if(muls[x].size())
				//~ edges.pb({i, muls[x][0], lcm(i, muls[x][0])});
		//~ }
	//~ }
	
	for(int i = 1; i < N; i ++) if(muls[i].size()) {
		int x = muls[i][0];
		for(int y : muls[i])
			edges.pb({x, y, lcm(x, y)});
	}
	
	sort(edges.begin(), edges.end());
	
	iota(p, p + N, 0);
	for(auto e : edges) {
		//~ cout << e.u << ' ' << e.v << ' ' << e.w << '\n';
		int u = e.u, v = e.v;
		u = find(u); v = find(v);
		if(u != v) {
			ans += e.w;
			p[u] = v;
		}
	}
	
	cout << ans << '\n';
}
0