# 斯特林数及斯特林反演

## 定义

$\begin{bmatrix}n\\m \end{bmatrix}$表示$n$个元素分成$m$个环的方案数

$\begin{bmatrix}n\\m \end{bmatrix}=\begin{bmatrix}n-1\\m-1 \end{bmatrix}+(n-1)*\begin{bmatrix}n-1\\m \end{bmatrix}$

$~~~~~~~~~~$空一个环则单独成环，如果$n-1$的时候就没有空环就任意放在一个元素前

## 性质

• $n!=\sum\limits_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}$

• $x^{\underline n}=\sum\limits_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i}x^i\\$

\begin{aligned}\\ x^{\underline {n+1}}&=(x-n)x^{\underline n}\\ &=(x-n)\sum\limits_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i}x^i\\ &=x\sum\limits_{i=0}^{n} \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i}x^{i}-n\sum\limits_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i} x^i\\ &=\sum\limits_{i=0}^{n} \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i}x^{i+1}-n\sum\limits_{i=0}^{n+1} \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i} x^i\\ &=\sum\limits_{i=1}^{n+1} \begin{bmatrix}n\\i-1 \end{bmatrix}(-1)^{n-i+1}x^{i}+n\sum\limits_{i=0}^{n+1} \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i+1} x^i\\ &=\sum\limits_{i=1}^{n+1} ( \begin{bmatrix}n\\i-1 \end{bmatrix} +n*\begin{bmatrix}n\\i \end{bmatrix})(-1)^{n-i+1}x^{i}\\ &=\sum\limits_{i=1}^{n+1} \begin{bmatrix}n+1\\i \end{bmatrix}(-1)^{n-i+1}x^{i}\\ &=\sum\limits_{i=0}^{(n+1)} \begin{bmatrix}n+1\\i \end{bmatrix}(-1)^{(n+1)-i}x^{i}\\ \end{aligned}

• $x^{\overline n}=\sum_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}x^i$

## 求第一类斯特林数

• $\sum_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}x^i=\prod_{i=0}^{n-1}(x+i)$

$\begin{array}{c c c}~&0&1&2&3&4\\0&0&0&0&0&0\\1&0&1&0&0&0\\2&0&1&1&0&0\\3&0&2&3&1&0\\4&0&6&11&6&1\end{array}$

• 还有一种类似于多项式求逆模式$O(nlogn)$的方法

$F(x)^n=\prod\limits_{i=0}^{n-1}(x+i),F(x)^{2n}=F(x)^nF(x+n)^n$

\begin{aligned}\\ F(x+n)^{n}&=\sum\limits_{i=0}^{n}a_i(x+n)^i\\ &=\sum\limits_{i=0}^na_i\sum\limits_{j=0}^i{i\choose j}n^{i-j}x^j\\ &=\sum\limits_{i=0}^n(\sum\limits_{j=i}^n {j\choose i}n^{j-i}a_j)x^i\\ &=\sum\limits_{i=0}^n(\sum\limits_{j=i}^n \frac{j!}{i!(j-i)!}n^{j-i}a_j)x^i\\ &=\sum\limits_{i=0}^n (i!)^{-1}x^i (\sum\limits_{j=i}^n (\frac{n^{j-i}}{(j-i)!})\cdot (j!a_j))\\ \end{aligned}

## Code

