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>
#define fi first
#define se second
#define mp make_pair
#define TASK ""
#define ALL(x) (x).begin(), (x).end()
using namespace std;
template <typename T1, typename T2> bool mini(T1 &a, T2 b) {
if (a > b) {a = b; return true;} return false;
}
template <typename T1, typename T2> bool maxi(T1 &a, T2 b) {
if (a < b) {a = b; return true;} return false;
}
const int N = 25e4 + 5;
const int oo = 1e9;
const long long ooo = 1e18;
const int mod = 1e9 + 7; // 998244353;
const long double pi = acos(-1);
bitset <N> dp;
int a[N];
int n, tot;
int cnt[2 * N];
void solve(int l, int r, bitset <N> &dp) {
if (l == r) {
for (int i = 0; i <= (tot >> 1); i++) {
int x = i;
int y = tot - i - a[l];
if (x <= y)
cnt[y - x] += dp[i];
}
// cerr << "left " << l << "\n";
// for (int i = 0; i <= (tot >> 1); i++)
// if (dp[i])
// cerr << i << " ";
// cerr << "\n";
return;
}
int mid = (l + r) >> 1;
bitset <N> tmp = dp;
for (int i = l; i <= mid; i++)
tmp |= (tmp << a[i]);
solve(mid + 1, r, tmp);
for (int i = mid + 1; i <= r; i++)
dp |= (dp << a[i]);
solve(l, mid, dp);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef ONLINE_JUDGE
// freopen(TASK".inp","r",stdin);
// freopen(TASK".out","w",stdout);
#endif
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
tot = accumulate(a + 1, a + n + 1, 0);
dp[0] = true;
for (int i = 1; i <= n; i++)
dp |= (dp << a[i]);
if ((tot & 1) || !dp[tot >> 1])
return cout << 0, 0;
dp.reset();
dp[0] = 1;
solve(1, n, dp);
vector <int> res;
for (int i = 1; i <= tot; i++)
if (cnt[i] == n)
res.push_back(i);
cout << res.size() << "\n";
for (int x : res)
cout << x << " ";
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... |