Submission #1359429

#TimeUsernameProblemLanguageResultExecution timeMemory
1359429domiKpart (eJOI21_kpart)C++20
100 / 100
288 ms880 KiB
#include <bits/stdc++.h>

#define int long long
#define fi first
#define se second

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()

#define lsb(x) (x & (-x))
#define vi vector<int>
#define YES { cout << "YES" << endl; return; }
#define NO { cout << "NO" << endl; return; }

using ll = long long;
using pii = std::pair<int, int>;

const int NMAX = 1e3;
const int SMAX = 5e4;

using namespace std;

int a[NMAX + 5], sp[NMAX + 5], dp[SMAX + 5], ans[NMAX + 5], n;

void solve_testcase() {
    cin >> n;

    int g = 0;
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
        g = gcd(g, a[i]);
    }

    for (int i = 1; i <= n; ++i) {
        a[i] /= g;
        sp[i] = sp[i - 1] + a[i];
    }


    int sz = (sp[n] / 2);
    fill(dp + 1, dp + sz + 2, INT_MAX);
    fill(ans + 1, ans + n + 1, 1LL);
    for (int i = n; i >= 1; --i) {
        dp[0] = i;
        for (int j = sz; j >= a[i]; --j)
            dp[j] = min(dp[j], dp[j - a[i]]);

        for (int j = i; j <= n; ++j) {
            int sum = sp[j] - sp[i - 1];
            bool good = (sum % 2 == 0 && dp[sum / 2] <= j);
            ans[j - i + 1] &= good;
        }
    }

    cout << accumulate(ans + 1, ans + n + 1, 0LL) << ' ';
    for (int i = 1; i <= n; ++i) {
        if (ans[i])
            cout << i << ' ';
    }
    cout << '\n';
}

signed main() {
    cin.tie(nullptr)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
        solve_testcase();
    return 0;
}
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...