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 <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
#include <cstdint>
int x;
int arr[505];
uint32_t knapsack[125005];
int freq[250005];
void knapsack_add(int val) {
for (int i = 125004; i >= val; i--) {
knapsack[i] += knapsack[i-val];
}
}
void knapsack_remove(int val) {
for (int i = 0; i < 125005-val; i++) {
knapsack[i+val] -= knapsack[i];
}
}
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
std::cout.tie(NULL);
std::cin >> x;
int sum = 0;
knapsack[0] = 1;
for (int i = 1; i <= x; i++) {
std::cin >> arr[i];
sum += arr[i];
knapsack_add(arr[i]);
}
if (sum%2==1) {
std::cout << "0\n";
return 0;
}
if (!knapsack[sum/2]) {
std::cout << "0\n";
return 0;
}
for (int i = x; i >= 1; i--) {
sum -= arr[i];
knapsack_remove(arr[i]);
if (i+1<=x) {
sum += arr[i+1];
knapsack_add(arr[i+1]);
}
for (int i = 0; i <= sum/2; i++) {
freq[sum-i-i] += !!knapsack[i];
}
}
std::vector<int> ret;
for (int i = 1; i < 250005; i++) {
if (freq[i]==x) {
ret.emplace_back(i);
}
}
std::cout << ret.size() << "\n";
for (const auto& i : ret) {
std::cout << i << " ";
}
std::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... |