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.
Because is sorted, every subsequence is also sorted. Group equal values together and focus on whether each group contributes an even or odd number of chosen indices.
First ignore . For selected positives , an odd gives . For even , pair the sum as . When can that equal zero?
The positive alternating sum is zero exactly when every value is selected an even number of times. From a group of size , there are even-sized subsets, so if there are distinct values, these choices multiply to .
Now split by the parity of the selected s. An even number cancels and gives the previous case. An odd number contributes and reverses the starting sign of the positive suffix, so the positives' usual alternating sum must equal .
A sorted positive sequence has alternating sum exactly when two groups and are selected an odd number of times and every other positive group is selected evenly. Let count occurring pairs . Therefore the answer is without , and when occurs, where counts all distinct values including .
The non-decreasing order is doing basically all the work here. The actual magnitudes up to are mostly a decoy; what matters is the parity of how many indices we choose from each equal-value group.
Suppose some value occurs times. Exactly half of its subsets have even size and half have odd size, so either prescribed parity has
choices. One way to see this is to fix one occurrence and toggle whether it is selected; this pairs every even-sized subset with a unique odd-sized subset.
This counts actual index subsets, not merely possible counts, so duplicate values are handled correctly.
If the array has distinct values with group sizes , then any fixed assignment of even/odd parity to every group has
realizations.
Call this common quantity .
Take any selected positive sequence
If is odd, rewrite its alternating sum as
Every parenthesized difference is non-negative and , so this sum is strictly positive. It cannot be zero.
If is even, pair consecutive positions:
Every term is non-positive because the sequence is sorted. Therefore exactly when every pair contains equal values.
That is equivalent to selecting every distinct value an even number of times:
Thus, when is absent, every group must have even selected size. There are exactly
valid subsequences. The empty subsequence is already included.
Because the entire array is sorted, all occurrences of come before every positive value. Let be the number of selected s.
Their contribution is
Since is even, the positive suffix starts with a plus sign. Its alternating sum must therefore be zero, which requires every positive group to be selected evenly.
So every group, including the group, has prescribed even parity. This contributes exactly subsequences.
The selected s contribute . They also occupy an odd number of positions, so the first selected positive receives a minus sign.
Let denote the positives' usual alternating sum starting with plus. The complete sum is then
For it to be zero, we need
We now characterize exactly when that happens.
An odd-length positive sequence has , as proved earlier, so its length must be even. Consider the positive value groups selected an odd number of times, and list their distinct values as
Groups selected evenly contribute zero and do not change the sign entering the next group. Each odd group contributes one uncancelled copy and flips that sign. Consequently,
Each difference is at most . Their sum can be exactly only when
Therefore, for some positive value , the groups and must be selected oddly, while every other positive group is selected evenly. The group is also selected oddly.
Let be the number of positive values for which both and occur in the array. Every such pair defines one parity assignment, and each assignment has exactly realizations. Different pairs give different parity assignments, so there is no double-counting.
If does not occur, only the positive-only case exists:
If occurs, there is one all-even parity assignment and valid odd- assignments:
Here is the number of all distinct array values, including when present.
For example, in , we have , so . The consecutive positive pairs are and , hence and the answer is .
Scan the already sorted array and count:
Then apply the formula above using precomputed powers of two modulo .
For positive-only selections, the pairing argument proves that alternating sum zero is equivalent to every value group having even selected size.
With an even number of selected s, those elements cancel and do not reverse the positive signs, so the same all-even condition is necessary and sufficient.
With an odd number of selected s, the total is zero exactly when the positives have usual alternating sum . The odd-group expansion proves this happens exactly when one occurring pair has odd counts and every other positive group has an even count.
Every fixed parity assignment has index subsets. Hence the algorithm counts one such assignment without , or disjoint assignments when exists. Therefore the returned formula counts every valid subsequence exactly once.
Across one test case, the scan takes time and auxiliary space. The shared powers-of-two table uses memory and preprocessing time.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
constexpr int MAX_N = 200000;
constexpr ll MOD = 1000000007;
vector<ll> pow2(MAX_N + 1, 1);
for (int i = 1; i <= MAX_N; ++i) {
pow2[i] = pow2[i - 1] * 2 % MOD;
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll previous;
cin >> previous;
bool hasMinusOne = (previous == -1);
int distinct = 1;
int consecutivePairs = 0;
for (int i = 1; i < n; ++i) {
ll current;
cin >> current;
if (current != previous) {
++distinct;
if (previous > 0 && current == previous + 1) {
++consecutivePairs;
}
}
previous = current;
}
ll answer = pow2[n - distinct];
if (hasMinusOne) {
answer = answer * (consecutivePairs + 1) % MOD;
}
cout << answer << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
constexpr int MAX_N = 200000;
constexpr ll MOD = 1000000007;
vector<ll> pow2(MAX_N + 1, 1);
for (int i = 1; i <= MAX_N; ++i) {
pow2[i] = pow2[i - 1] * 2 % MOD;
}
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
ll previous;
cin >> previous;
bool hasMinusOne = (previous == -1);
int distinct = 1;
int consecutivePairs = 0;
for (int i = 1; i < n; ++i) {
ll current;
cin >> current;
if (current != previous) {
++distinct;
if (previous > 0 && current == previous + 1) {
++consecutivePairs;
}
}
previous = current;
}
ll answer = pow2[n - distinct];
if (hasMinusOne) {
answer = answer * (consecutivePairs + 1) % MOD;
}
cout << answer << '\n';
}
}