結果

問題 No.247 線形計画問題もどき
ユーザー moti
提出日時 2015-07-21 20:11:58
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,138 bytes
コンパイル時間 806 ms
コンパイル使用メモリ 93,988 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-07-08 11:34:25
合計ジャッジ時間 4,022 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 5
other AC * 2 TLE * 1 -- * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <complex>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <unordered_map>

using namespace std;

#define REP(i,a,b) for(int i=a;i<(int)b;i++)
#define rep(i,n) REP(i,0,n)

typedef long long ll;
typedef unsigned long long ull;

int N, C;
int A[101];

int constexpr MOD = 1e9+7;
int constexpr invalid = std::numeric_limits<int>::max() / 6;

std::unordered_map<ull, ll> memo;
std::hash<ull> ullhash;

ll solve(int csum, int i, int xsum) {

  ull hash = ullhash(csum ^ xsum ^ i);
  if(memo.find(hash) != memo.end()) { return memo[hash]; }

  if(i == -1) {
    if(csum == 0) { return xsum; }
    else { return invalid; }
  }

  if(csum < 0) { return invalid; }

  ll& ret = memo[hash];
  ret = invalid;

  for(int x = csum / A[i]; x >= 0; x--) {
    ret = min(ret, solve(csum - A[i] * x, i-1, xsum + x));
  }

  return ret;
}

int main() {

  cin >> C >> N;
  rep(i, N) cin >> A[i];
  sort(A, A+N);
  auto ret = solve(C, N-1, 0);
  if(ret == invalid) {
    cout << -1 << endl;
  }
  else {
    cout << ret << endl;
  }

  return 0;
}
0