#include<bits/stdc++.h>
typedef long long LL;
const LL mod=998244353,g=3,maxn=1e6+9;
inline LL Pow(LL base,LL b){
LL ret(1);
while(b){
if(b&1) ret=ret*base%mod; base=base*base%mod; b>>=1;
}return ret;
}
LL r[maxn];
inline LL Fir(LL n){
LL limit(1),len(0);
while(limit<(n<<1)){
limit<<=1; ++len;
}
for(LL i=0;i<limit;++i) r[i]=(r[i>>1]>>1)|((i&1)<<len-1);
return limit;
}
inline void NTT(LL *a,LL n,LL type){
for(LL i=0;i<n;++i) if(i<r[i]) std::swap(a[i],a[r[i]]);
for(LL mid=1;mid<n;mid<<=1){
LL wn(Pow(g,(mod-1)/(mid<<1))); if(type==-1) wn=Pow(wn,mod-2);
for(LL R=mid<<1,j=0;j<n;j+=R)
for(LL k=0,w=1;k<mid;++k,w=w*wn%mod){
LL x(a[j+k]),y(w*a[j+mid+k]%mod);
a[j+k]=(x+y)%mod; a[j+mid+k]=(x-y+mod)%mod;
}
}
if(type==-1){
LL ty(Pow(n,mod-2)); for(LL i=0;i<n;++i) a[i]=a[i]*ty%mod;
}
}
LL T[maxn],F[maxn],H[maxn],G[maxn],fac[maxn],fav[maxn],tmp[maxn],sum[maxn],B[maxn];
inline void Mul(LL n,LL *a,LL *b,LL *ans){
LL limit(Fir(n));
NTT(a,limit,1); NTT(b,limit,1);
for(LL i=0;i<limit;++i) ans[i]=a[i]*b[i]%mod;
NTT(ans,limit,-1);
}
inline void Solve(LL n,LL *a){
if(!n){ a[0]=1; return; }
if(n==1){ a[1]=1; return; }
LL len(n/2);
Solve(len,a);

LL limit(Fir(len+1));
for(LL i=0;i<=len;++i) F[i]=Pow(len,i)*fav[i]%mod;
for(LL i=0;i<=len;++i) H[i]=fac[i]*a[i]%mod;
for(LL i=0;i<=(len>>1);++i) std::swap(H[i],H[len-i]);
for(LL i=len+1;i<limit;++i) F[i]=H[i]=0;
NTT(F,limit,1); NTT(H,limit,1);
for(LL i=0;i<limit;++i) G[i]=F[i]*H[i]%mod;
NTT(G,limit,-1);
for(LL i=0;i<=len;++i) tmp[i]=G[len-i]*Pow(fac[i],mod-2)%mod;//right

Mul(len+1,a,tmp,B);//left * right
for(LL i=0;i<=(len<<1);++i) a[i]=B[i];
for(LL i=(len<<1)+1;i<=(len<<2);++i) a[i]=tmp[i]=0;

if(n&1){
for(LL i=0;i<n;++i) T[i]=a[i];
for(LL i=1;i<=n;++i) a[i]=(T[i-1]+(n-1)*T[i]%mod)%mod;
}
}
inline LL Get_c(LL n,LL m){
return fac[n]*fav[m]%mod*fav[n-m]%mod;
}
LL n;
LL ans[maxn];
int main(){
scanf("%lld",&n);
fac[0]=fac[1]=1;
for(LL i=2;i<=n;++i) fac[i]=fac[i-1]*i%mod;
fav[n]=Pow(fac[n],mod-2);
for(LL i=n;i>=1;--i) fav[i-1]=fav[i]*i%mod;
Solve(n,ans);
for(LL i=0;i<=n;++i) printf("%lld ",ans[i]);printf("\n");
return 0;
}


## 定义

$\begin{Bmatrix}n\\m\end{Bmatrix}$表示$n$个有区别的小球丢进$m$个无区别的盒子，无空盒子的方案数

$\begin{Bmatrix}n\\m\end{Bmatrix}=\begin{Bmatrix}n-1\\m-1\end{Bmatrix}+m*\begin{Bmatrix}n-1\\m\end{Bmatrix}$

$~~~~~~~~~~$空一个盒子则只能放到那个空盒子里面了，如果$n-1$的时候就没有空箱子就随便放

## 性质

$m^n=\sum_{i=0}^{m}\begin{Bmatrix}n\\i\end{Bmatrix}*i!*C(m,i)$

$m^n=\sum\limits_{i=0}^m \begin{Bmatrix}n\\i\end{Bmatrix}*m^{\underline i}$

$m^n=\sum\limits_{i=0}^n \begin{Bmatrix}n\\i\end{Bmatrix}*m^{\underline i}$

$~~~~~~~~~~$枚举有效盒子的个数，再从$m$个盒子选$i$个盒子，然后$n$个小球丢进$i$个盒子

## 转换到组合表示

$\begin{Bmatrix}n\\m\end{Bmatrix}=\frac{1}{m!}\sum\limits_{k=0}^m(-1)^kC(m,k)(m-k)^n$

$~~~~~~~~~~$反过来求第二类斯特林数，又得减掉这种情况：
$~~~~~~~~~~$$k$个空盒子，然后小球放到其他的盒子里
$~~~~~~~~~~$但最后我们求出来的答案为有区别的盒子，转换过来要$×\frac{1}{m!}$

## 求第二类斯特林数

