結果

問題 No.1917 LCMST
ユーザー Trí Tâm NguyễnTrí Tâm Nguyễn
提出日時 2023-02-11 18:17:04
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 721 ms / 4,000 ms
コード長 1,987 bytes
コンパイル時間 4,406 ms
コンパイル使用メモリ 209,432 KB
実行使用メモリ 137,836 KB
最終ジャッジ日時 2023-09-22 14:18:02
合計ジャッジ時間 25,051 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
58,292 KB
testcase_01 AC 94 ms
58,328 KB
testcase_02 AC 97 ms
58,096 KB
testcase_03 AC 100 ms
58,452 KB
testcase_04 AC 101 ms
58,452 KB
testcase_05 AC 101 ms
58,428 KB
testcase_06 AC 99 ms
58,584 KB
testcase_07 AC 102 ms
58,448 KB
testcase_08 AC 105 ms
58,032 KB
testcase_09 AC 708 ms
136,780 KB
testcase_10 AC 693 ms
136,528 KB
testcase_11 AC 696 ms
136,544 KB
testcase_12 AC 639 ms
131,712 KB
testcase_13 AC 430 ms
124,500 KB
testcase_14 AC 708 ms
134,024 KB
testcase_15 AC 376 ms
120,932 KB
testcase_16 AC 454 ms
124,604 KB
testcase_17 AC 536 ms
127,304 KB
testcase_18 AC 591 ms
128,492 KB
testcase_19 AC 398 ms
120,720 KB
testcase_20 AC 351 ms
119,212 KB
testcase_21 AC 402 ms
120,320 KB
testcase_22 AC 356 ms
119,460 KB
testcase_23 AC 368 ms
119,116 KB
testcase_24 AC 405 ms
120,456 KB
testcase_25 AC 356 ms
119,732 KB
testcase_26 AC 398 ms
122,188 KB
testcase_27 AC 388 ms
122,184 KB
testcase_28 AC 387 ms
121,244 KB
testcase_29 AC 672 ms
137,836 KB
testcase_30 AC 697 ms
137,628 KB
testcase_31 AC 645 ms
137,564 KB
testcase_32 AC 661 ms
137,420 KB
testcase_33 AC 671 ms
137,656 KB
testcase_34 AC 240 ms
102,804 KB
testcase_35 AC 244 ms
102,948 KB
testcase_36 AC 243 ms
102,960 KB
testcase_37 AC 656 ms
137,612 KB
testcase_38 AC 660 ms
137,564 KB
testcase_39 AC 653 ms
137,708 KB
testcase_40 AC 660 ms
137,744 KB
testcase_41 AC 664 ms
137,492 KB
testcase_42 AC 721 ms
137,248 KB
testcase_43 AC 241 ms
102,964 KB
testcase_44 AC 248 ms
103,156 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// Make the best become better
// No room for laziness
#include<bits/stdc++.h>

#define int long long
#define pb push_back
#define fi first
#define se second
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxN = 1e6 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
int n;
int a[maxN];
vector<int> vc[maxN];
vector<int> L[maxN];
int lab[maxN];
int Findset(int u)
{
    return lab[u] < 0 ? u : lab[u] = Findset(lab[u]);
}
bool Unite(int u, int v)
{
    int r = Findset(u), s = Findset(v);
    if(r == s) return false;
    if(lab[r] > lab[s]) swap(r, s);
    lab[r] += lab[s];
    lab[s] = r;
    return true;
}
struct TEdge
{
    int u, v, w;
};
vector<TEdge> e;
void ReadInput()
{
    cin >> n;
    for(int i=1; i<=n; i++)
    {
        cin >> a[i];
        vc[a[i]].pb(i);
    }
}
void Solve()
{
    for(int i=1; i<maxN; i++)
    {
        for(int j=1; j<vc[i].size(); j++)
            e.pb({vc[i][0], vc[i][j], i});
        while(vc[i].size() > 1) vc[i].pop_back();
    }
    for(int i=1; i<maxN; i++)
    {
        for(int j=i; j<maxN; j+=i)
        {
            for(int v : vc[j])
                L[i].pb(v);
        }
    }
    for(int i=1; i<maxN; i++)
    {
        sort(L[i].begin(), L[i].end(), [](int i, int j)
        {
            return a[i] < a[j];
        });
        for(int j=1; j<L[i].size(); j++)
            e.pb({L[i][0], L[i][j], a[L[i][0]] * a[L[i][j]] / __gcd(a[L[i][0]], a[L[i][j]])});
    }
    memset(lab, -1, sizeof lab);
    sort(e.begin(), e.end(), [](TEdge p, TEdge q)
    {
        return p.w < q.w;
    });
    int res = 0;
    for(TEdge a : e)
    {
        //cout << a.u << " " << a.v << " " << a.w << '\n';
        if(Unite(a.u, a.v)) res += a.w;
    }
    cout << res;
}
int32_t main()
{
    //freopen("sol.inp", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}
0