Submission #174659

#TimeUsernameProblemLanguageResultExecution timeMemory
174659srvltBootfall (IZhO17_bootfall)C++14
100 / 100
909 ms5072 KiB
//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree <pair <int, int>, null_type, less <pair <int, int> >, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;

#define ll long long
#define db double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define size(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()

#define endl "\n"
//#define int long long

using namespace std;

void dout() {
    cerr << endl;
}
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
    cerr << H << ' ';
    dout(T...);
}

//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;

const int N = 503, K = 250001;
const pii MOD = {1e9 + 7, 1e9 + 9};
int n, a[N], ans[K], res[K];
pii dp[K];

void add(pii & x, pii y) {
    x.fi += y.fi;
    if (x.fi >= MOD.fi) {
        x.fi -= MOD.fi;
    }
    x.se += y.se;
    if (x.se >= MOD.se) {
        x.se -= MOD.se;
    }
}

void sub(pii & x, pii y) {
    x.fi -= y.fi;
    if (x.fi < 0) {
        x.fi += MOD.fi;
    }
    x.se -= y.se;
    if (x.se < 0) {
        x.se += MOD.se;
    }
}

bool exist(int x) {
    return dp[x].fi > 0 || dp[x].se > 0;
}

void solve(int tc) {
    // check for (int i = 0; i < n; j++)
    cin >> n;
    dp[0] = {1, 1};
    int total = 0;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        total += a[i];
        for (int j = K - 1; j >= a[i]; j--) {
            add(dp[j], dp[j - a[i]]);
        }
    }
    if ((total & 1) || !exist(total >> 1)) {
        cout << 0;
        return;
    }
    fill(ans, ans + K, 1);
    for (int i = 1; i <= n; i++) {
        for (int j = a[i]; j < K; j++) {
            sub(dp[j], dp[j - a[i]]);
        }
        total -= a[i];

        memset(res, 0, sizeof(res));
        for (int j = total & 1; j <= total; j += 2) {
            if (exist(total + j >> 1) || exist(total - j >> 1)) {
                res[j] = 1;
            }
        }
        for (int j = 0; j < K; j++) {
            ans[j] &= res[j];
        }
        memset(res, 0, sizeof(res));

        total += a[i];
        for (int j = K - 1; j >= a[i]; j--) {
            add(dp[j], dp[j - a[i]]);
        }
    }
    int cnt = 0;
    for (int i = 0; i < K; i++) {
        if (ans[i]) {
            cnt++;
        }
    }
    cout << cnt << endl;
    for (int i = 0; i < K; i++) {
        if (ans[i]) {
            cout << i << " ";
        }
    }
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);
    int tc = 1;
//    cin >> tc;
    for (int i = 0; i < tc; i++) {
        solve(i);
//        cleanup();
    }
}

Compilation message (stderr)

bootfall.cpp: In function 'void solve(int)':
bootfall.cpp:93:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
             if (exist(total + j >> 1) || exist(total - j >> 1)) {
                       ~~~~~~^~~
bootfall.cpp:93:54: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
             if (exist(total + j >> 1) || exist(total - j >> 1)) {
                                                ~~~~~~^~~
#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...