結果

問題 No.1917 LCMST
ユーザー Trí Tâm NguyễnTrí Tâm Nguyễn
提出日時 2023-02-11 18:11:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
MLE  
実行時間 -
コード長 1,810 bytes
コンパイル時間 2,683 ms
コンパイル使用メモリ 210,044 KB
実行使用メモリ 814,368 KB
最終ジャッジ日時 2023-09-22 14:16:13
合計ジャッジ時間 111,785 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 98 ms
58,888 KB
testcase_01 AC 97 ms
58,692 KB
testcase_02 AC 96 ms
58,676 KB
testcase_03 AC 100 ms
59,136 KB
testcase_04 AC 100 ms
59,096 KB
testcase_05 AC 99 ms
58,980 KB
testcase_06 AC 97 ms
58,952 KB
testcase_07 AC 99 ms
59,060 KB
testcase_08 AC 95 ms
58,644 KB
testcase_09 MLE -
testcase_10 MLE -
testcase_11 MLE -
testcase_12 MLE -
testcase_13 MLE -
testcase_14 MLE -
testcase_15 MLE -
testcase_16 MLE -
testcase_17 MLE -
testcase_18 MLE -
testcase_19 MLE -
testcase_20 MLE -
testcase_21 MLE -
testcase_22 MLE -
testcase_23 MLE -
testcase_24 MLE -
testcase_25 MLE -
testcase_26 MLE -
testcase_27 MLE -
testcase_28 MLE -
testcase_29 MLE -
testcase_30 MLE -
testcase_31 MLE -
testcase_32 MLE -
testcase_33 MLE -
testcase_34 MLE -
testcase_35 AC 634 ms
204,928 KB
testcase_36 MLE -
testcase_37 MLE -
testcase_38 MLE -
testcase_39 MLE -
testcase_40 MLE -
testcase_41 MLE -
testcase_42 MLE -
testcase_43 MLE -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

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=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