結果

問題 No.3270 No Coprime Cycles
ユーザー risujiroh
提出日時 2025-09-12 22:43:36
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 292 ms / 2,000 ms
コード長 2,514 bytes
コンパイル時間 3,699 ms
コンパイル使用メモリ 306,632 KB
実行使用メモリ 41,504 KB
最終ジャッジ日時 2025-09-12 23:42:57
合計ジャッジ時間 11,888 ms
ジャッジサーバーID
(参考情報)
judge1 / judge7
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#if __INCLUDE_LEVEL__ == 0

#include __BASE_FILE__

void Solve() {
  int n;
  IN(n);
  vector<int> a(n);
  IN(a);

  int A = ranges::max(a);
  linear_sieve::init(A);

  vector<tuple<int64_t, int, int>> edges;

  for (int i : Rep(0, n)) {
    auto f = linear_sieve::factor(a[i]);
    for (int l = 0; l < Sz(f);) {
      int e = 1;
      while (l + e < Sz(f) && f[l] == f[l + e]) {
        ++e;
      }
      int p = f[l];
      edges.emplace_back(p * e, i, n + p);
      l += e;
    }
  }

  ranges::sort(edges);

  atcoder::dsu dsu(n + A + 1);
  int64_t ans = 0;
  for (auto [w, i, j] : Rev(edges)) {
    if (dsu.same(i, j)) {
      ans += w;
    } else {
      dsu.merge(i, j);
    }
  }

  OUT(ans);
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  Solve();
}

#elif __INCLUDE_LEVEL__ == 1

#include <bits/stdc++.h>

#include <atcoder/dsu.hpp>

namespace linear_sieve {

std::vector<int> primes, lpf;
void init(int n) {
  if (n < int(std::size(lpf))) return;
  if (n < 2 * int(std::size(lpf))) n = 2 * std::size(lpf);
  lpf.resize(n + 1, -1);
  for (int d = 2; d <= n; ++d) {
    if (lpf[d] == -1) lpf[d] = d, primes.push_back(d);
    for (int p : primes) {
      if (p * d > n or p > lpf[d]) break;
      lpf[p * d] = p;
    }
  }
}
std::vector<int> factor(int n) {
  __glibcxx_assert(n >= 1);
  std::vector<int> res;
  for (init(n); n > 1; n /= res.back()) res.push_back(lpf[n]);
  return res;
}

}  // namespace linear_sieve

template <class T> concept MyRange = std::ranges::range<T> && !std::convertible_to<T, std::string_view>;
template <class T> concept MyTuple = std::__is_tuple_like<T>::value && !MyRange<T>;

namespace std {

istream& operator>>(istream& is, MyRange auto&& r) {
  for (auto&& e : r) is >> e;
  return is;
}
istream& operator>>(istream& is, MyTuple auto&& t) {
  apply([&](auto&... xs) { (is >> ... >> xs); }, t);
  return is;
}

ostream& operator<<(ostream& os, MyRange auto&& r) {
  auto sep = "";
  for (auto&& e : r) os << exchange(sep, " ") << e;
  return os;
}
ostream& operator<<(ostream& os, MyTuple auto&& t) {
  auto sep = "";
  apply([&](auto&... xs) { ((os << exchange(sep, " ") << xs), ...); }, t);
  return os;
}

}  // namespace std

using namespace std;

#define Rev views::reverse
#define Rep(...) [](int l, int r) { return views::iota(min(l, r), r); }(__VA_ARGS__)
#define Sz(r) int(size(r))
#define IN(...) (cin >> forward_as_tuple(__VA_ARGS__))
#define OUT(...) (cout << forward_as_tuple(__VA_ARGS__) << '\n')

#endif  // __INCLUDE_LEVEL__ == 1
0