結果

問題 No.2039 Copy and Avoid
ユーザー sapphire__15sapphire__15
提出日時 2022-08-12 22:40:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 32 ms / 2,000 ms
コード長 2,317 bytes
コンパイル時間 9,974 ms
コンパイル使用メモリ 209,976 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-24 10:58:40
合計ジャッジ時間 4,217 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
4,348 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 4 ms
4,372 KB
testcase_06 AC 4 ms
4,372 KB
testcase_07 AC 1 ms
4,372 KB
testcase_08 AC 4 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 4 ms
4,372 KB
testcase_13 AC 29 ms
4,372 KB
testcase_14 AC 30 ms
4,372 KB
testcase_15 AC 32 ms
4,372 KB
testcase_16 AC 28 ms
4,372 KB
testcase_17 AC 5 ms
4,372 KB
testcase_18 AC 6 ms
4,372 KB
testcase_19 AC 4 ms
4,372 KB
testcase_20 AC 6 ms
4,372 KB
testcase_21 AC 4 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 1 ms
4,372 KB
testcase_27 AC 2 ms
4,372 KB
testcase_28 AC 2 ms
4,372 KB
testcase_29 AC 2 ms
4,372 KB
testcase_30 AC 2 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i,n) for(int i = 0; i < int(n); i++)
#define per(i,n) for(int i = (n) - 1; 0 <= i; i--)
#define rep2(i,l,r) for(int i = (l); i < int(r); i++)
#define per2(i,l,r) for(int i = (r) - 1; int(l) <= i; i--)
#define MM << " " <<
#define pb push_back
#define eb emplace_back
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
#define sz(x) (int)x.size()

template <typename T>
void print(const vector<T> &v, T x = 0) {
    int n = v.size();
    for (int i = 0; i < n; i++) cout << v[i] + x << (i == n - 1 ? '\n' : ' ');
    if (v.empty()) cout << '\n';
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

template <typename T>
bool chmax(T &x, const T &y) {
    return (x < y) ? (x = y, true) : false;
}

template <typename T>
bool chmin(T &x, const T &y) {
    return (x > y) ? (x = y, true) : false;
}

// const ll MOD = 1'000'000'007;
const ll MOD = 998'244'353;

///////////////////////////////////////////////////////////////

template <class T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
template <class T>
using maxheap = priority_queue<T>;

template <typename T>
int lb(const vector<T> &v, T x) {
    return lower_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
int ub(const vector<T> &v, T x) {
    return upper_bound(begin(v), end(v), x) - begin(v);
}

template <typename T>
void rearrange(vector<T> &v) {
    sort(begin(v), end(v));
    v.erase(unique(begin(v), end(v)), end(v));
}

using namespace std;

int main() {
    ll n, m, a, b; cin >> n >> m >> a >> b;
    vector<int> da(m);
    rep(i,m) cin >> da[i];
    vector<int> div;
    for(int i = 1; i*i <= n; i++) {
        if(n % i == 0) {
            div.emplace_back(i);
            if(i * i != n) div.emplace_back(n / i);
        }
    }
    sort(all(div));
    vector<ll> dp(div.size(), LLONG_MAX / 2);
    dp[0] = 0;
    rep(i,dp.size()) {
        int k = n + 1;
        for(auto &j : da) {
            if(j % div[i] == 0) chmin(k, j);
        }
        rep2(j,i+1,dp.size()) {
            if(k <= div[j]) break;
            if(div[j] % div[i] == 0) {
                chmin(dp[j], dp[i] + b + div[j] / div[i] * a - a);
            }
        }
    }
    cout << (dp.back() == LLONG_MAX / 2 ? -1 : dp.back() - b) << endl;
}
0