发现设排列最大值位置为 \(i\),那么 \([1, i]\) 只可能存在前缀最大值,\([i, n]\) 只可能存在后缀最大值。
由此设 \(f_{i, j}\) 为长度为 \(i\) 的排列,前缀最大值有 \(j\) 个的方案数,有转移:
\[f_{i, j} = f_{i - 1, j - 1} + (i - 1)f_{i - 1, j} \]意思是每次可以填最大值,或者选一个不是最大值的空隙插进去。
那么答案为:
\[\sum\limits_{i = 1}^n f_{i - 1, a - 1} \times f_{n - i, b - 1} \times \binom{n - 1}{i - 1} \]容易发现 \(f_{i, j} = \begin{bmatrix} i \\ j \end{bmatrix}\),所以推到这一步应该就可以用 P5409 第一类斯特林数·列 的做法过了,但是这题不止于此。
考虑上式的组合意义就是,先从 \(n - 1\) 个球选出 \(i - 1\) 个,将它们组成 \(a - 1\) 个环,再把剩下的 \(n - i\) 个球组成 \(b - 1\) 个环,\(i\) 取遍 \([1, n]\)。听起来很怪对吧。
我们考虑直接把 \(n - 1\) 个球分成 \(a + b - 2\) 个环,考虑每种这样的方案在答案中被统计的次数。其实这个次数就是在分好的这 \(a + b - 2\) 个环选 \(a - 1\) 个环的方案数,因为 \(i\) 取遍 \([1, n]\)。
所以答案就是 \(\begin{bmatrix} n - 1 \\ a + b - 2 \end{bmatrix} \binom{a + b - 2}{a - 1}\)。使用 P5408 第一类斯特林数·行 的方法计算,复杂度还是 \(O(n \log n)\)。
code
// Problem: G. Bandit Blues
// Contest: Codeforces - Divide by Zero 2018 and Codeforces Round 474 (Div. 1 + Div. 2, combined)
// URL: https://codeforces.com/problemset/problem/960/G
// Memory Limit: 256 MB
// Time Limit: 3500 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
#define pb emplace_back
#define fst first
#define scd second
#define mkp make_pair
#define mems(a, x) memset((a), (x), sizeof(a))
using namespace std;
typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;
typedef pair<ll, ll> pii;
const int maxn = 500100;
const ll mod = 998244353, G = 3;
inline ll qpow(ll b, ll p) {
ll res = 1;
while (p) {
if (p & 1) {
res = res * b % mod;
}
b = b * b % mod;
p >>= 1;
}
return res;
}
ll n, A, B, r[maxn], fac[maxn], ifac[maxn];
typedef vector<ll> poly;
inline poly NTT(poly a, int op) {
int n = (int)a.size();
for (int i = 0; i < n; ++i) {
if (i < r[i]) {
swap(a[i], a[r[i]]);
}
}
for (int k = 1; k < n; k <<= 1) {
ll wn = qpow(op == 1 ? G : qpow(G, mod - 2), (mod - 1) / (k << 1));
for (int i = 0; i < n; i += (k << 1)) {
ll w = 1;
for (int j = 0; j < k; ++j, w = w * wn % mod) {
ll x = a[i + j], y = w * a[i + j + k] % mod;
a[i + j] = (x + y) % mod;
a[i + j + k] = (x - y + mod) % mod;
}
}
}
if (op == -1) {
ll inv = qpow(n, mod - 2);
for (int i = 0; i < n; ++i) {
a[i] = a[i] * inv % mod;
}
}
return a;
}
inline poly operator * (poly a, poly b) {
a = NTT(a, 1);
b = NTT(b, 1);
int n = (int)a.size();
for (int i = 0; i < n; ++i) {
a[i] = a[i] * b[i] % mod;
}
a = NTT(a, -1);
return a;
}
inline poly mul(poly a, poly b) {
int n = (int)a.size() - 1, m = (int)b.size() - 1, k = 0;
while ((1 << k) <= n + m + 1) {
++k;
}
for (int i = 1; i < (1 << k); ++i) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (k - 1));
}
poly A(1 << k), B(1 << k);
for (int i = 0; i <= n; ++i) {
A[i] = a[i];
}
for (int i = 0; i <= m; ++i) {
B[i] = b[i];
}
poly res = A * B;
res.resize(n + m + 1);
return res;
}
inline poly dmul(poly a, poly b) {
int n = (int)a.size();
reverse(a.begin(), a.end());
a = mul(a, b);
a.resize(n);
reverse(a.begin(), a.end());
return a;
}
inline poly work(poly a) {
int n = (int)a.size() - 1;
for (int i = 0; i <= n; ++i) {
a[i] = a[i] * fac[i] % mod;
}
poly b(n + 1);
ll pw = 1;
for (int i = 0; i <= n; ++i) {
b[i] = pw * ifac[i] % mod;
pw = pw * n % mod;
}
poly c = dmul(a, b);
for (int i = 0; i <= n; ++i) {
c[i] = c[i] * ifac[i] % mod;
a[i] = a[i] * ifac[i] % mod;
}
poly res = mul(a, c);
return res;
}
poly dfs(int n) {
if (n == 1) {
poly res(2);
res[1] = 1;
return res;
}
int m = n / 2;
poly res = dfs(m);
res = work(res);
if (n & 1) {
poly a(2);
a[0] = n - 1;
a[1] = 1;
res = mul(res, a);
}
return res;
}
inline ll C(ll n, ll m) {
if (n < m || n < 0 || m < 0) {
return 0;
} else {
return fac[n] * ifac[m] % mod * ifac[n - m] % mod;
}
}
void solve() {
scanf("%lld%lld%lld", &n, &A, &B);
fac[0] = 1;
for (int i = 1; i <= n; ++i) {
fac[i] = fac[i - 1] * i % mod;
}
ifac[n] = qpow(fac[n], mod - 2);
for (int i = n - 1; ~i; --i) {
ifac[i] = ifac[i + 1] * (i + 1) % mod;
}
if (!A || !B || A + B - 1 > n) {
puts("0");
return;
}
if (n == 1) {
puts("1");
return;
}
poly res = dfs(n - 1);
printf("%lld\n", res[A + B - 2] * C(A + B - 2, A - 1) % mod);
}
int main() {
int T = 1;
// scanf("%d", &T);
while (T--) {
solve();
}
return 0;
}