結果
| 問題 | No.2039 Copy and Avoid |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2022-08-19 18:53:20 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,190 bytes |
| 記録 | |
| コンパイル時間 | 2,314 ms |
| コンパイル使用メモリ | 210,176 KB |
| 最終ジャッジ日時 | 2025-01-31 00:31:43 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 24 WA * 4 TLE * 3 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define FOR(i, n, m) for (int i = n; i < m; i++)
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
void print() { std::cout << std::endl; }
template<class T, class... A> void print(const T& first, const A&... rest) { std::cout << first << " "; print(rest...); }
template<class... A> void print(const A&... rest) { print(rest...); }
# define SZ(x) ((int)(x).size())
const ll INF = 1LL<<60;
int main() {
int n, m;
ll a, b;
cin >> n >> m >> a >> b;
set<int> c;
rep(i,m) {
int x;
cin >> x;
c.insert(x);
}
vector<int> div;
for (int x = 1; x * x <= n; x++) {
if (x * x == n)div.emplace_back(x);
else if (n % x == 0){
div.emplace_back(x);
div.emplace_back(n/x);
}
}
sort(div.begin(), div.end());
reverse(div.begin(), div.end());
//cout << "ok" << endl;
auto check = [&](int x, int y) -> bool {
x -= y;
if (x / y < m) {
while (x >= y) {
if (c.find(x) != c.end())return false;
x -= y;
}
return true;
}
else {
for(auto z:c) {
if (z % y == 0 && z < x)return false;
}
return true;
}
};
map<int, ll> memo;
auto saiki = [&](auto&& saiki, int x) -> ll{
if (x == 1)return 0;
if (memo.find(x)!=memo.end())return memo[x];
ll ans = INF;
for(int y : div) {
//print("x,y", x, y);
if (y < x && x % y == 0 && check(x, y)) {
ll d = x / y;
d--;
chmin(ans, saiki(saiki, y) + b + d * a);
}
}
//cout << x << " " << ans << endl;
memo[x] = ans;
return ans;
};
if (n == 1)return 0;
else {
auto z = saiki(saiki, n);
if (z == INF)cout << -1 << endl;
else cout << z - b << endl;
}
}