CF960G Bandit Blues 【第一类斯特林数 + 分治NTT】

题目链接

CF960G

题解

FJOI2016只不过数据范围变大了
考虑如何预处理第一类斯特林数
性质

\[x^{\overline{n}} = \sum\limits_{i = 0}^{n}\begin{bmatrix} n \\ i \end{bmatrix}x^{i} \]

分治\(NTT\)即可在\(O(nlog^2n)\)的时间内预处理出同一个\(n\)的所有\(\begin{bmatrix} n \\ i \end{bmatrix}\)
其实还有比较优美的倍增\(fft\)\(O(nlogn)\)的方法

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 0x3f3f3f3f;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
	return flag ? out : -out;
}
const int P = 998244353,G = 3;
inline int qpow(int a,int b){
	int re = 1;
	for (; b; b >>= 1,a = 1ll * a * a % P)
		if (b & 1) re = 1ll * re * a % P;
	return re;
}
int R[maxn];
void NTT(int* a,int n,int f){
	for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
	for (int i = 1; i < n; i <<= 1){
		int gn = qpow(G,(P - 1) / (i << 1));
		for (int j = 0; j < n; j += (i << 1)){
			int g = 1,x,y;
			for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
				x = a[j + k],y = 1ll * a[j + k + i] * g % P;
				a[j + k] = (x + y) % P,a[j + k + i] = (P - y + x) % P;
			}
		}
	}
	if (f == 1) return;
	int nv = qpow(n,P - 2); reverse(a + 1,a + n);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int n,a,b,A[20][maxn],deg[20],cnt;
void solve(int l,int r){
	if (l == r){
		cnt++; A[cnt][0] = l; A[cnt][1] = 1; deg[cnt] = 1;
		return;
	}
	int mid = l + r >> 1;
	solve(l,mid); solve(mid + 1,r);
	int a = cnt - 1,b = cnt,n = 1,L = 0;
	while (n <= deg[a] + deg[b]) n <<= 1,L++;
	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
	for (int i = deg[a] + 1; i < n; i++) A[a][i] = 0;
	for (int i = deg[b] + 1; i < n; i++) A[b][i] = 0;
	NTT(A[a],n,1); NTT(A[b],n,1);
	for (int i = 0; i < n; i++) A[a][i] = 1ll * A[a][i] * A[b][i] % P;
	NTT(A[a],n,-1);
	cnt--;
	deg[a] += deg[b];
}
int C(int n,int m){
	if (n < m) return 0;
	int re = 1;
	for (int i = 1; i <= m; i++)
		re = 1ll * re * (n - i + 1) % P * qpow(i,P - 2) % P;
	return re;
}
int S[100][100];
int main(){
	n = read(); a = read(); b = read();
	if (a + b - 2 > n - 1 || !a || !b){puts("0"); return 0;}
	if (n == 1) A[1][0] = 1;
	else solve(0,n - 2);
	printf("%I64d\n",1ll * A[1][a + b - 2] * C(a + b - 2,a - 1) % P);
	return 0;
}

posted @ 2018-07-10 08:14  Mychael  阅读(408)  评论(0编辑  收藏  举报