結果

問題 No.1917 LCMST
ユーザー KudeKude
提出日時 2022-04-29 22:31:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,980 bytes
コンパイル時間 2,595 ms
コンパイル使用メモリ 245,232 KB
実行使用メモリ 299,732 KB
最終ジャッジ日時 2023-09-11 13:52:44
合計ジャッジ時間 10,994 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
22,540 KB
testcase_01 AC 52 ms
18,092 KB
testcase_02 AC 51 ms
17,952 KB
testcase_03 AC 59 ms
18,732 KB
testcase_04 AC 63 ms
18,628 KB
testcase_05 AC 66 ms
18,624 KB
testcase_06 AC 63 ms
18,536 KB
testcase_07 AC 61 ms
18,620 KB
testcase_08 AC 52 ms
18,224 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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