結果
問題 | No.2039 Copy and Avoid |
ユーザー |
![]() |
提出日時 | 2022-08-13 20:05:51 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 111 ms / 2,000 ms |
コード長 | 1,381 bytes |
コンパイル時間 | 1,143 ms |
コンパイル使用メモリ | 98,808 KB |
最終ジャッジ日時 | 2025-01-30 22:19:34 |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
#include <iostream>#include <map>#include <set>#include <algorithm>#include <vector>using namespace std;typedef long long ll;ll c[5010];int main(){ll i,n,m,a,b; cin >> n >> m >> a >> b;for(i=0;i<m;i++) cin >> c[i];sort(c,c + m);map<ll,ll> mp;set<ll> s;mp[1] = 0; s.insert(1);while(s.size()){ll x = *s.begin(); s.erase(x);ll nn = n/x;ll val = mp[x];vector<ll> d1,d2;for(i=1;i*i<=nn;i++){if(nn%i==0){if(i!=1) d1.push_back(i);if(i*i!=nn) d2.push_back(nn/i);}}reverse(d2.begin(),d2.end());d1.insert(d1.end(),d2.begin(),d2.end());int j = 0;for(i=0;i<d1.size();i++){int now = d1[i]*x;bool f = false;while(j<m && c[j]<=now){if(c[j]>=x && c[j]%x==0) f = true;j++;}if(f) break;s.insert(now);//if(now==6) cout << d1[i] << " vvv " << val << " " << x << endl;if(mp.find(now)==mp.end()) mp[now] = val + (ll)(d1[i] - 1)*a + b;else mp[now] = min(mp[now],val + (ll)(d1[i] - 1)*a + b);}}if(mp.find(n)==mp.end()) cout << -1 << "\n";else cout << mp[n] - b << "\n";//for(auto x:mp) cout << x.first << " " << x.second << "\n";}