結果

問題 No.2039 Copy and Avoid
ユーザー merom686
提出日時 2022-08-12 23:18:07
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,187 bytes
コンパイル時間 2,407 ms
コンパイル使用メモリ 212,400 KB
最終ジャッジ日時 2025-01-30 21:46:25
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

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;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0