跳到正文

题解

青鸟 / bird

pystraf11约 11 分钟
原文
文章信息与版本

文章信息

发布
2026年11月28日
收录
2026年11月29日
更新
2026年11月29日
篇幅
约 11 分钟
题目
P18117

版本

  1. v1当前

Description

给定长 nn 的序列 aia_i 和常数 mm,你可以执行操作,选择 l≤x≤rl\le x\le r 并将 aa 的区间 [l,r][l,r] 都改为 xx。问零次或多次操作,得到所有本质不同序列 bb,求:

(∑b∏imbi) mod (109+7)\Big(\sum_b \prod_{i} m^{b_i}\Big)\bmod (10^9+7)

1≤n≤5000,1≤m<P,0≤ai≤n1\le n\le 5000,1\le m<P,0\le a_i\le n

Solution

讨论区给的简单一些的做法。

正向几乎无法刻画 bb,但是倒过来就是擦除:若 bi>0b_i> 0 且 ii 到 bib_i 之间(除了 ii)都已经擦掉,就能擦掉 ii,bi=0b_i=0 的永远擦不掉。

那么可以得到:bb 是合法的 ⟺\Longleftrightarrow 能擦除到剩下都是 ai=bia_i=b_i 的位置:

  • 正向:按最后一次被改的时间倒序处理,每次从中心 xx 往两边擦,一定能擦掉。
  • 反向:把擦除的过程倒过来,每步在 [min⁡(i,bi),max⁡(i,bi)][\min(i,b_i),\max(i,b_i)] 赋值 bib_i。被顺带改掉的位置后面会各自写回来,ii 自己不会再被碰到。

擦掉一个点,只会让接下来更容易擦,因此无论怎么擦,剩下的集合 SbS_b 都唯一。现在考虑 SS 里的点把 bb 分为若干段,段内的值必须都指向本段内部,因此每段独立且都要能擦干净。

同时每个保留点 uu 必须真的不能动:要么 au=0a_u=0,要么 au>ua_u>u 且右边相邻保留点位 ≤au\le a_u,要么 au<ua_u<u 且左边相邻保留点 ≥au\ge a_u。

现在把位置 ii 的权变为 mbi−im^{b_i-i} 考虑,则平移后指数不变,长度相同的段权一样,最后再乘回 ∏mi\prod m^i 就行。

现在先算每段,设段长为 dd 且值域 [1,d][1,d],要求可以擦空。钦定每次擦最小的可行点,设最后擦了 kk。显然左边先擦完,然后:

  • k=dk=d 右边为空,bk∈[1,d]b_k\in [1,d]。
  • k<dk<d 设右边最后擦掉 tt,则需 bk∈[t,d]b_k\in [t,d] 否则 tt 会被先擦。

设 fdf_d 为段权值和,gdg_d 为额外乘上 ∑x∈[t,d]mx\sum_{x\in [t,d]} m^x 的和,则 f0=1f_0=1,转移时右边平移后,bkb_k 的取值因子正好是 gg 里面的幂和:

  • k<dk<d 时候转移加上 fk−1gd−kf_{k-1}g_{d-k}。
  • k=dk=d 时候转移加上 fk−1∑x∈[1,d]mx−df_{k-1}\sum_{x\in [1,d]} m^{x-d}。

这些是容易算的。接下来要把段拼起来,两端加上哨兵位 0,n+10,n+1,设 dpi\text{dp}_i 为以 ii 为最后保留点的前缀权值和,枚举合法的上个保留点 j<ij<i,把 dpjfi−j−1\text{dp}_jf_{i-j-1} 转移过来,然后给 dpi\text{dp}_i 乘上 mai−im^{a_i-i} 最后取 dpn+1∏mi\text{dp}_{n+1}\prod m^i 即可。

Code
C++
#include <bits/stdc++.h>
using namespace std;

using i64 = long long;
using ui64 = unsigned long long;
using i128 = __int128;
using ui128 = unsigned __int128;
using f4 = float;
using f8 = double;
using f16 = long double;

