# 题解

$$f[i] = (i-1) \cdot f[i-1] + \sum_{j=2}^{i-2} (j-1) \cdot f[j] \cdot f[i-j]$$

1.  $j\in [2,R-L],i-j\in[L,mid]$

2.  $i-j\in [2,R-L], j\in[L,mid]$

# 代码

#include <bits/stdc++.h>
#define clr(x) memset(x,0,sizeof x)
#define For(i,a,b) for (int i=a;i<=b;i++)
#define Fod(i,b,a) for (int i=b;i>=a;i--)
#define fi first
#define se second
#define pb(x) push_back(x)
#define mp(x,y) make_pair(x,y)
#define outval(x) printf(#x" = %d\n",x)
#define outtag(x) puts("---------------"#x"---------------")
#define outarr(a,L,R) printf(#a"[%d..%d] = ",L,R);\
For(_x,L,R)printf("%d ",a[_x]);puts("")
using namespace std;
typedef long long LL;
LL x=0,f=0;
char ch=getchar();
while (!isdigit(ch))
f|=ch=='-',ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
return f?-x:x;
}
const int N=1<<17,mod=998244353;
int Pow(int x,int y){
int ans=1;
for (;y;y>>=1,x=(LL)x*x%mod)
if (y&1)
ans=(LL)ans*x%mod;
return ans;
}
if ((x+=y)>=mod)
x-=mod;
}
void Del(int &x,int y){
if ((x-=y)<0)
x+=mod;
}
int Del(int x){
return x<0?x+mod:x;
}
namespace fft{
int w[N],R[N];
void init(int n){
int d=0;
while ((1<<d)<n)
d++;
For(i,0,n-1)
R[i]=(R[i>>1]>>1)|((i&1)<<(d-1));
w[0]=1,w[1]=Pow(3,(mod-1)/n);
For(i,2,n-1)
w[i]=(LL)w[i-1]*w[1]%mod;
}
void FFT(int *a,int n,int flag){
if (flag<0)
reverse(w+1,w+n);
For(i,0,n-1)
if (i<R[i])
swap(a[i],a[R[i]]);
for (int t=n>>1,d=1;d<n;d<<=1,t>>=1)
for (int i=0;i<n;i+=d<<1)
for (int j=0;j<d;j++){
int tmp=(LL)w[t*j]*a[i+j+d]%mod;
a[i+j+d]=Del(a[i+j]-tmp);
}
if (flag<0){
reverse(w+1,w+n);
int inv=Pow(n,mod-2);
For(i,0,n-1)
a[i]=(LL)a[i]*inv%mod;
}
}
}
using fft::FFT;
namespace pre{
int f[N];
int a[N],b[N];
void solve(int L,int R){
if (L==R){
f[L]=((LL)f[L-1]*(L-1)+f[L])%mod;
return;
}
int mid=(L+R)>>1;
solve(L,mid);
if (L>1){
int n=1;
while (n<(mid-L+1)+(R-L))
n<<=1;
fft::init(n);
memset(a,0,n<<2),memset(b,0,n<<2);
For(i,L,mid)
a[i-L]=f[i];
For(i,2,R-L)
b[i]=(LL)(i-1)*f[i]%mod;
FFT(a,n,1),FFT(b,n,1);
For(i,0,n-1)
a[i]=(LL)a[i]*b[i]%mod;
FFT(a,n,-1);
For(i,mid+1,R)
memset(a,0,n<<2),memset(b,0,n<<2);
For(i,L,mid)
a[i-L]=(LL)(i-1)*f[i]%mod;
For(i,2,R-L)
b[i]=f[i];
FFT(a,n,1),FFT(b,n,1);
For(i,0,n-1)
a[i]=(LL)a[i]*b[i]%mod;
FFT(a,n,-1);
For(i,mid+1,R)
}
else {
int n=1;
while (n<mid*2+1)
n<<=1;
memset(a,0,n<<2),memset(b,0,n<<2);
For(i,2,mid)
a[i]=f[i];
For(i,2,mid)
b[i]=(LL)(i-1)*f[i]%mod;
fft::init(n);
FFT(a,n,1),FFT(b,n,1);
For(i,0,n-1)
a[i]=(LL)a[i]*b[i]%mod;
FFT(a,n,-1);
For(i,mid+1,R)
}
solve(mid+1,R);
}
void prework(int n){
f[0]=1,f[1]=2;
solve(1,n);
//		For(i,2,n){
//			f[i]=(LL)f[i-1]*(i-1)%mod;
//			For(j,2,i-2)
//		}
//1 2 2 4 16 88 600 4800 43680 443296 4949920 60217408 792134528
}
}
int T,n;
int a[N];
void Solve(){
For(i,1,n)
if (a[n]!=n)
return (void)(puts("0"));
int ans=1;
For(i,1,n){
int c=0,j;
for (j=i-1;j>i-a[i];j-=a[j])
c++;
if (j<i-a[i])
return (void)(puts("0"));
ans=(LL)ans*pre::f[c]%mod;
}
printf("%d\n",ans);
}
int main(){
pre::prework(50000);
while (T--)
Solve();
return 0;
}


posted @ 2019-05-07 09:09 -zhouzhendong- 阅读(...) 评论(...) 编辑 收藏

CLY AK IOI