結果

問題 No.1861 Required Number
ユーザー 👑 hos.lyrichos.lyric
提出日時 2022-03-04 22:54:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 503 ms / 2,500 ms
コード長 1,941 bytes
コンパイル時間 1,096 ms
コンパイル使用メモリ 100,812 KB
実行使用メモリ 9,120 KB
最終ジャッジ日時 2023-09-26 03:47:46
合計ジャッジ時間 26,111 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
8,764 KB
testcase_01 AC 2 ms
9,120 KB
testcase_02 AC 2 ms
8,760 KB
testcase_03 AC 227 ms
4,380 KB
testcase_04 AC 226 ms
4,412 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 7 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,384 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 4 ms
4,384 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,380 KB
testcase_19 AC 4 ms
4,376 KB
testcase_20 AC 4 ms
4,376 KB
testcase_21 AC 4 ms
4,380 KB
testcase_22 AC 4 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 207 ms
4,380 KB
testcase_25 AC 465 ms
4,380 KB
testcase_26 AC 257 ms
4,376 KB
testcase_27 AC 421 ms
4,380 KB
testcase_28 AC 335 ms
4,380 KB
testcase_29 AC 448 ms
4,380 KB
testcase_30 AC 467 ms
4,380 KB
testcase_31 AC 453 ms
4,380 KB
testcase_32 AC 448 ms
4,380 KB
testcase_33 AC 456 ms
4,380 KB
testcase_34 AC 448 ms
4,380 KB
testcase_35 AC 495 ms
4,380 KB
testcase_36 AC 456 ms
4,380 KB
testcase_37 AC 493 ms
4,380 KB
testcase_38 AC 448 ms
4,384 KB
testcase_39 AC 445 ms
4,380 KB
testcase_40 AC 501 ms
4,380 KB
testcase_41 AC 466 ms
4,384 KB
testcase_42 AC 503 ms
4,380 KB
testcase_43 AC 478 ms
4,384 KB
testcase_44 AC 2 ms
4,384 KB
04_evil_1.txt TLE -
04_evil_2.txt TLE -
04_evil_3.txt TLE -
04_evil_4.txt TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


constexpr int MAX_K = 100'010;
using BS = bitset<MAX_K>;

int N, K;
vector<int> A;

int ansAny;
vector<int> ans;

void solve(int l, int r, const BS &bs) {
  if (l + 1 == r) {
    if (bs[K] || bs[K - A[l]]) {
      ansAny = 1;
    }
    if (!bs[K] && bs[K - A[l]]) {
      ans[l] = 1;
    }
  } else {
    const int mid = (l + r) / 2;
    {
      BS bbs = bs;
      for (int i = mid; i < r; ++i) {
        bbs |= bbs << A[i];
      }
      solve(l, mid, bbs);
    }
    {
      BS bbs = bs;
      for (int i = l; i < mid; ++i) {
        bbs |= bbs << A[i];
      }
      solve(mid, r, bbs);
    }
  }
}

int main() {
  for (; ~scanf("%d%d", &N, &K); ) {
    A.resize(N);
    for (int i = 0; i < N; ++i) {
      scanf("%d", &A[i]);
    }
    
    ansAny = 0;
    ans.assign(N, 0);
    BS bs;
    bs[0] = 1;
    solve(0, N, bs);
// cerr<<"ans = ";pv(ans.begin(),ans.end());
    
    int ansCnt = 0;
    for (int i = 0; i < N; ++i) if (ans[i]) {
      ++ansCnt;
    }
    printf("%d\n", ansAny ? ansCnt : -1);
  }
  return 0;
}
0