4555: [Tjoi2016&Heoi2016]求和
Time Limit: 40 Sec Memory Limit: 128 MBSubmit: 188 Solved: 153
[ Submit][ Status][ Discuss]
Description
在2016年,佳媛姐姐刚刚学习了第二类斯特林数,非常开心。
现在他想计算这样一个函数的值:
S(i, j)表示第二类斯特林数,递推公式为:
S(i, j) = j ∗ S(i − 1, j) + S(i − 1, j − 1), 1 <= j <= i − 1。
边界条件为:S(i, i) = 1(0 <= i), S(i, 0) = 0(1 <= i)
你能帮帮他吗?
Input
输入只有一个正整数
Output
输出f(n)。由于结果会很大,输出f(n)对998244353(7 × 17 × 223 + 1)取模的结果即可。1 ≤ n ≤ 100000
Sample Input
3
Sample Output
87
HINT
Source
明确一下题目中的第二类斯特林数,,然后就好做了
一开始以为998244353原根为2。。。搞什么,,调了好久= =
两边的式子都有Gn,可以用类似CDQ分治的形式来搞
最后,,注意下数组下标,,尴尬了。
#include<iostream>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<bitset>
using namespace std;
typedef long long LL;
const LL P = 998244353;
const int maxn = (1<<18);
const LL g = 3;
LL Ans,A[maxn],B[maxn],C[maxn],G[maxn],w[maxn],_w[maxn],fac[maxn],inv[maxn];
int n,N = 1;
LL Mul(const LL &x,const LL &y) {return x*y%P;}
LL Add(const LL &x,const LL &y) {return (x + y)%P;}
LL Dec(const LL &x,const LL &y) {return (x - y + P)%P;}
LL ksm(LL x,int y)
{
LL ret = 1;
for (; y; y >>= 1)
{
if (y&1) ret = Mul(ret,x);
x = Mul(x,x);
}
return ret;
}
void Rader(LL *F,int M)
{
int j = (M>>1);
for (int i = 1; i < M - 1; i++)
{
if (i < j) swap(F[i],F[j]);
int k = (M>>1);
while (j >= k) j ^= k,k >>= 1;
j |= k;
}
}
void NTT(LL *F,int M,LL *w,int on)
{
Rader(F,M);
for (int h = 2; h <= M; h <<= 1)
for (int k = 0; k < M; k += h)
{
int t = 0;
for (int i = k; i < k + (h>>1); i++)
{
int j = i + (h>>1);
LL u = F[i],v = F[j];
v = Mul(v,w[t]); t += N / h;
F[i] = Add(u,v); F[j] = Dec(u,v);
}
}
if (on == -1)
{
LL Inv = ksm(M,P-2);
for (int i = 0; i < M; i++)
F[i] = Mul(F[i],Inv);
}
}
void Solve(int l,int r)
{
if (l == r) return;
int mid = (l + r) >> 1,M = r - l + 1;
Solve(l,mid);
for (int i = 0; i < M; i++) A[i] = B[i] = C[i] = 0;
for (int i = l; i <= mid; i++) B[i-l] = G[i];
for (int i = 1; i <= M; i++)
A[i-1] = Mul(2,inv[i]);
NTT(A,M,w,1);
NTT(B,M,w,1);
for (int i = l; i <= r; i++)
C[i-l] = Mul(A[i-l],B[i-l]);
NTT(C,M,_w,-1);
for (int i = mid + 1; i <= r; i++)
G[i] = Add(G[i],C[i-l-1]);
Solve(mid+1,r);
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n; while (N < n) N <<= 1; N <<= 1;
w[0] = w[N] = 1; w[1] = ksm(g,(P-1)/N);
for (int i = 2; i < N; i++) w[i] = Mul(w[1],w[i-1]);
for (int i = 0; i <= N; i++) _w[N-i] = w[i];
fac[0] = 1; for (int i = 1; i < N; i++) fac[i] = Mul(i,fac[i-1]);
inv[N-1] = ksm(fac[N-1],P-2);
for (int i = N-2; i >= 0; i--) inv[i] = Mul(i+1,inv[i+1]);
G[0] = 1; Solve(0,N-1);
for (int i = 0; i <= n; i++)
Ans = Add(Ans,Mul(G[i],fac[i]));
cout << Ans;
return 0;
}