結果

問題 No.1917 LCMST
ユーザー rabimearabimea
提出日時 2023-01-27 01:39:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,385 bytes
コンパイル時間 3,740 ms
コンパイル使用メモリ 208,004 KB
実行使用メモリ 182,184 KB
最終ジャッジ日時 2023-09-09 22:59:02
合計ジャッジ時間 127,968 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,381 ms
144,900 KB
testcase_01 AC 2,501 ms
145,148 KB
testcase_02 AC 2,464 ms
144,860 KB
testcase_03 AC 2,376 ms
145,884 KB
testcase_04 AC 2,413 ms
145,856 KB
testcase_05 AC 2,400 ms
146,016 KB
testcase_06 AC 2,374 ms
145,836 KB
testcase_07 AC 2,372 ms
145,692 KB
testcase_08 AC 2,472 ms
145,040 KB
testcase_09 AC 2,871 ms
180,364 KB
testcase_10 AC 2,821 ms
180,420 KB
testcase_11 AC 2,869 ms
181,824 KB
testcase_12 AC 3,033 ms
167,940 KB
testcase_13 WA -
testcase_14 AC 2,808 ms
181,400 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 AC 2,512 ms
151,336 KB
testcase_20 AC 2,676 ms
148,096 KB
testcase_21 AC 2,549 ms
151,920 KB
testcase_22 AC 2,647 ms
148,228 KB
testcase_23 AC 2,606 ms
149,756 KB
testcase_24 AC 2,481 ms
151,696 KB
testcase_25 AC 2,501 ms
149,908 KB
testcase_26 AC 2,552 ms
150,580 KB
testcase_27 AC 2,521 ms
151,048 KB
testcase_28 AC 2,544 ms
151,052 KB
testcase_29 AC 2,758 ms
182,184 KB
testcase_30 AC 2,684 ms
182,012 KB
testcase_31 AC 2,747 ms
182,116 KB
testcase_32 AC 2,707 ms
182,108 KB
testcase_33 AC 2,709 ms
181,924 KB
testcase_34 AC 2,543 ms
146,560 KB
testcase_35 AC 2,527 ms
146,492 KB
testcase_36 AC 2,467 ms
146,480 KB
testcase_37 AC 2,735 ms
182,004 KB
testcase_38 AC 2,779 ms
181,920 KB
testcase_39 AC 2,744 ms
181,984 KB
testcase_40 AC 2,731 ms
182,144 KB
testcase_41 AC 2,783 ms
182,084 KB
testcase_42 AC 2,784 ms
181,912 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 = 1e6 + 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])});
		}
	}
	
	sort(edges.begin(), edges.end());
	
	iota(p, p + N, 0);
	for(auto e : edges) {
		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