template<class T>
bool chmax(T &a, const T &b){
    if(a < b){ a = b; return true; }
    return false;
}

template<class T>
bool chmin(T &a, const T &b){
    if(a > b){ a = b; return true; }
    return false;
}

template <unsigned M_> struct modint {
    static constexpr unsigned M = M_; unsigned x;
    constexpr modint() : x(0U) {}
    constexpr modint(unsigned x_) : x(x_ % M) {}
    constexpr modint(unsigned long long x_) : x(x_ % M) {}
    constexpr modint(int x_) : x(((x_ %= static_cast<int>(M)) < 0) ? (x_ + static_cast<int>(M)) : x_) {}
    constexpr modint(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
    modint &operator+=(const modint &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
    modint &operator-=(const modint &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
    modint &operator*=(const modint &a) { x = (static_cast<unsigned long long>(x) * a.x) % M; return *this; }
    modint &operator/=(const modint &a) { return *this *= a.inv(); }
    modint pow(long long e) const {
        if (e < 0) return inv().pow(-e);
        modint a = *this, b = 1U;
        for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; }
        return b;
    }
    modint inv() const {
        unsigned a = M, b = x; int y = 0, z = 1;
        for (; b;) {
            const unsigned q = a / b, c = a - q * b;
            a = b, b = c; const int w = y - static_cast<int>(q) * z; y = z, z = w;
        }
        return modint(y);
    }
    modint operator-() const { modint a; a.x = x ? (M - x) : 0U; return a; }
    modint operator+(const modint &a) const { return modint(*this) += a; }
    modint operator-(const modint &a) const { return modint(*this) -= a; }
    modint operator*(const modint &a) const { return modint(*this) *= a; }
    modint operator/(const modint &a) const { return modint(*this) /= a; }
    template <class T> friend modint operator+(T a, const modint &b) { return modint(a) += b; }
    template <class T> friend modint operator-(T a, const modint &b) { return modint(a) -= b; }
    template <class T> friend modint operator*(T a, const modint &b) { return modint(a) *= b; }
    template <class T> friend modint operator/(T a, const modint &b) { return modint(a) /= b; }
    bool operator==(const modint &a) const { return (x == a.x); }
    bool operator!=(const modint &a) const { return (x != a.x); }
    friend ostream &operator<<(ostream &os, const modint &a) { return os << a.x; }
};
using Z = modint<1000000007>;

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int c, t; cin >> c >> t;
    while (t--) {
        int n, m; cin >> n >> m;
        vector<int> a(n);
        for (int i = 0; i < n; i++) cin >> a[i], a[i]--;

        vector<Z> pw(n + 1), pre(n + 1); pw[0] = 1; Z fac = 1;
        for (int i = 0; i < n; i++) {
            pw[i + 1] = pw[i] * m;
            pre[i + 1] = pre[i] + pw[i + 1];
            fac *= pw[i + 1];
        }

        vector<Z> f(n + 1), g(n + 1); f[0] = 1; Z one = 0;
        for (int d = 0; d < n; d++) {
            one += pw[d].inv();
            for (int k = 0; k <= d; k++) {
                Z ways = f[k] * (k == d ? one : g[d - k]);
                f[d + 1] += ways, g[d + 1] += ways * (pre[d + 1] - pre[k]);
            }
        }

        vector<Z> dp(n + 2); dp[0] = 1;
        for (int i = 0; i <= n; i++) {
            for (int j = -1; j < i; j++) {
                if ((j == -1 || a[j] <= j || a[j] >= i)
                 && (i == n || a[i] > i || a[i] <= j))
                    dp[i + 1] += dp[j + 1] * f[i - j - 1];
            }
            if (i < n) dp[i + 1] *= (a[i] >= i ? pw[a[i] - i] : pw[i - a[i]].inv());
        }
        cout << dp[n + 1] * fac << '\n';
    }
    
    return 0;
}
原文