青鸟 题解
Generated by GPT-6 Astra high.
1. Reverse the operations: remove positions
Fix a proposed final array
For
We may remove
Why? After constructing the final values at the other remaining positions, we can paint
with color
Thus removal means:
“We can restore this position later without disturbing the positions still under consideration.”
A final array is reachable precisely when some sequence of these removals leaves only positions satisfying
The reverse direction follows from the painting construction. For necessity, consider the last painting operation touching the remaining positions: one of the positions nearest to its color-index is removable.
2. Remove everything possible: obtain a unique core
Removing positions only expands the intervals between surviving neighbors. Therefore:
Once a position becomes removable, removing other positions cannot make it unremovable.
Consequently, repeatedly removing any available position produces the same final set, regardless of order. Call it the core.
Then
This gives a duplicate-free way to classify final arrays: by their unique core.
Suppose the core positions are
with sentinels
There are two requirements.
Requirement A: gaps disappear completely
Every open interval
must be fully removable while its two endpoints remain.
Requirement B: core positions cannot disappear
At core position
Otherwise it would be removable.
We will calculate gap weights first, then sum over cores.
3. Gap weights depend only on length
Let
The empty interval has weight
Write
A fully removable gap of length
Subtract
Therefore,
This identity is what makes the full algorithm quadratic.
We only need to compute
4. Deriving the gap DP
This is the hardest part: different removal orders must not be counted separately.
Define
The total weight of choosing a value strictly between
All arithmetic below is modulo
4.1 Inclusion–exclusion over the last remaining position
Consider the gap
Let
If
Hence
We cannot simply sum these: an array may permit several choices of last position.
For a nonempty set
the intersection has weight
Here:
- every gap between consecutive selected positions disappears completely;
- the value at selected position
s_h lies between its neighboring selected positions.
One way to justify sufficiency is the following useful characterization:
An assignment inside
(l,r) is fully removable iff all its values lie in(l,r) and there is no pairi<j withb_i\ge j,\qquad b_j\le i.
Such a pair blocks itself forever. Conversely, a nonempty stuck core must contain such a pair: its first position points right, its last points left, so some consecutive pair forms a right-to-left transition.
The restrictions in the intersection formula prevent these blocking pairs across its gaps.
Now inclusion–exclusion gives
This looks exponential, but the factor
can be separated into two terms.
4.2 Compress the inclusion–exclusion sum
We only need the case
Think of a selected set as an increasing path starting at
Initialize the paths with no selected internal position:
Extending a path from
Define two aggregates:
Then extension to
Inclusion–exclusion implies a cancellation:
Indeed, for
Thus only the
Final recurrence
Initialize
For
For each
Then
and
There are
Avoid exponentiation inside the loops
For fixed
is multiplied by
Maintain these powers incrementally in an array. No modular exponentiation is needed inside the DP.
As a sanity check, for
5. Count the cores in another O(n^2) DP
Assume the gap weights are available through
Conceptually, define
- All earlier core positions have already been checked.
- Position
j is waiting for its right neighbor. - Its weight
q_{a_j} is already included. -
When we choose the next core position
So the coefficient needed for the transition is
This only needs two aggregates:
Therefore the transition coefficient is
This avoids storing the two-dimensional DP.
Initialization
If
Add this to
It belongs to
which means
The empty-core contribution is simply
Transition
For
Update
If
To finish at the right sentinel
to the answer. There is no sentinel value weight.
Again, this takes
6. Complete C++17 code
The code below uses standard input/output. For the original file-based contest, enable the two freopen lines.
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;
constexpr int64 MOD = 1000000007;
int64 solve(int n, int64 p, const vector<int>& a) {
// q[x] = p^x
vector<int64> q(n + 3, 1);
for (int i = 1; i <= n + 2; ++i) {
q[i] = q[i - 1] * p % MOD;
}
// Q[t] = q[0] + ... + q[t - 1]
vector<int64> Q(n + 3, 0);
for (int i = 0; i <= n + 1; ++i) {
Q[i + 1] = (Q[i] + q[i]) % MOD;
}
// Phase 1: fully removable gap weights.
vector<int64> f(n + 1, 0);
vector<int64> B(n + 2, 0);
vector<int64> power(n + 2, 0);
f[0] = 1;
B[1] = Q[1];
for (int r = 2; r <= n + 1; ++r) {
// New j = r - 1 has gap length zero.
power[r - 1] = 1;
int64 sum = 0;
int64 moment = 0;
for (int j = 1; j < r; ++j) {
int64 coefficient;
if (j == 1) {
coefficient = (Q[r] - Q[1] + MOD) % MOD;
} else {
coefficient = (MOD - B[j]) % MOD;
}
// power[j] = p^(j * (r - j - 1))
int64 term = f[r - j - 1] * power[j] % MOD;
term = term * coefficient % MOD;
sum = (sum + term) % MOD;
moment = (moment + Q[j + 1] * term) % MOD;
// Prepare for the next r.
power[j] = power[j] * q[j] % MOD;
}
f[r - 1] = sum;
B[r] = (Q[1] * sum % MOD - moment + MOD) % MOD;
}
// Phase 2: count unique cores.
vector<int64> total(n + 2, 0);
vector<int64> eligible(n + 2, 0);
for (int k = 1; k <= n; ++k) {
int64 value = f[k - 1] * q[a[k]] % MOD;
total[k] = value;
// Previous core position is sentinel 0.
if (a[k] == 0) {
eligible[k] = value;
}
}
// Empty core: every position is removable.
int64 answer = f[n];
for (int j = 1; j <= n; ++j) {
// At k = j + 1, p^(j * (k - j - 1)) = 1.
int64 shift = 1;
for (int k = j + 1; k <= n + 1; ++k) {
int64 coefficient =
(k <= a[j] ? total[j] : eligible[j]);
int64 value = coefficient * f[k - j - 1] % MOD;
value = value * shift % MOD;
if (k == n + 1) {
// Right sentinel has no value weight.
answer = (answer + value) % MOD;
} else {
value = value * q[a[k]] % MOD;
total[k] = (total[k] + value) % MOD;
if (j >= a[k]) {
eligible[k] = (eligible[k] + value) % MOD;
}
}
shift = shift * q[j] % MOD;
}
}
return answer;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
// freopen("bird.in", "r", stdin);
// freopen("bird.out", "w", stdout);
int c, t;
cin >> c >> t;
while (t--) {
int n;
int64 p;
cin >> n >> p;
vector<int> a(n + 1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
cout << solve(n, p, a) << '\n';
}
return 0;
}
Complexity
For each test case:
No modular division or inverse is used, so
The solution in four steps
- Reverse painting into position removal.
- Use the unique unremovable core to avoid counting operation orders.
- Compute fully removable gap weights with inclusion–exclusion, then use index-shift symmetry to reduce them to a one-dimensional array.
- Count cores with two aggregate states per last position.
The major obstacle was not the final core DP. It was finding a duplicate-free gap recurrence—and then noticing that
turns the cubic interval computation into the full quadratic solution.