?

· · 题解

以下记 s_i = \displaystyle\sum_{j=1}^i a_i。

考虑 DP:如果 s_n > 0,则枚举 s_n 的因数 p。设 f_{i,j} 为将数组前 i 项分成 j 段,其中每一段总和为 p 的方案数。

如果 p\mid s_i,则有如下转移:

f_{i,\frac{s_i}{p}} = \displaystyle\sum_{j=1}^{i-1}f_{j,\frac{s_i}{p}-1}

该 DP 可以前缀和优化。

如果 s_n < 0,把数组每一个数取反后按 s_n > 0 做即可。

如果 s_n = 0,设原数组可以分为最多 c 段,则答案即为 2^{c-1}。

:::success[code]

namespace DZ1000 {
    typedef long long ll;
    constexpr ll N = 1e5 + 10, P = 998244353;
    ll t, n, ans;
    array<ll, N> s, dp;

    ll qpow(ll a, ll b) {
        ll res = 1;
        for(; b; b >>= 1, a = a * a % P) {
            if(b & 1) {
                res = res * a % P;
            }
        }
        return res;
    }

    ll calc0() {
        ll cnt = 0;
        for(ll i = 1, j = 0; i <= n; i++) {
            if(s[i] == s[j]) {
                cnt++, j = i;
            }
        }
        return qpow(2, cnt - 1);
    }

    ll calc(ll x) {
        if(s[n] / x > n) {
            return 0;
        }
        if(s[n] == x) {
            return 1;
        }
        dp.fill(0);
        dp[0] = 1;
        for(ll i = 1; i < n; i++) {
            if((s[i] == 0) || ((s[i] > 0) ^ (x > 0)) || (s[i] % x) || (s[i] / x > n)) {
                continue;
            }
            dp[s[i] / x] = (dp[s[i] / x] + dp[s[i] / x - 1]) % P;
        }
        return dp[s[n] / x - 1];
    }

    void main() {
        cin >> n;
        for(ll i = 1; i <= n; i++) {
            cin >> s[i];
            s[i] += s[i - 1];
        }
        if(s[n] == 0) {
            cout << calc0() << '\n';
            return;
        }
        if(s[n] < 0) {
            for(ll i = 1; i <= n; i++) {
                s[i] = -s[i];
            }
        }
        ans = 0;
        for(ll k = 1; k * k <= s[n]; k++) {
            if(s[n] % k == 0) {
                ans = (ans + calc(k) + calc(s[n] / k)) % P;
            }
        }
        cout << ans << '\n';
    }
}

:::