結果

問題 No.1917 LCMST
ユーザー Kude
提出日時 2022-04-29 22:31:42
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,980 bytes
コンパイル時間 2,711 ms
コンパイル使用メモリ 233,736 KB
最終ジャッジ日時 2025-01-28 23:18:55
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 6 TLE * 33 MLE * 3
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic pop
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

constexpr int MX = 100010;
VI d[MX];

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for(int i = 1; i < MX; i++) {
    for(int j = i; j < MX; j += i) {
      d[j].emplace_back(i);
    }
  }
  int n;
  cin >> n;
  VI a(n);
  vector<set<pair<int, int>>> vals(MX);
  rep(i, n) {
    cin >> a[i];
    for(int x: d[a[i]]) {
      vals[x].emplace(a[i], i);
    }
  }
  ll ans = 0;
  dsu uf(n);
  while(true) {
    auto gs = uf.groups();
    if (gs.size() == 1) break;
    vector<P> todo;
    for(const auto& g: gs) {
      for(int i: g) {
        for(int x: d[a[i]]) {
          vals[x].erase({a[i], i});
        }
      }
      ll mn = 1002003004005006007;
      int i1 = -1, i2 = -1;
      for(int i: g) {
        for(int x: d[a[i]]) if (auto it = vals[x].begin(); it != vals[x].end()) {
          ll cost = ll(a[i] / x) * it->first;
          if (cost < mn) {
            mn = cost;
            i1 = i, i2 = it->second;
          }
        }
      }
      todo.emplace_back(i1, i2);
      for(int i: g) {
        for(int x: d[a[i]]) {
          vals[x].emplace(a[i], i);
        }
      }
    }
    for(auto [i, j]: todo) if (!uf.same(i, j)) {
      uf.merge(i, j);
      ans += lcm(a[i], a[j]);
    }
  }
  cout << ans << endl;
}
0