結果
問題 | No.2039 Copy and Avoid |
ユーザー |
![]() |
提出日時 | 2023-07-17 03:00:47 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 37 ms / 2,000 ms |
コード長 | 1,185 bytes |
コンパイル時間 | 5,446 ms |
コンパイル使用メモリ | 317,848 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-17 21:20:41 |
合計ジャッジ時間 | 5,676 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge6 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 31 |
ソースコード
#include<bits/stdc++.h> using namespace std; #include <atcoder/all> using namespace atcoder; using mint=modint998244353; //1000000007; using ll=long long; using pp=pair<ll,int>; #define sr string #define vc vector #define db double #define fi first #define se second #define rep(i,n) for(int i=0;i<(int)n;i++) #define pb push_back #define all(v) v.begin(),v.end() #define pque priority_queue #define bpc(a) __builtin_popcount(a) int main(){ int n,m,a,b;cin>>n>>m>>a>>b; vc<int>ban(m); rep(i,m)cin>>ban[i]; vc<int>d; for(int i=1;i*i<=n;i++)if(n%i==0){ d.pb(i); if(i!=n/i)d.pb(n/i); } int k=d.size(); sort(all(d)); vc v(k,vc<int>(0)); rep(i,k){ int b=n+1; rep(j,m)if(ban[j]%d[i]==0)b=min(b,ban[j]); for(int j=i+1;j<k;j++)if(d[j]%d[i]==0){ if(d[j]>=b)break; v[i].pb(j); } } pque<pp,vc<pp>,greater<pp>>q; vc<ll>dist(k,1e18); dist[0]=0; q.push({0,0}); while(q.size()){ auto [di,x]=q.top(); q.pop(); if(dist[x]<di)continue; for(auto au:v[x]){ ll cost=b+(ll)a*(d[au]/d[x]-1); if(x==0)cost-=b; if(dist[au]<=dist[x]+cost)continue; dist[au]=dist[x]+cost; q.push({dist[au],au}); } } if(dist[k-1]==1e18)cout<<-1; else cout<<dist[k-1]; }