Description
给定长 的序列 和常数 ,你可以执行操作,选择 并将 的区间 都改为 。问零次或多次操作,得到所有本质不同序列 ,求:
Solution
讨论区给的简单一些的做法。
正向几乎无法刻画 ,但是倒过来就是擦除:若 且 到 之间(除了 )都已经擦掉,就能擦掉 , 的永远擦不掉。
那么可以得到: 是合法的 能擦除到剩下都是 的位置:
- 正向:按最后一次被改的时间倒序处理,每次从中心 往两边擦,一定能擦掉。
- 反向:把擦除的过程倒过来,每步在 赋值 。被顺带改掉的位置后面会各自写回来, 自己不会再被碰到。
擦掉一个点,只会让接下来更容易擦,因此无论怎么擦,剩下的集合 都唯一。现在考虑 里的点把 分为若干段,段内的值必须都指向本段内部,因此每段独立且都要能擦干净。
同时每个保留点 必须真的不能动:要么 ,要么 且右边相邻保留点位 ,要么 且左边相邻保留点 。
现在把位置 的权变为 考虑,则平移后指数不变,长度相同的段权一样,最后再乘回 就行。
现在先算每段,设段长为 且值域 ,要求可以擦空。钦定每次擦最小的可行点,设最后擦了 。显然左边先擦完,然后:
- 右边为空,。
- 设右边最后擦掉 ,则需 否则 会被先擦。
设 为段权值和, 为额外乘上 的和,则 ,转移时右边平移后, 的取值因子正好是 里面的幂和:
- 时候转移加上 。
- 时候转移加上 。
这些是容易算的。接下来要把段拼起来,两端加上哨兵位 ,设 为以 为最后保留点的前缀权值和,枚举合法的上个保留点 ,把 转移过来,然后给 乘上 最后取 即可。
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;
}