結果

問題 No.1917 LCMST
ユーザー rabimearabimea
提出日時 2023-01-27 01:58:06
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 450 ms / 4,000 ms
コード長 1,423 bytes
コンパイル時間 3,600 ms
コンパイル使用メモリ 208,468 KB
実行使用メモリ 74,932 KB
最終ジャッジ日時 2023-09-09 23:13:50
合計ジャッジ時間 20,275 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 63 ms
16,952 KB
testcase_01 AC 64 ms
16,848 KB
testcase_02 AC 65 ms
16,964 KB
testcase_03 AC 82 ms
17,856 KB
testcase_04 AC 79 ms
17,916 KB
testcase_05 AC 79 ms
17,916 KB
testcase_06 AC 76 ms
17,848 KB
testcase_07 AC 70 ms
17,908 KB
testcase_08 AC 66 ms
16,896 KB
testcase_09 AC 435 ms
74,084 KB
testcase_10 AC 450 ms
74,476 KB
testcase_11 AC 445 ms
73,660 KB
testcase_12 AC 414 ms
48,084 KB
testcase_13 AC 270 ms
32,404 KB
testcase_14 AC 442 ms
73,916 KB
testcase_15 AC 224 ms
25,428 KB
testcase_16 AC 272 ms
33,128 KB
testcase_17 AC 358 ms
47,264 KB
testcase_18 AC 358 ms
46,552 KB
testcase_19 AC 220 ms
25,716 KB
testcase_20 AC 203 ms
20,864 KB
testcase_21 AC 235 ms
24,840 KB
testcase_22 AC 209 ms
21,160 KB
testcase_23 AC 196 ms
21,396 KB
testcase_24 AC 227 ms
24,648 KB
testcase_25 AC 204 ms
21,156 KB
testcase_26 AC 208 ms
23,960 KB
testcase_27 AC 210 ms
25,644 KB
testcase_28 AC 223 ms
24,444 KB
testcase_29 AC 369 ms
73,996 KB
testcase_30 AC 365 ms
73,752 KB
testcase_31 AC 369 ms
74,932 KB
testcase_32 AC 401 ms
74,884 KB
testcase_33 AC 355 ms
74,172 KB
testcase_34 AC 160 ms
16,852 KB
testcase_35 AC 157 ms
17,080 KB
testcase_36 AC 159 ms
16,860 KB
testcase_37 AC 403 ms
74,332 KB
testcase_38 AC 396 ms
74,424 KB
testcase_39 AC 406 ms
74,380 KB
testcase_40 AC 412 ms
73,872 KB
testcase_41 AC 403 ms
74,296 KB
testcase_42 AC 422 ms
74,356 KB
testcase_43 AC 163 ms
17,512 KB
testcase_44 AC 163 ms
17,604 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:41:1: 警告: ISO C++ では型の無い ‘main’ の宣言を禁止しています [-Wreturn-type]
   41 | main() {
      | ^~~~

ソースコード

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; }

#define int long long

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;
}

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 ++)
		if(muls[i].size()) 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