結果

問題 No.2039 Copy and Avoid
ユーザー CyanmondCyanmond
提出日時 2022-08-12 22:27:31
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 105 ms / 2,000 ms
コード長 1,387 bytes
コンパイル時間 2,627 ms
コンパイル使用メモリ 216,536 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-24 10:42:32
合計ジャッジ時間 4,798 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 102 ms
4,372 KB
testcase_01 AC 2 ms
4,372 KB
testcase_02 AC 4 ms
4,372 KB
testcase_03 AC 2 ms
4,372 KB
testcase_04 AC 2 ms
4,372 KB
testcase_05 AC 5 ms
4,372 KB
testcase_06 AC 2 ms
4,372 KB
testcase_07 AC 2 ms
4,372 KB
testcase_08 AC 2 ms
4,372 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,372 KB
testcase_11 AC 2 ms
4,372 KB
testcase_12 AC 2 ms
4,372 KB
testcase_13 AC 104 ms
4,372 KB
testcase_14 AC 105 ms
4,372 KB
testcase_15 AC 101 ms
4,372 KB
testcase_16 AC 90 ms
4,372 KB
testcase_17 AC 4 ms
4,372 KB
testcase_18 AC 5 ms
4,372 KB
testcase_19 AC 4 ms
4,372 KB
testcase_20 AC 8 ms
4,372 KB
testcase_21 AC 2 ms
4,372 KB
testcase_22 AC 2 ms
4,372 KB
testcase_23 AC 2 ms
4,372 KB
testcase_24 AC 2 ms
4,372 KB
testcase_25 AC 2 ms
4,372 KB
testcase_26 AC 2 ms
4,372 KB
testcase_27 AC 4 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 1 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using i64 = long long;
constexpr i64 inf = 1ll << 50;

int main() {
    i64 N, A, B; int M; std::cin >> N >> M >> A >> B;
    std::vector<i64> C(M); for (auto &e : C) std::cin >> e;
    std::sort(C.begin(), C.end());

    std::map<i64, i64> dp;
    std::map<i64, i64> mi_d;

    auto calc_mi_d = [&](const i64 x) {
        if (mi_d.find(x) != mi_d.end()) return;
        for (const auto e : C) {
            if (e % x == 0) {
                mi_d[x] = e;
                return;
            }
        }
        mi_d[x] = inf;
        return;
    };

    auto solve = [&](auto &&self, const i64 x) -> i64 {
        calc_mi_d(x);
        if (std::binary_search(C.begin(), C.end(), x)) return inf;
        if (x == 1) return 0;
        if (dp.find(x) != dp.end()) return dp[x];

        std::vector<i64> dvs;
        for (i64 i = 1; i * i <= x; ++i) {
            if (x % i == 0) {
                dvs.push_back(i);
                dvs.push_back(x / i);
            }
        }

        i64 result = inf;
        for (const auto e : dvs) {
            if (e == x) continue;
            i64 val = self(self, e) + A * (x / e - 1);
            if (mi_d[e] <= x) continue;
            result = std::min(result, val);
        }
        return dp[x] = result + B;
    };

    const auto ans = solve(solve, N);
    std::cout << (ans >= inf ? -1 : ans - B) << std::endl;
}
0