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 <vector>
#include <algorithm>
#include <cmath>
#define ff first
#define ss second
typedef long long ll;
using namespace std;
const int P1 = 727196341;
const int P2 = 271915031;
const int N = 500 + 10;
int a[N];
int dp1[N * N];
int dp2[N * N];
int cnt[N * N];
void add(int x) {
for (int i = N * N - 1; i >= x; i--) {
dp1[i] = (dp1[i - x] + dp1[i]) % P1;
dp2[i] = (dp2[i - x] + dp2[i]) % P2;
}
}
void sub(int x) {
for (int i = x; i < N * N; i++) {
dp1[i] = (dp1[i] - dp1[i - x] + P1) % P1;
dp2[i] = (dp2[i] - dp2[i - x] + P2) % P2;
}
}
int main() {
int n;
cin >> n;
int sm = 0;
dp1[0] = 1;
dp2[0] = 1;
for (int i = 1; i <= n; i++) {
cin >> a[i];
add(a[i]);
sm += a[i];
}
if (sm % 2 != 0) {
cout << 0 << "\n";
return 0;
}
if (dp1[sm / 2] == 0 && dp2[sm / 2] == 0) {
cout << 0 << "\n";
return 0;
}
for (int i = 1; i <= n; i++) {
sub(a[i]);
sm -= a[i];
for (int i = 0; 2 * i < sm; i++) {
if ((dp1[i] != 0 || dp2[i] != 0)) {
cnt[sm - 2 * i]++;
}
}
add(a[i]);
sm += a[i];
}
vector <int> ans;
for (int i = 1; i < N * N; i++) {
if (cnt[i] == n) ans.push_back(i);
}
cout << (int)ans.size() << "\n";
for (auto it : ans) {
cout << it << " ";
}
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... |