[CF960G]Bandit Blues(第一类斯特林数+分治卷积)
Solution:
先考虑前缀,设 \(f(i, j)\) 为长度为 \(i\) 的排列中满足前缀最大值为自己的数有 \(j\) 个的排列数。
假设新加一个数 \(i+1\) 那么会有:
\[f(i,j)\rightarrow f(i + 1, j + 1)\\
f(i, j)\times i\rightarrow f(i + 1, j)
\]
即将 \(i+1\) 放在那哪个位置,会对后面产生贡献,综合一下,\(f(i, j)\) 就是第一类斯特林数 \(i \brack j\) 。
然后再考虑后缀,不难发现,对于长度为 \(n\) 的排列,前缀为自己的一定是在 \(n\) 以及 \(n\) 的左边,后缀为自己的一定在 \(n\) 及 \(n\) 的右边,于是可以枚举 \(n\) 的位置 \(i\),生成一个合法的方案为:先从 \(n-1\) 个数中选 \(i-1\) 个数,然后放在 \(n\) 两边,再将他们(两边互不干扰)分别分成 \(a-1, b-1\) 个环。
\[ans=\sum_{i=1}^n{~n - 1~\choose i - 1} {i - 1\brack a - 1}{n - i\brack b - 1}
\]
考虑组合意义,分成两个部分,环是可以拼在一起的,于是可以改变操作的顺序,即先分环,再分边。
\[ans = {n - 1\brack a + b - 2}{a + b - 2\choose a - 1}
\]
第一类斯特林数 \(n\brack i\) 的生成函数为:
\[F_n(x) =\prod_{i\geq0}^{n-1}(x + i)
\]
用分治卷积快速求出一行第一类斯特林数即可。
Code
#include <vector>
#include <cmath>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <iostream>
#include <algorithm>
typedef long long LL;
typedef unsigned long long uLL;
#define fir first
#define sec second
#define SZ(x) (int)x.size()
#define MP(x, y) std::make_pair(x, y)
#define PB(x) push_back(x)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define GO debug("GO\n")
#define rep(i, a, b) for (register int i = (a), i##end = (b); (i) <= i##end; ++ (i))
#define drep(i, a, b) for (register int i = (a), i##end = (b); (i) >= i##end; -- (i))
#define REP(i, a, b) for (register int i = (a), i##end = (b); (i) < i##end; ++ (i))
inline int read() {
register int x = 0; register int f = 1; register char c;
while (!isdigit(c = getchar())) if (c == '-') f = -1;
while (x = (x << 1) + (x << 3) + (c xor 48), isdigit(c = getchar()));
return x * f;
}
template<class T> inline void write(T x) {
static char stk[30]; static int top = 0;
if (x < 0) { x = -x, putchar('-'); }
while (stk[++top] = x % 10 xor 48, x /= 10, x);
while (putchar(stk[top--]), top);
}
template<typename T> inline bool chkmin(T &a, T b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
using namespace std;
const int MOD = 998244353;
const int maxn = 1e5 + 2;
LL qpow(LL a, LL b)
{
LL ans = 1;
while (b)
{
if (b & 1)
ans = ans * a % MOD;
a = a * a % MOD;
b >>= 1;
}
return ans;
}
int Inv(LL x)
{
return qpow(x, MOD - 2);
}
namespace Poly
{
const int G = 3;
int rev[maxn * 2], omega[maxn * 2], invomega[maxn * 2];
void init(int lim, int lg2)
{
REP (i, 0, lim) rev[i] = rev[i >> 1] >> 1 | (i & 1) << (lg2 - 1);
omega[0] = invomega[0] = 1;
omega[1] = qpow(G, (MOD - 1) / lim);
invomega[1] = Inv(omega[1]);
REP (i, 2, lim)
{
omega[i] = 1ll * omega[i - 1] * omega[1] % MOD;
invomega[i] = 1ll * invomega[i - 1] * invomega[1] % MOD;
}
}
void NTT(int a[], int lim, int omega[])
{
REP (i, 0, lim) if (rev[i] > i) swap(a[i], a[rev[i]]);
for (register int len = 2; len <= lim; len <<= 1)
{
register int m = len >> 1;
for (register int *p = a; p != a + lim; p += len)
for (register int i = 0; i < m; ++i)
{
register int t = 1ll * omega[lim / len * i] * p[i + m] % MOD;
p[i + m] = (1ll * p[i] - t + MOD) % MOD;
p[i] = (1ll * p[i] + t) % MOD;
}
}
}
void DFT(int a[], int lim)
{ NTT(a, lim, omega); }
void IDFT(int a[], int lim)
{
NTT(a, lim, invomega);
int inv = Inv(lim);
REP (i, 0, lim) a[i] = 1ll * a[i] * inv % MOD;
}
void Mul(const vector<int> a, const vector<int> b, vector<int> &c)
{
static int A[maxn * 2], B[2 * maxn];
int n = a.size(), m = b.size();
int lg2 = log2(n + m) + 1;
int lim = 1 << lg2;
copy(a.begin(), a.end(), A);
fill(A + n, A + lim, 0);
copy(b.begin(), b.end(), B);
fill(B + m, B + lim, 0);
init(lim, lg2);
DFT(A, lim);
DFT(B, lim);
REP (i, 0, lim) A[i] = 1ll * A[i] * B[i] % MOD;
IDFT(A, lim);
c.resize(n + m - 1);
copy(A, A + n + m - 1, c.begin());
}
}
vector<int> s[maxn * 4];
void solve(int o, int l, int r)
{
if (l == r)
{
s[o].push_back(l);
s[o].push_back(1);
return;
}
int mid = (l + r) >> 1;
solve(o << 1, l, mid);
solve(o << 1 | 1, mid + 1, r);
Poly::Mul(s[o << 1], s[o << 1 | 1], s[o]);
}
int Stirling1(int n, int m)
{
if (m == 0) return n == 0;
if (m < 0 || m > n) return 0;
if (n < 0) return 0;
solve(1, 0, n - 1);
return s[1][m];
}
int n, a, b;
void Input()
{
n = read(), a = read(), b = read();
}
int fac[maxn * 2];
void Init(int N)
{
fac[0] = 1;
rep (i, 1, N) fac[i] = 1ll * fac[i - 1] * i % MOD;
}
int combine(int n, int m)
{
if (n < 0 || m < 0 || n < m) return 0;
return 1ll * fac[n] * Inv(fac[m]) % MOD * Inv(fac[n - m]) % MOD;
}
void Solve()
{
cout << 1ll * Stirling1(n - 1, a + b - 2) * combine(a + b - 2, a - 1) % MOD << endl;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
Input();
Init(n * 2);
Solve();
return 0;
}