题目大意:
就是你有一个
n
n
n的全排列,现在问你去重排这个排列使得对于给定的
k
k
k,满足对于任意的
a
i
,
i
>
k
a_i,i>k
ai,i>k的都有
a
i
>
m
i
n
(
a
i
−
1
,
.
.
.
,
a
i
−
k
)
a_i>min(a_{i-1},...,a_{i-k})
ai>min(ai−1,...,ai−k)
求排列数
解题思路:
这里很妙的一点就是我们知道对于最小的数肯定是一定要放到
[
1
,
k
]
[1,k]
[1,k]里面去的,然后最小的数肯定会把数列切成两半,前面那一半是随便选,后面剩下的数又变成新的问题。
那么就是新的问题
那么
d
p
[
i
]
=
∑
j
=
1
k
A
i
−
1
j
−
1
d
p
[
i
−
j
]
dp[i]=\sum_{j=1}^{k} A^{j-1}_{i-1} dp[i-j]
dp[i]=∑j=1kAi−1j−1dp[i−j]
预处理组合数然后前缀和维护
d
p
dp
dp值就行了
AC code
#include <bits/stdc++.h>
#define mid ((l + r) >> 1)
#define Lson rt << 1, l , mid
#define Rson rt << 1|1, mid + 1, r
#define ms(a,al) memset(a,al,sizeof(a))
#define log2(a) log(a)/log(2)
#define lowbit(x) ((-x) & x)
#define IOS std::ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define INF 0x3f3f3f3f
#define LLF 0x3f3f3f3f3f3f3f3f
#define f first
#define s second
#define endl '\n'
using namespace std;
const int N = 1e7 + 10, mod = 998244353;
const int maxn = 500010;
const long double eps = 1e-5;
const int EPS = 500 * 500;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<ll,ll> PLL;
typedef pair<double,double> PDD;
template<typename T> void read(T &x) {
x = 0;char ch = getchar();ll f = 1;
while(!isdigit(ch)){if(ch == '-')f*=-1;ch=getchar();}
while(isdigit(ch)){x = x*10+ch-48;ch=getchar();}x*=f;
}
template<typename T, typename... Args> void read(T &first, Args& ... args) {
read(first);
read(args...);
}
int n, k;
inline ll qmi(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1) res = (res * a) % mod;
b >>= 1;
a = a * a % mod;
}
return res;
}
ll fac[N], inv[N], sum[N], dp[N];
inline void init() {
fac[0] = 1;
for(int i = 1; i < N; ++ i) fac[i] = (fac[i-1] * i) % mod;
inv[N - 1] = qmi(fac[N-1],mod-2);
for(int i = N - 2; i >= 0; -- i) inv[i] = inv[i+1] * (i+1) % mod;
}
int main() {
IOS;
init();
cin >> n >> k;
dp[0] = 1;
sum[0] = 1;
for(int i = 1; i <= n; ++ i) {
dp[i] = (sum[i-1] - (i-k-1>=0?sum[i-k-1]:0) % mod + mod) % mod;
dp[i] = dp[i] * fac[i-1] % mod;
sum[i] = (sum[i-1] + (dp[i] * inv[i]) % mod) % mod;
}
cout << dp[n] % mod;
return 0;
}