Submission #531701

#TimeUsernameProblemLanguageResultExecution timeMemory
531701erkeBank (IZhO14_bank)C++11
71 / 100
1092 ms408 KiB
#include <bits/stdc++.h> using namespace std; int n, m; namespace sub_1 { void solve() { int s; cin >> s; vector<int> a(m); for (auto &i : a) { cin >> i; } for (int mask = 0; mask < (1 << m); mask++) { int sum = 0; for (int i = 0; i < m; i++) { if ((mask >> i) & 1) { sum += a[i]; } } if (sum == s) { cout << "YES" << '\n'; return; } } cout << "NO" << '\n'; } } namespace sub_2_3 { void solve() { if (n > m) { cout << "NO" << '\n'; return; } vector<int> a(n + 1), b(m); for (int i = 1; i <= n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } vector<int> sum(1 << m); for (int mask = 1; mask < (1 << m); mask++) { for (int i = 0; i < m; i++) { if ((mask >> i) & 1) { sum[mask] += b[i]; } } } vector<vector<bool>> dp(n + 1, vector<bool>(1 << m)); dp[0][0] = 1; for (int i = 1; i <= n; i++) for (int mask = 1; mask < (1 << m); mask++) for (int subm = mask; subm > 0; subm = (subm - 1) & mask) { if (dp[i - 1][mask ^ subm] && sum[subm] == a[i]) { dp[i][mask] = 1; if (i == n) { cout << "YES" << '\n'; return; } } } cout << "NO" << '\n'; } } namespace sub_4 { void brute(int n, int mask, vector<int> &a, vector<vector<int>> &vec) { if (n == -1) { cout << "YES" << '\n'; exit(0); } for (auto &i : vec[n]) { if ((mask & i) == i) { brute(n - 1, mask ^ i, a, vec); } } } void solve() { vector<int> a(n), b(m); for (int i = 0; i < n; i++) { cin >> a[i]; } for (int i = 0; i < m; i++) { cin >> b[i]; } vector<vector<int>> vec(n); for (int mask = 1; mask < (1 << m); mask++) { int sum = 0; for (int i = 0; i < m; i++) { if ((mask >> i) & 1) sum += b[i]; } for (int i = 0; i < n; i++) { if (sum == a[i]) vec[i].push_back(mask); } } brute(n - 1, (1 << m) - 1, a, vec); cout << "NO" << '\n'; } } signed main() { cin.tie(0)->sync_with_stdio(0); cin >> n >> m; if (n == 1 && m <= 20) sub_1::solve(); else if (n <= 20 && m <= 14) sub_2_3::solve(); else sub_4::solve(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...