結果

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

ソースコード

diff #


#include <iostream>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <random>
#include <stdio.h>
#include <fstream>
#include <functional>
#include <cassert>
#include <unordered_map>
#include <bitset>
#include <atcoder/all>

using namespace std;
using namespace atcoder;



#define rep(i,n) for (int i=0;i<n;i+=1)
#define rrep(i,n) for (int i=n-1;i>-1;i--)
#define pb push_back
#define all(x) (x).begin(), (x).end()

template<class T>
using vec = vector<T>;
template<class T>
using vvec = vec<vec<T>>;
template<class T>
using vvvec = vec<vvec<T>>;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;




template<class T>
bool chmin(T &a, T b){
  if (a>b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
bool chmax(T &a, T b){
  if (a<b){
    a = b;
    return true;
  }
  return false;
}

template<class T>
T sum(vec<T> x){
  T res=0;
  for (auto e:x){
    res += e;
  }
  return res;
}

template<class T>
ostream& operator<<(ostream& os, const vec<T>& A){
  os << "[";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"]";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const set<T>& A){
  os << "{";
  for (auto e:A){
    os << e;
    auto it = A.find(e);
    it++;
    if (it!=A.end()){
      os << " ";
    }
  }
  os << "}";
  return os;
}

template<class T,class U>
ostream& operator<<(ostream& os, const pair<T,U>& A){
  os << "(" << A.first <<", " << A.second << ")";
  return os;
}

template<class T>
ostream& operator<<(ostream& os, const deque<T>& A){
  os << "deque({";
  rep(i,A.size()){
    os << A[i];
    if (i!=A.size()-1){
      os << ' ';
    }
  }
  os<<"})";
  return os;
}

using mint = modint998244353;

ostream& operator<<(ostream& os, const mint& a){
  os << a.val();
  return os;
}

int main(){
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  ll N,M,A,B;
  cin>>N>>M>>A>>B;

  vec<ll> C(M);
  rep(i,M){cin>>C[i];}

  map<ll,ll> D;
  vec<ll> d;
  for (ll i=1;i*i<=N;i++){
    if ((N%i)==0){
      D[i] = 1e18;
      D[N/i] = 1e18;
      d.pb(i);
      d.pb(N/i);
    }
  }

  sort(all(d));
  D[1] = 0;
  for (auto a:d){
    if (D[a]==1e18){continue;}
    //cout << D[a] << endl;
    ll next_max = 1e18/a;
    for (auto c:C){
      if ((c%a)==0){
        chmin(next_max,c/a-1);
      }
    }
    //cout << a << " " << next_max << endl;
    for (auto na:d){
      if (a < na && (na%a)==0 && na <= next_max * a){
        chmin(D[na],D[a]+(na/a-1)*A+B);
      }
    }
  }

  if (D[N]==1e18){
    cout << -1 << endl;
  }
  else{
    cout << D[N]-B << endl;
  }
  



  




 
}
0