結果

問題 No.1917 LCMST
ユーザー SSRSSSRS
提出日時 2022-04-29 22:07:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 416 ms / 4,000 ms
コード長 1,438 bytes
コンパイル時間 1,897 ms
コンパイル使用メモリ 179,500 KB
実行使用メモリ 41,792 KB
最終ジャッジ日時 2023-09-11 13:21:56
合計ジャッジ時間 18,598 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,376 KB
testcase_01 AC 4 ms
4,380 KB
testcase_02 AC 4 ms
4,376 KB
testcase_03 AC 5 ms
4,376 KB
testcase_04 AC 5 ms
4,380 KB
testcase_05 AC 6 ms
4,376 KB
testcase_06 AC 5 ms
4,376 KB
testcase_07 AC 5 ms
4,376 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 388 ms
24,340 KB
testcase_10 AC 392 ms
24,612 KB
testcase_11 AC 388 ms
25,088 KB
testcase_12 AC 369 ms
25,704 KB
testcase_13 AC 296 ms
15,920 KB
testcase_14 AC 373 ms
24,480 KB
testcase_15 AC 279 ms
13,224 KB
testcase_16 AC 300 ms
15,924 KB
testcase_17 AC 319 ms
24,404 KB
testcase_18 AC 340 ms
25,048 KB
testcase_19 AC 284 ms
13,324 KB
testcase_20 AC 273 ms
9,736 KB
testcase_21 AC 287 ms
12,040 KB
testcase_22 AC 276 ms
9,380 KB
testcase_23 AC 273 ms
9,524 KB
testcase_24 AC 285 ms
12,032 KB
testcase_25 AC 276 ms
9,464 KB
testcase_26 AC 277 ms
9,852 KB
testcase_27 AC 280 ms
12,248 KB
testcase_28 AC 281 ms
12,848 KB
testcase_29 AC 409 ms
41,452 KB
testcase_30 AC 410 ms
40,988 KB
testcase_31 AC 410 ms
41,200 KB
testcase_32 AC 413 ms
41,792 KB
testcase_33 AC 411 ms
41,176 KB
testcase_34 AC 261 ms
7,608 KB
testcase_35 AC 261 ms
7,992 KB
testcase_36 AC 263 ms
7,840 KB
testcase_37 AC 413 ms
40,716 KB
testcase_38 AC 411 ms
40,588 KB
testcase_39 AC 416 ms
40,696 KB
testcase_40 AC 409 ms
41,732 KB
testcase_41 AC 407 ms
40,964 KB
testcase_42 AC 403 ms
41,428 KB
testcase_43 AC 262 ms
7,848 KB
testcase_44 AC 261 ms
8,048 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
const int MAX = 100001;
struct unionfind{
	vector<int> p;
	unionfind(int N){
		p = vector<int>(N, -1);
	}
	int root(int x){
		if (p[x] < 0){
			return x;
		} else {
			p[x] = root(p[x]);
			return p[x];
		}
	}
	bool same(int x, int y){
		return root(x) == root(y);
	}
	void unite(int x, int y){
		x = root(x);
		y = root(y);
		if (x != y){
			if (p[x] < p[y]){
				swap(x, y);
			}
			p[y] += p[x];
			p[x] = y;
		}
	}
};
int main(){
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++){
    cin >> A[i];
  }
  vector<int> cnt(MAX, 0);
  for (int i = 0; i < N; i++){
    cnt[A[i]]++;
  }
  vector<tuple<long long, int, int>> E;
  for (int i = 1; i < MAX; i++){
    vector<int> p;
    for (int j = i; j < MAX; j += i){
      if (cnt[j] > 0){
        p.push_back(j);
      }
    }
    if (p.size() >= 2){
      int cnt2 = p.size();
      for (int j = 1; j < cnt2; j++){
        long long c = (long long) p[0] * p[j] / i;
        E.push_back(make_tuple(c, p[0], p[j]));
      }
    }
  }
  sort(E.begin(), E.end());
  int M = E.size();
  unionfind UF(MAX);
  long long ans = 0;
  for (int i = 0; i < M; i++){
    int u = get<1>(E[i]);
    int v = get<2>(E[i]);
    if (!UF.same(u, v)){
      UF.unite(u, v);
      ans += get<0>(E[i]);
    }
  }
  for (int i = 0; i < MAX; i++){
    if (cnt[i] >= 2){
      ans += (long long) i * (cnt[i] - 1);
    }
  }
  cout << ans << endl;
}
0