結果

問題 No.1917 LCMST
ユーザー Trí Tâm NguyễnTrí Tâm Nguyễn
提出日時 2024-03-12 20:44:44
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 818 ms / 4,000 ms
コード長 1,862 bytes
コンパイル時間 2,251 ms
コンパイル使用メモリ 179,804 KB
実行使用メモリ 139,356 KB
最終ジャッジ日時 2024-03-12 20:45:16
合計ジャッジ時間 31,633 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 128 ms
59,516 KB
testcase_01 AC 107 ms
59,516 KB
testcase_02 AC 107 ms
59,516 KB
testcase_03 AC 128 ms
59,920 KB
testcase_04 AC 122 ms
59,908 KB
testcase_05 AC 117 ms
59,940 KB
testcase_06 AC 120 ms
59,916 KB
testcase_07 AC 124 ms
59,928 KB
testcase_08 AC 114 ms
59,516 KB
testcase_09 AC 780 ms
139,080 KB
testcase_10 AC 753 ms
138,928 KB
testcase_11 AC 805 ms
136,840 KB
testcase_12 AC 688 ms
133,352 KB
testcase_13 AC 541 ms
126,012 KB
testcase_14 AC 729 ms
135,996 KB
testcase_15 AC 407 ms
122,520 KB
testcase_16 AC 528 ms
126,176 KB
testcase_17 AC 579 ms
128,972 KB
testcase_18 AC 638 ms
130,156 KB
testcase_19 AC 446 ms
122,332 KB
testcase_20 AC 388 ms
120,944 KB
testcase_21 AC 461 ms
122,112 KB
testcase_22 AC 379 ms
120,988 KB
testcase_23 AC 376 ms
120,772 KB
testcase_24 AC 475 ms
122,088 KB
testcase_25 AC 387 ms
121,476 KB
testcase_26 AC 444 ms
123,668 KB
testcase_27 AC 422 ms
123,840 KB
testcase_28 AC 418 ms
123,160 KB
testcase_29 AC 788 ms
139,356 KB
testcase_30 AC 782 ms
139,324 KB
testcase_31 AC 780 ms
139,256 KB
testcase_32 AC 760 ms
139,300 KB
testcase_33 AC 818 ms
139,296 KB
testcase_34 AC 262 ms
103,328 KB
testcase_35 AC 273 ms
103,328 KB
testcase_36 AC 261 ms
103,328 KB
testcase_37 AC 784 ms
139,300 KB
testcase_38 AC 783 ms
139,332 KB
testcase_39 AC 776 ms
139,260 KB
testcase_40 AC 794 ms
139,256 KB
testcase_41 AC 806 ms
139,352 KB
testcase_42 AC 777 ms
139,192 KB
testcase_43 AC 257 ms
103,488 KB
testcase_44 AC 269 ms
103,496 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]] / i});
    }
    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)
    {
        if(Unite(a.u, a.v)) res += a.w;
    }
    cout << res;
}
int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    ReadInput();
    Solve();
}
0