結果

問題 No.1701 half price
ユーザー tnakao0123tnakao0123
提出日時 2021-10-09 01:43:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 4 ms / 3,000 ms
コード長 932 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 32,100 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-30 17:12:02
合計ジャッジ時間 1,085 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1701.cc:  No.1701 half price - yukicoder
 */

#include<cstdio>
 
using namespace std;

/* constant */

const int MAX_N = 13;
const int NBITS = 1 << MAX_N;

/* typedef */

typedef long long ll;

/* global variables */

int as[MAX_N];
ll bss[NBITS];

/* subroutines */

/* main */

int main() {
  int n, w;
  scanf("%d%d", &n, &w);
  for (int i = 0; i < n; i++) scanf("%d", as + i);

  int nbits = 1 << n;

  for (int bits = 1, msb = 1, msi = 0; bits < nbits; bits++) {
    if ((msb << 1) <= bits) msb <<= 1, msi++;
    bss[bits] = bss[bits ^ msb] + as[msi];
  }

  int cnt = 0;

  for (int bits = 1; bits < nbits; bits++) {
    bool found = false;
    for (int bits0 = bits;; bits0 = (bits0 - 1) & bits) {
      ll sum = bss[bits0] / 2 + bss[bits ^ bits0];
      if (sum == w) { found = true; break; }
      if (bits0 == 0) break;
    }

    if (found) cnt++;
  }

  printf("%d\n", cnt);
  return 0;
}
0