結果

問題 No.1538 引きこもりさんは引き算が得意。
ユーザー tnakao0123tnakao0123
提出日時 2021-06-08 16:56:36
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 24 ms / 2,000 ms
コード長 1,803 bytes
コンパイル時間 971 ms
コンパイル使用メモリ 97,008 KB
実行使用メモリ 5,456 KB
最終ジャッジ日時 2023-08-20 22:39:06
合計ジャッジ時間 3,324 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 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,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,384 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 1 ms
4,376 KB
testcase_34 AC 1 ms
4,380 KB
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 18 ms
5,284 KB
testcase_38 AC 18 ms
5,280 KB
testcase_39 AC 20 ms
5,216 KB
testcase_40 AC 18 ms
5,344 KB
testcase_41 AC 18 ms
5,216 KB
testcase_42 AC 18 ms
5,280 KB
testcase_43 AC 1 ms
4,376 KB
testcase_44 AC 18 ms
5,276 KB
testcase_45 AC 18 ms
5,456 KB
testcase_46 AC 16 ms
5,364 KB
testcase_47 AC 8 ms
4,380 KB
testcase_48 AC 22 ms
5,284 KB
testcase_49 AC 18 ms
5,272 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 23 ms
5,400 KB
testcase_52 AC 22 ms
5,264 KB
testcase_53 AC 23 ms
5,292 KB
testcase_54 AC 23 ms
5,400 KB
testcase_55 AC 23 ms
5,272 KB
testcase_56 AC 24 ms
5,284 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 1538.cc:  No.1538 引きこもりさんは引き算が得意。 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 20;
const int MAX_M = 59049; // = 3^10

/* typedef */

typedef long long ll;
typedef pair<ll,int> pli;

/* global variables */

int as[MAX_N];
pli ass[MAX_M], bss[MAX_M];

/* subroutines */

inline int exp3(int n) {
  int e = 1;
  while (n--) e *= 3;
  return e;
}

inline pli t2sum(int t, int l, int r) {
  ll sum = 0;
  int b = 0;
  for (int i = l; i < r; i++, t /= 3) {
    int d = t % 3;
    if (d == 1) sum += as[i], b |= 1;
    else if (d == 2) sum -= as[i], b |= 2;
  }
  return pli(sum, b);
}

/* main */

int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < n; i++) {
    scanf("%d", as + i);
    if (as[i] == k) { puts("Yes"); return 0; }
  }

  int a = n / 2, b = n - a;
  int am = exp3(a), bm = exp3(b);

  for (int ts = 0; ts < am; ts++) ass[ts] = t2sum(ts, 0, a);
  sort(ass, ass + am);
  am = unique(ass, ass + am) - ass;

  for (int ts = 0; ts < bm; ts++) bss[ts] = t2sum(ts, a, n);
  sort(bss, bss + bm);
  bm = unique(bss, bss + bm) - bss;

  for (int ts = 0; ts < am; ts++) {
    pli pa = ass[ts];
    //printf("%d -> %lld\n", ts, sum);

    int i = lower_bound(bss, bss + bm, pli(k - pa.first, 0)) - bss;
    while (i < bm && bss[i].first + pa.first == k) {
      if ((bss[i].second | pa.second) == 3) {
	puts("Yes"); return 0;
      }
      i++;
    }
  }

  puts("No");
  return 0;
}
0