結果

問題 No.1917 LCMST
ユーザー 👑 colognecologne
提出日時 2022-05-01 13:16:19
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2,022 ms / 4,000 ms
コード長 1,027 bytes
コンパイル時間 3,055 ms
コンパイル使用メモリ 262,564 KB
実行使用メモリ 25,352 KB
最終ジャッジ日時 2023-09-13 05:25:40
合計ジャッジ時間 49,205 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 14 ms
4,380 KB
testcase_04 AC 14 ms
4,376 KB
testcase_05 AC 14 ms
4,376 KB
testcase_06 AC 13 ms
4,380 KB
testcase_07 AC 15 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1,729 ms
16,208 KB
testcase_10 AC 1,948 ms
15,688 KB
testcase_11 AC 2,022 ms
16,076 KB
testcase_12 AC 1,746 ms
16,000 KB
testcase_13 AC 828 ms
12,796 KB
testcase_14 AC 1,904 ms
16,808 KB
testcase_15 AC 571 ms
9,112 KB
testcase_16 AC 909 ms
11,372 KB
testcase_17 AC 1,214 ms
15,536 KB
testcase_18 AC 1,422 ms
15,776 KB
testcase_19 AC 517 ms
9,112 KB
testcase_20 AC 415 ms
8,000 KB
testcase_21 AC 566 ms
9,048 KB
testcase_22 AC 412 ms
8,008 KB
testcase_23 AC 420 ms
8,012 KB
testcase_24 AC 556 ms
9,012 KB
testcase_25 AC 421 ms
8,004 KB
testcase_26 AC 463 ms
8,220 KB
testcase_27 AC 492 ms
9,008 KB
testcase_28 AC 508 ms
9,320 KB
testcase_29 AC 1,526 ms
23,804 KB
testcase_30 AC 1,564 ms
23,732 KB
testcase_31 AC 1,543 ms
24,752 KB
testcase_32 AC 1,553 ms
25,352 KB
testcase_33 AC 1,520 ms
23,472 KB
testcase_34 AC 262 ms
7,148 KB
testcase_35 AC 263 ms
7,212 KB
testcase_36 AC 264 ms
7,128 KB
testcase_37 AC 1,534 ms
23,980 KB
testcase_38 AC 1,560 ms
24,320 KB
testcase_39 AC 1,532 ms
23,656 KB
testcase_40 AC 1,624 ms
25,036 KB
testcase_41 AC 1,597 ms
23,804 KB
testcase_42 AC 1,550 ms
23,960 KB
testcase_43 AC 274 ms
7,536 KB
testcase_44 AC 276 ms
7,436 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/dsu>
using namespace std;

int main()
{
    int N;
    cin >> N;
    vector<int> A(N);
    for (int &a : A)
        cin >> a;

    long ans = 0;
    int M = *max_element(A.begin(), A.end());
    vector<bool> V(M + 1);
    for (int a : A)
    {
        if (V[a])
            ans += a;
        V[a] = 1;
    }

    vector<pair<int, int>> E;
    for (int i = 1; i <= M; i++)
    {
        for (int j = i; j <= M; j += i)
        {
            if (V[j])
            {
                for (int k = j + i; k <= M; k += i)
                    if (V[k])
                        E.emplace_back(j, k);
                break;
            }
        }
    }

    sort(E.begin(), E.end(), [&](auto a, auto b)
         { return lcm((long)a.first, a.second) < lcm((long)b.first, b.second); });

    atcoder::dsu D(M + 1);

    for (auto &[a, b] : E)
    {
        if (!D.same(a, b))
        {
            D.merge(a, b);
            ans += lcm((long)a, b);
        }
    }

    cout << ans << endl;
}
0