結果

問題 No.2039 Copy and Avoid
ユーザー merom686merom686
提出日時 2022-08-12 23:18:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 2,187 bytes
コンパイル時間 2,896 ms
コンパイル使用メモリ 219,960 KB
実行使用メモリ 7,232 KB
最終ジャッジ日時 2023-10-24 11:38:23
合計ジャッジ時間 3,562 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

struct Graph {
    static constexpr ll INF = 1LL << 60;
    struct VertexQ {
        bool operator<(const VertexQ &o) const {
            return c > o.c; // 逆
        }
        int i;
        ll c;
    };
    struct Vertex { int n; ll c; };
    struct Edge { int i, n; ll c; };
    Graph(int n) : v(n, { -1, INF }), n(n), m(0) {}
    void add_edge(int a, int b, ll c) {
        e.push_back({ b, v[a].n, c });
        v[a].n = m;
        m++;
    }
    void dijkstra(int i, int j) {
        for (int i = 0; i < n; i++) v[i].c = INF;
        priority_queue<VertexQ> q;
        q.push({ i, v[i].c = 0 });
        while (!q.empty()) {
            auto p = q.top(); q.pop();
            if (p.i == j) break;
            if (p.c > v[p.i].c) continue;
            for (int j = v[p.i].n; j >= 0; j = e[j].n) {
                Edge &o = e[j];
                ll c = p.c + o.c;
                if (c < v[o.i].c) q.push({ o.i, v[o.i].c = c });
            }
        }
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

int main() {
    int n, m, a, b;
    cin >> n >> m >> a >> b;

    unordered_set<int> st;
    vector<int> e;
    for (int i = 0; i < m; i++) {
        int c;
        cin >> c;
        st.insert(c);
        e.push_back(c);
    }
    sort(e.begin(), e.end());

    vector<int> d;
    for (int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            if (st.count(i) == 0) d.push_back(i);
            if (n / i != i && st.count(n / i) == 0) d.push_back(n / i);
        }
    }
    sort(d.begin(), d.end());

    int l = d.size();
    Graph g(l);
    for (int i = 0; i < l; i++) {
        int u = 0;
        for (const auto &c : e) {
            if (c % d[i] == 0) {
                u = c;
                break;
            }
        }
        for (int j = i + 1; j < l; j++) {
            if (d[j] % d[i] == 0) {
                if (u > 0 && u < d[j]) continue;
                g.add_edge(i, j, (d[j] / d[i] - 1) * (ll)a + b);
            }
        }
    }
    g.dijkstra(0, l - 1);
    ll r = g.v[l - 1].c;
    if (r == g.INF) r = -1; else r -= b;
    cout << r << endl;

    return 0;
}
0