\begin{aligned}\\ \begin{Bmatrix}n\\m\end{Bmatrix}&=\frac{1}{m!}\sum\limits_{k=0}^m(-1)^k\frac{m!}{k!(m-k)!}(m-k)^n\\ &=\sum\limits_{k=0}^m\frac{(-1)^k(m-k)^n}{k!(m-k)!}\\ \end{aligned}

## 第二类斯特林数与自然数幂的关系

\begin{aligned}\\ Sum(n)&=\sum\limits_{i=0}^n i^k\\ &=\sum\limits_{i=0}^n\sum\limits_{j=0}^k\begin{Bmatrix}k\\j \end{Bmatrix}i^{\underline j}\\ &=\sum\limits_{j=0}^k\begin{Bmatrix}k\\j \end{Bmatrix}\sum\limits_{i=0}^n i^{\underline j}\\ &=\sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix}j!\sum\limits_{i=0}^nC_i^j\\ &=\sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix}j!C_{n+1}^{j+1}\\ &=\sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix} \frac{(n+1)^{\underline {j+1}}}{j+1}\\ \end{aligned}

## 总结上面我们所推倒的性质

• $x^{\underline n}=\sum\limits_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}(-1)^{n-i}x^i,x^{\overline n}=\sum_{i=0}^n \begin{bmatrix}n\\i \end{bmatrix}x^i$
• $m^n=\sum\limits_{i=0}^n \begin{Bmatrix}n\\i\end{Bmatrix}*m^{\underline i}$

## 补充

$x^{\underline n}=(-1)^n (-x)^{\overline n},x^{\overline n}=(-1)^n (-x)^{\underline n}$

## 前置

\begin{aligned}\displaystyle \sum_{k=m}^n (-1)^{n-k}\begin{bmatrix}n\\k\end{bmatrix} \begin{Bmatrix}k\\m\end{Bmatrix}=[m=n]\\ \sum_{k=m}^n (-1)^{n-k}\begin{Bmatrix}n\\k\end{Bmatrix} \begin{bmatrix}k\\m\end{bmatrix}=[m=n]\end{aligned}

\begin{aligned}\\ m^{\underline n}&=\sum\limits_{i=0}^n \begin{bmatrix}n\\i\end{bmatrix}(-1)^{n-i}m^i\\ &=\sum\limits_{i=0}^n \begin{bmatrix}n\\i\end{bmatrix}(-1)^{n-i}\sum\limits_{j=0}^i \begin{Bmatrix}i\\j\end{Bmatrix}m^{\underline j}\\ &=\sum\limits_{i=0}^n m^{\underline i}\sum\limits_{j=i}^n (-1)^{n-j} \begin{bmatrix}n\\j\end{bmatrix} \begin{Bmatrix}j\\i\end{Bmatrix}\\ \end{aligned}

\begin{aligned}\\ m^n&=\sum\limits_{i=0}^n\begin{Bmatrix}n\\i\end{Bmatrix}m^{\underline i}\\ &=\sum\limits_{i=0}^n\begin{Bmatrix}n\\i\end{Bmatrix}(-1)^i(-m)^{\overline i}\\ &=\sum\limits_{i=0}^n\begin{Bmatrix}n\\i\end{Bmatrix}(-1)^i\sum\limits_{j=0}^i \begin{bmatrix}i\\j\end{bmatrix}(-m)^j\\ &=\sum\limits_{i=0}^n m^i\sum\limits_{j=i}^n(-1)^{i-j} \begin{Bmatrix}n\\j\end{Bmatrix}\begin{bmatrix}j\\i\end{bmatrix}\\ \end{aligned}

## 推式

\begin{aligned}\\ f(n)&=\sum\limits_{k=0}^n [k=n]f(k)\\ &=\sum\limits_{k=0}^n\sum\limits_{j=k}^n \begin {Bmatrix} n\\j \end{Bmatrix}\begin {bmatrix} j\\k \end{bmatrix}(-1)^{j-k}f(k)\\ &=\sum\limits_{k=0}^n \begin {Bmatrix} n\\k \end{Bmatrix}\sum\limits_{j=0}^k (-1)^{k-j}\begin {bmatrix} k\\j \end{bmatrix}f(j)\\ &=\sum\limits_{k=0}^n \begin {Bmatrix} n\\k \end{Bmatrix}g(k)\\ \end{aligned}

## ${\large\color{SpringGreen}{斯特林数应用}}$

posted @ 2019-04-14 09:45  y2823774827y  阅读(7902)  评论(8编辑  收藏  举报