結果

問題 No.247 線形計画問題もどき
ユーザー motimoti
提出日時 2015-07-21 20:54:55
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,182 bytes
コンパイル時間 833 ms
コンパイル使用メモリ 94,572 KB
実行使用メモリ 11,524 KB
最終ジャッジ日時 2023-09-22 20:59:19
合計ジャッジ時間 4,163 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1 ms
4,380 KB
testcase_02 TLE -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
権限があれば一括ダウンロードができます

ソースコード

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) ^ ullhash(xsum) ^ ullhash(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] + 1; 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); std::reverse(A, A+N);
  auto ret = solve(C, N-1, 0);
  if(ret == invalid) {
    cout << -1 << endl;
  }
  else {
    cout << ret << endl;
  }

  return 0;
}
0