Submission #1109757

#TimeUsernameProblemLanguageResultExecution timeMemory
1109757Kirill22Bootfall (IZhO17_bootfall)C++17
100 / 100
721 ms4572 KiB
#include "bits/stdc++.h" using namespace std; const int mod = 998244353, mod2 = (int) 1e9 + 7; const int N = 500 * 500 + 22; int dp[N], dp2[N]; void solve() { int n; cin >> n; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } int S = std::accumulate(a.begin(), a.end(), 0); dp[0] = dp2[0] = 1; for (auto& x : a) { for (int j = N - 1; j >= x; j--) { dp[j] += dp[j - x]; if (dp[j] >= mod) { dp[j] -= mod; } dp2[j] += dp2[j - x]; if (dp2[j] >= mod2) { dp2[j] -= mod2; } } } vector<int> good(S, 0); if (S % 2 != 0 || (dp[S / 2] == 0 && dp2[S / 2] == 0)) { cout << 0 << '\n'; return; } for (int i = 0; i < n; i++) { int x = a[i]; for (int j = x; j < N; j++) { dp[j] -= dp[j - x]; if (dp[j] < 0) { dp[j] += mod; } dp2[j] -= dp2[j - x]; if (dp2[j] < 0) { dp2[j] += mod2; } } for (int val = 1; val < S; val++) { int Sval = S + val - x; if (Sval % 2 != 0 || (dp[Sval / 2] == 0 && dp2[Sval / 2] == 0)) { continue; } good[val]++; } for (int j = N - 1; j >= x; j--) { dp[j] += dp[j - x]; if (dp[j] >= mod) { dp[j] -= mod; } dp2[j] += dp2[j - x]; if (dp2[j] >= mod2) { dp2[j] -= mod2; } } } vector<int> ans; for (int i = 0; i < S; i++) { if (good[i] == n) { ans.push_back(i); } } cout << ans.size() << '\n'; for (auto& i : ans) { cout << i << " "; } cout << '\n'; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...