結果

問題 No.2756 GCD Teleporter
ユーザー haihamabossu
提出日時 2024-05-10 23:36:16
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 839 ms / 2,000 ms
コード長 1,108 bytes
コンパイル時間 1,870 ms
コンパイル使用メモリ 208,468 KB
最終ジャッジ日時 2025-02-21 13:22:51
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <atcoder/dsu>
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

const ll MX = 200'010;

void solve() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];
  ll min_pf = MX;
  vector has(MX + 10, vector<ll>());
  rep(i, n) {
    ll rem = a[i];
    for (ll j = 2; rem > 1 && j * j <= rem; j++) {
      if (rem % j == 0) {
        has[j].push_back(i);
        min_pf = min(min_pf, j);
      }
      while (rem % j == 0)
        rem /= j;
    }
    if (rem > 1) {
      has[rem].push_back(i);
      min_pf = min(min_pf, rem);
    }
  }
  atcoder::dsu dsu(n);
  rep(j, MX) {
    if (has[j].empty())
      continue;
    ll i1 = has[j][0];
    for (const auto i2 : has[j]) {
      if (i1 == i2 || dsu.same(i1, i2))
        continue;
      dsu.merge(i1, i2);
    }
  }
  ll sz = dsu.groups().size();
  ll ans = min(sz * 2, (sz - 1) * min_pf);
  cout << ans << '\n';
}

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  int T = 1;
  for (int t = 0; t < T; t++) {
    solve();
  }
  return 0;
}
0