이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)(a).size()
#define el '\n'
#define F first
#define S second
#define For(i, a, b) for (int i = (a); i <= (int)(b); i++)
#define Rof(i, a, b) for (int i = (a); i >= (int)(b); i--)
#define Fore(it, x) for (auto it = (x).begin(); it != (x).end(); ++it)
using vb = vector<bool>;
using vvb = vector<vb>;
using vc = vector<char>;
using vvc = vector<vc>;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vii = vector<pii>;
//*** START CODING ***//
int setBit(int n, int pos) { return n |= (1 << pos); }
int clearBit(int n, int pos) { return n &= ~(1 << pos); }
int getBit(int n, int pos) { return (n >> pos) & 1; }
int toggleBit(int n, int pos) { return n ^= (1 << pos); }
const long long oo = 2e18, mod = 1e9 + 7;
const int ms = 20 + 5;
int n, m;
int a[ms], b[ms];
vvi memo;
bool f(int mask, int idx) {
if (idx == n) return true;
if (memo[mask][idx] != -1) return memo[mask][idx];
bool ans = false;
for (int mk = 0; mk < (1LL << m); mk++) {
int sum = 0;
for (int i = 0; i < m; i++) {
if (!getBit(mk, i)) continue;
if (getBit(mask, i)) break;
sum += b[i];
}
if (sum == a[idx]) {
int newMask = mk | mask;
ans |= f(newMask, idx + 1);
}
}
return memo[mask][idx] = ans;
};
void solve() {
cin >> n >> m;
For(i, 0, n - 1) cin >> a[i];
For(i, 0, m - 1) cin >> b[i];
memo = vvi(1 << m, vi(n, -1));
bool ans = f(0, 0);
if (ans)
cout << "YES" << el;
else
cout << "NO" << el;
// cout << f(0, 0) << el;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
bank.cpp: In function 'bool f(long long int, long long int)':
bank.cpp:54:28: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
54 | return memo[mask][idx] = ans;
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |