跳到正文

题解

?

DZ1000约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年12月5日
收录
2026年1月11日
更新
2026年11月12日
篇幅
约 3 分钟

版本

  1. v1当前

以下记 si=∑j=1iais_i = \displaystyle\sum_{j=1}^i a_i。

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

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

fi,sip=∑j=1i−1fj,sip−1f_{i,\frac{s_i}{p}} = \displaystyle\sum_{j=1}^{i-1}f_{j,\frac{s_i}{p}-1}

该 DP 可以前缀和优化。

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

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

code
C++
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';
	}
}
原文