Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
Forget the original array for a moment. First classify what a self-produced sequence can look like.
Because all values are nonnegative, a zero is valid only if everything on its left sums to or everything on its right sums to .
So if the sequence has positive elements, any selected zeros can only sit before the first positive or after the last positive. Zeros between positives are dead on arrival.
Look at the positive block. Its first element must equal the sum of everything after it, and its last element must equal the sum of everything before it.
The only nonzero shape is with . Count all-zero subsequences, then count every equal nonzero pair with arbitrary zeros before the first and after the second.
Research note: I checked the official statement and the official Codeforces editorial/code. The official F tutorial text was not filled in, but the official solution code matches the formula derived below. No source text is copied.
Let be a chosen subsequence. All elements are nonnegative, which is the whole game here.
If the total sum of is , then every chosen element is , and every position is valid. This also includes the empty subsequence. The samples force this: for [0, 0], the answer is , not .
Now suppose contains at least one positive element. Consider a selected zero. For it to be valid, either the sum on the left is , or the sum on the right is . Since all numbers are nonnegative, this means either all elements to its left are zero, or all elements to its right are zero. So zeros cannot appear between positive elements. They may only form a prefix and/or suffix.
After deleting those leading and trailing zeros, we get a positive block where every .
For the first positive element, the left sum is , so it must use the right condition:
.
If is the sum of the positive block, then .
Similarly, the last positive element has right sum , so it must use the left condition:
,
so .
Therefore .
If , the single positive element would need to equal on one side, impossible. If , then the middle elements sum to
,
but all middle elements are positive. Also impossible. So the only possible positive block has length , and it must be .
So every valid subsequence is exactly one of these two forms:
That is the key observation. Everything else is just counting without stepping on a rake.
Let be the total number of zeros in the array.
All-zero subsequences: choose any subset of zero indices, so this contributes
.
Now count subsequences of the form . Pick two equal nonzero values at indices with .
For this pair to be the two positive elements:
Let be the number of zeros before index . Since is nonzero, the number of zeros after is .
So this pair contributes
.
Scan the array left to right. For every value , maintain
over earlier positions where .
When we are at a current nonzero position with value , all earlier equal positions can pair with it, so add
to the answer.
Then insert the current position as a possible first positive element:
.
Zeros just increment the current prefix-zero count.
We proved every valid subsequence is either all zeros or has the exact shape with .
For all-zero subsequences, choosing any subset of zero indices gives a valid subsequence, and every all-zero valid subsequence is counted once by .
For nonzero valid subsequences, the two positive elements uniquely determine a pair of equal nonzero indices . Once that pair is fixed, the only free choices are zeros before and zeros after . There are exactly such choices. These choices are all valid by direct checking: prefix zeros have left sum , the first sees the second on the right, the second sees the first on the left, and suffix zeros have right sum .
No subsequence is double-counted, because every nonzero valid subsequence has exactly two positive elements, hence exactly one such pair. Done.
For each test case, sorting/compressing the nonzero values costs , and the scan costs due to binary searches. Memory is .
The total over all test cases is at most , so this easily fits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int addmod(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), vals;
int zeroTotal = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) zeroTotal++;
else vals.push_back(a[i]);
}
vector<int> pow2(zeroTotal + 1, 1);
for (int i = 1; i <= zeroTotal; i++) {
pow2[i] = (ll)pow2[i - 1] * 2 % MOD;
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
vector<int> sum(vals.size(), 0);
int ans = pow2[zeroTotal];
int zerosBefore = 0;
for (int x : a) {
if (x == 0) {
zerosBefore++;
continue;
}
int id = int(lower_bound(vals.begin(), vals.end(), x) - vals.begin());
ans = (ans + (ll)sum[id] * pow2[zeroTotal - zerosBefore]) % MOD;
sum[id] = addmod(sum[id], pow2[zerosBefore]);
}
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int addmod(int a, int b) {
a += b;
if (a >= MOD) a -= MOD;
return a;
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), vals;
int zeroTotal = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (a[i] == 0) zeroTotal++;
else vals.push_back(a[i]);
}
vector<int> pow2(zeroTotal + 1, 1);
for (int i = 1; i <= zeroTotal; i++) {
pow2[i] = (ll)pow2[i - 1] * 2 % MOD;
}
sort(vals.begin(), vals.end());
vals.erase(unique(vals.begin(), vals.end()), vals.end());
vector<int> sum(vals.size(), 0);
int ans = pow2[zeroTotal];
int zerosBefore = 0;
for (int x : a) {
if (x == 0) {
zerosBefore++;
continue;
}
int id = int(lower_bound(vals.begin(), vals.end(), x) - vals.begin());
ans = (ans + (ll)sum[id] * pow2[zeroTotal - zerosBefore]) % MOD;
sum[id] = addmod(sum[id], pow2[zerosBefore]);
}
cout << ans << '\n';
}
}