# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
524256 | macktvz | Bank (IZhO14_bank) | C++17 | 886 ms | 262148 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
// can we split B into disjoint subsets s.t. there is at least one subset per Ai
// for multiple A, need to find subsets that satisfy each A and & to 0
// dp[MASK] = TRUE if
int main() {
int n,m; cin >> n >> m;
vector<int> A(n);
vector<int> B(m);
for(auto &a : A) cin >> a;
for(auto &b : B) cin >> b;
vector<vector<int>> S(n); // with some sum there are only sqrt(sum) different possible values
for(int mask = 0; mask < (1<<m); mask++) {
int s = 0;
for(int i = 0; i < m; i++) {
if (mask&(1<<i)) s += B[i];
}
for(int i = 0; i < n; i++) {
if (s == A[i]) S[i].push_back(mask);
}
}
vector<vector<int>> dp((1 << n));
for(int i = 0; i < n; i++) {
dp[1<<i]=S[i];
}
for(int mask = 0; mask < (1 << n); mask++) {
if (__builtin_popcount(mask) <= 1) continue;
int ch;
for(int i = 0; i < n; i++) { // find first bit set
if (mask&(1<<i)) {
ch = i;
break;
}
}
for(int j : dp[mask^(1<<ch)]) {
for(int i : S[ch]) {
if ((j&i) == 0) {
dp[mask].push_back(j|i);
}
}
}
}
int sz = (int)dp[(1<<n)-1].size();
cout << (sz == 0 ? "NO" : "YES") << '\n';
}
Compilation message (stderr)
# | 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... |