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;
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 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |