結果

問題 No.2039 Copy and Avoid
コンテスト
ユーザー 00 Sakuda
提出日時 2026-02-05 00:00:06
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,046 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,507 ms
コンパイル使用メモリ 200,580 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-05 00:00:14
合計ジャッジ時間 6,003 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <iomanip>
#include <numeric>
#include <queue>
#include <stack>
using namespace std;
using ll = long long;
void chmin(ll& a, ll b) {
	a = min(a, b);
}
int main() {
	ll N;int M;ll A, B;
	cin >> N >> M >> A >> B;
	vector<ll> C(M);
	for (int i = 0;i < M;i++) cin >> C[i];
	vector<ll> D;
	for (ll x = 1;x*x <= N;x++) {
		if (N%x != 0) continue;
		D.push_back(x);
		D.push_back(N/x);
	}
	sort(D.begin(),D.end());
	D.erase(unique(D.begin(),D.end()), D.end());
	int L = D.size();
	ll inf = (1LL << 60);
	vector<ll> dp(L, inf);
	dp[0] = 0;
	for (int i = 0;i < L;i++) {
		if (dp[i] == inf) continue;
		ll now = D[i];
		ll mn = inf;
		for (auto t : C) {
			if (t%now == 0) mn = min(mn, t);
		}
		for (int j = i+1;j < L;j++) {
			ll nx = D[j];
			if (nx%now != 0) continue;
			if (mn <= nx) continue;
			ll c = nx / now;
			ll cst = (A * (c - 1)) + (i == 0 ? 0 : B);
			chmin(dp[j], dp[i] + cst);
		}
	}
	cout << (dp.back() == inf ? -1 : dp.back()) << endl;
}
0