青鸟 / bird

· · 题解

Description

给定长 n 的序列 a_i 和常数 m,你可以执行操作,选择 l\le x\le r 并将 a 的区间 [l,r] 都改为 x。问零次或多次操作,得到所有本质不同序列 b,求:

\Big(\sum_b \prod_{i} m^{b_i}\Big)\bmod (10^9+7) 1\le n\le 5000,1\le m<P,0\le a_i\le n

Solution

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

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

那么可以得到:b 是合法的 \Longleftrightarrow 能擦除到剩下都是 a_i=b_i 的位置:

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

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

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

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

设 f_d 为段权值和,g_d 为额外乘上 \sum_{x\in [t,d]} m^x 的和,则 f_0=1,转移时右边平移后,b_k 的取值因子正好是 g 里面的幂和:

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

:::info[Code]

#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;
}

::::