Submission #342626

#TimeUsernameProblemLanguageResultExecution timeMemory
342626SortingBootfall (IZhO17_bootfall)C++17
100 / 100
131 ms18988 KiB
#include <bits/stdc++.h> using namespace std; const int N = 500 + 3; int n, a[N], sum; bitset<N * N> dp[N]; int is_ans[N * N]; vector<int> ans; void dnc(int l, int r, const bitset<N * N> &b){ if(l == r) dp[l] = b; else{ int mid = (l + r) >> 1; bitset<N * N> new_b; new_b = b; for(int i = l; i <= mid; ++i) new_b |= new_b << a[i]; dnc(mid + 1, r, new_b); new_b = b; for(int i = mid + 1; i <= r; ++i) new_b |= new_b << a[i]; dnc(l, mid, new_b); } } int main(){ ios::sync_with_stdio(false); cin.tie(NULL); cin >> n; for(int i = 0; i < n; ++i){ cin >> a[i]; sum += a[i]; } a[n] = 0; if(sum & 1){ cout << "0\n"; return 0; } bitset<N * N> curr; curr[0] = 1; dnc(0, n, curr); if(!dp[n][(sum >> 1)]){ cout << "0\n"; return 0; } for(int i = 0; i < n; ++i){ for(int j = (sum - a[i]) & 1; j <= sum; j += 2){ int new_sum = sum - a[i] + j; if(dp[i][(new_sum >> 1)]) is_ans[j]++; } } for(int i = 0; i <= sum; ++i) if(is_ans[i] == n) ans.push_back(i); cout << ans.size() << "\n"; for(int x: ans) cout << x << " "; cout << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...