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 kN = 500;
const int kMax = kN * kN;
int a[1 + kN];
bitset<1 + kMax> dp, evenStr, oddStr, pref[1 + kN], suff[1 + kN + 1];
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
int sum = 0;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
sum += a[i];
}
pref[0][0] = true;
for (int i = 1; i <= n; ++i) {
pref[i] = pref[i - 1] | (pref[i - 1] << a[i]);
}
suff[n + 1][0] = true;
for (int i = n; i > 0; --i) {
suff[i] = suff[i + 1] | (suff[i + 1] << a[i]);
}
if (sum % 2 == 1 || !pref[n].test(sum / 2)) {
cout << "0\n";
return 0;
}
bool even = true, odd = true;
evenStr.set();
oddStr.set();
evenStr[0].flip();
for (int i = 1; i <= n; ++i) {
if (i <= n / 2) {
dp = suff[i + 1];
for (int j = 1; j < i; ++j) {
dp |= dp << a[j];
}
} else {
dp = pref[i - 1];
for (int j = i + 1; j <= n; ++j) {
dp |= dp << a[j];
}
}
int rest = sum - a[i];
if (rest % 2 == 0) {
if (even) {
evenStr &= dp >> (rest / 2);
}
if (odd) {
odd = false;
oddStr = 0;
}
} else {
if (odd) {
oddStr &= dp >> (rest / 2 + 1);
}
if (even) {
even = false;
evenStr = 0;
}
}
}
cout << evenStr.count() + oddStr.count() << '\n';
for (int i = 0; i < kMax; ++i) {
if (evenStr.test(i)) {
cout << 2 * i << ' ';
} else if (oddStr.test(i)) {
cout << 2 * i + 1 << ' ';
}
}
cout << '\n';
return 0;
}
# | 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... |