제출 #173729

#제출 시각아이디문제언어결과실행 시간메모리
173729juggernaut은행 (IZhO14_bank)C++14
100 / 100
253 ms16600 KiB
#include <iostream>
#include <time.h>
#include <vector>

using namespace std;

#define ll long long
#define sz(x) (int)x.size()
#define pii pair < int, int >
#define endl "\n"
#define METH ios::sync_with_stdio(0); cin.tie(0);
#define BEGIN cout << "BEGIN" << endl;
#define END cout << "END" << endl;

const int mod = 1e9 + 7;                              /// ANOTHER HASH MOD: 228228227
const int prime = 29;                                  /// ANOTHER HASH PRIME: 997
const int INF = ((long long) 0xCAFEBABE - 1e9 - 4e8);

int n, m;
vector < short > a, b;
short pre[1 << 20];
bool dp[21][1 << 20];
vector<int>vec[10];
string int_to_bit(int x) {
  string res = "";
  while (x) {
    if (x & 1) {
      res += "1";
    } else {
      res += "0";
    }
    x >>= 1;
  }
  while (res.size() < m) {
    res += "0";
  }
  return res;
}

inline void read() {
  scanf("%d %d", &n, &m);
  a.resize(n + 1);
  b.resize(m + 1);
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &b[i]);
  }
}

void calc(int mask, int cnt) {
  //cout << int_to_bit(mask) << " " << cnt << endl;
  if (cnt == n) {
    cout << "YES" << endl;
    exit(0);
  }
  if (dp[cnt][mask]) {
    return;
  }
  dp[cnt][mask] = 1;
  for(int to:vec[cnt]){
    if((mask&to)==to)calc(mask^to,cnt+1);
  }/*
  for (int sub = mask; sub != 0; sub = mask & (sub - 1)) {
    if (pre[sub] == a[cnt]) {
      calc(mask ^ sub, cnt + 1);
    }
  }*/
}

inline int solve() {
  for (int i = 0; i < (1 << m); i++) {
    int sum = 0;
    for (int j = 0; j < m; j++) {
      if (i >> j & 1) {
        sum += b[j];
      }
    }
    pre[i] = sum;
    for(int cur=0;cur<n;cur++){
        if(sum==a[cur])vec[cur].push_back(i);
    }
  }
  calc((1 << m) - 1, 0);
  cout << "NO" << endl;
}

int main() {
  int t = 1;
  while (t--) {
    read();
    solve();
  }
}

컴파일 시 표준 에러 (stderr) 메시지

bank.cpp: In function 'std::__cxx11::string int_to_bit(int)':
bank.cpp:34:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   while (res.size() < m) {
          ~~~~~~~~~~~^~~
bank.cpp: In function 'void read()':
bank.cpp:45:22: warning: format '%d' expects argument of type 'int*', but argument 2 has type '__gnu_cxx::__alloc_traits<std::allocator<short int> >::value_type* {aka short int*}' [-Wformat=]
     scanf("%d", &a[i]);
                      ^
bank.cpp:48:22: warning: format '%d' expects argument of type 'int*', but argument 2 has type '__gnu_cxx::__alloc_traits<std::allocator<short int> >::value_type* {aka short int*}' [-Wformat=]
     scanf("%d", &b[i]);
                      ^
bank.cpp: In function 'int solve()':
bank.cpp:87:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
bank.cpp: In function 'void read()':
bank.cpp:41:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &n, &m);
   ~~~~~^~~~~~~~~~~~~~~~~
bank.cpp:45:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &a[i]);
     ~~~~~^~~~~~~~~~~~~
bank.cpp:48:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d", &b[i]);
     ~~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...