# y2823774827y 的博客

### 浅谈斯特林数及斯特林反演

posted on 2019-04-14 09:52:25 | under 未分类 |

## 定义

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

## 性质

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

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

$\displaystyle x^{\underline{n+1}}=(x-n)x^{\underline n}$

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

$~~~~~~~~=\displaystyle 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$

$~~~~~~~~=\displaystyle \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$

$~~~~~~~~=\displaystyle \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$

$~~~~~~~~=\displaystyle \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}$

$~~~~~~~~=\displaystyle \sum\limits_{i=1}^{n+1} \begin{bmatrix}n+1\\i \end{bmatrix}(-1)^{n-i+1}x^{i}$

$~~~~~~~~=\displaystyle \sum\limits_{i=0}^{(n+1)} \begin{bmatrix}n+1\\i \end{bmatrix}(-1)^{(n+1)-i}x^{i}$

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

## 求第一类斯特林数

• $\displaystyle \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$$

$F(x+n)^{n}=\displaystyle \sum\limits_{i=0}^{n}a_i(x+n)^i$

$~~~~~~~~~~~~=\displaystyle \sum\limits_{i=0}^na_i\sum\limits_{j=0}^i{i\choose j}n^{i-j}x^j$

$~~~~~~~~~~~~=\displaystyle \sum\limits_{i=0}^n(\sum\limits_{j=i}^n {j\choose i}n^{j-i}a_j)x^i$

$~~~~~~~~~~~~=\displaystyle \sum\limits_{i=0}^n(\sum\limits_{j=i}^n \frac{j!}{i!(j-i)!}n^{j-i}a_j)x^i$

$~~~~~~~~~~~~=\displaystyle \sum\limits_{i=0}^n (i!)^{-1}x^i (\sum\limits_{j=i}^n (\frac{n^{j-i}}{(j-i)!})\cdot (j!a_j))$

## 定义

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

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

## 性质

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

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

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

## 转换到组合表示

$~~~~~~~~~~$反过来求第二类斯特林数，又得减掉这种情况：

$~~~~~~~~~~$选$k$个空盒子，然后小球放到其他的盒子里

$~~~~~~~~~~$但最后我们求出来的答案为有区别的盒子，转换过来要$×\frac{1}{m!}$

## 求第二类斯特林数

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

$~~~~~~~~~=\displaystyle \sum\limits_{k=0}^m\frac{(-1)^k(m-k)^n}{k!(m-k)!}$

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

$Sum(n)=\displaystyle \sum\limits_{i=0}^n i^k$

$~~~~~~~~~~~~~~=\displaystyle \sum\limits_{i=0}^n\sum\limits_{j=0}^k\begin{Bmatrix}k\\j \end{Bmatrix}i^{\underline j}$

$~~~~~~~~~~~~~~=\displaystyle \sum\limits_{j=0}^k\begin{Bmatrix}k\\j \end{Bmatrix}\sum\limits_{i=0}^n i^{\underline j}$

$~~~~~~~~~~~~~~=\displaystyle \sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix}j!\sum\limits_{i=0}^nC_i^j$

$~~~~~~~~~~~~~~=\displaystyle \sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix}j!C_{n+1}^{j+1}$

$~~~~~~~~~~~~~~=\displaystyle \sum\limits_{j=0}^k \begin{Bmatrix}k\\j \end{Bmatrix} \frac{(n+1)^{\underline {j+1}}}{j+1}$

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

• $x^{\underline n}=\displaystyle \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=\displaystyle \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}$$

## 前置

$\displaystyle \sum\limits_{k=m}^n (-1)^{n-k}\begin{bmatrix}n\\k\end{bmatrix} \begin{Bmatrix}k\\m\end{Bmatrix}=[m=n]$

$\displaystyle \sum\limits_{k=m}^n (-1)^{n-k}\begin{Bmatrix}n\\k\end{Bmatrix} \begin{bmatrix}k\\m\end{bmatrix}=[m=n]$

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

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

$~~~~~~=\displaystyle \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}$

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

$~~~~~~=\displaystyle \sum\limits_{i=0}^n\begin{Bmatrix}n\\i\end{Bmatrix}(-1)^i(-m)^{\overline i}$

$~~~~~~=\displaystyle \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$

$~~~~~~=\displaystyle \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}$

## 证明斯特林反演

$f(n)=\displaystyle \sum\limits_{k=0}^n [k=n]f(k)$

$~~~~~~~~=\displaystyle \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)$

$~~~~~~~~=\displaystyle \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)$

$~~~~~~~~=\displaystyle \sum\limits_{k=0}^n \begin {Bmatrix} n\\k \end{Bmatrix}g(k)$

CF960G

## Code

#include<bits/stdc++.h>
typedef int LL;
const LL mod=998244353,g=3,_g=332748118,maxn=2e5+9;
inline LL Pow(LL base,LL b){
LL ret(1);
while(b){
if(b&1) ret=1ll*ret*base%mod; base=1ll*base*base%mod; b>>=1;
}return ret;
}
LL r[maxn],W[maxn];
inline LL Fir(LL n){
LL limit(1),len(0),up(n<<1);
while(limit<up){
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(type?g:_g,(mod-1)/(mid<<1)));
W[0]=1; for(LL i=1;i<mid;++i) W[i]=1ll*W[i-1]*wn%mod;
for(LL R=mid<<1,j=0;j<n;j+=R)
for(LL k=0;k<mid;++k){
LL x(a[j+k]),y(1ll*W[k]*a[j+mid+k]%mod);
a[j+k]=1ll*(x+y)%mod; a[j+mid+k]=1ll*(x-y+mod)%mod;
}
}
}
LL T[maxn],F[maxn],H[maxn],fac[maxn],fav[maxn],tmp[maxn],sum[maxn],B[maxn];
inline LL 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]=1ll*a[i]*b[i]%mod;
NTT(ans,limit,0);
for(LL i=((n-1)<<1)+1;i<limit;++i) a[i]=b[i]=0;
return Pow(limit,mod-2);
}
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);
for(LL i=0;i<=len;++i){
F[i]=1ll*Pow(len,i)*fav[i]%mod;
H[i]=1ll*fac[i]*a[i]%mod;
}
std::reverse(H,H+len+1);

LL limit(Fir(len+1));
NTT(F,limit,1); NTT(H,limit,1);
for(LL i=0;i<limit;++i) F[i]=1ll*F[i]*H[i]%mod;
NTT(F,limit,0);
LL ty(Pow(limit,mod-2));
for(LL i=0;i<=len;++i) tmp[i]=1ll*F[len-i]*ty%mod*Pow(fac[i],mod-2)%mod;
for(LL i=(len<<1);i<=limit;++i) F[i]=H[i]=0;

LL val(Mul(len+1,a,tmp,B));
for(LL i=0;i<=(len<<1);++i) a[i]=1ll*B[i]*val%mod;

if(n&1)
for(LL i=n;i>=1;--i) a[i]=1ll*(a[i-1]+1ll*(n-1)*a[i]%mod)%mod;
}
LL n,a,b,m;
LL ans[maxn];
int main(){
scanf("%d%d%d",&n,&a,&b);
LL val;
val=fac[0]=fac[1]=1;
for(LL i=2;i<=n;++i) val=fac[i]=1ll*val*i%mod;
val=fav[n]=Pow(fac[n],mod-2);
for(LL i=n;i>=1;--i) val=fav[i-1]=1ll*val*i%mod;
Solve(n-1,ans);

n=a+b-2; m=a-1;
printf("%d\n",1ll*ans[n]*fac[n]%mod*fav[m]%mod*fav[n-m]%mod%mod);
}

【BZOJ4671】异或图

## 做法

$f_i$表示至少有$i$个联通块的方案，形如设立$i$个联通块轮廓，联通块内连边随意，联通块与联通块之间无连边

$g_i$表示恰好有$i$个联通块的方案，形如设立$i$个联通块轮廓，在保证内部联通的情况下，外部块与块间无连边

## Code

#include<bits/stdc++.h>
typedef int LL;
const LL maxn=109;
LL N,n;
LL G[maxn][maxn][maxn],a[maxn];
char s[maxn];
long long ans,p[maxn],S,fac[15];
void Dfs(LL x,LL up){
if(x==n+1){
memset(p,0,sizeof(p)); LL ele(0);
for(LL i=1;i<=N;++i){
S=0; LL tot(0);
for(LL j=1;j<=n;++j)
for(LL k=j+1;k<=n;++k)
if(a[k]!=a[j]){
S|=(1ll<<tot)*G[i][j][k];
++tot;
}
for(LL j=0;j<tot;++j){
if(S&(1ll<<j)){
if(!p[j]){
p[j]=S;
++ele;
break;
}else
S^=p[j];
}
}
}
ans+=1ll*((up&1)?1:-1)*fac[up-1]*(1ll<<N-ele);
return;
}
for(LL i=1;i<=up+1;++i){
a[x]=i;
Dfs(x+1,std::max(up,i));
}
}
int main(){
scanf("%d",&N);
for(LL i=1;i<=N;++i){
scanf(" %s",s+1);
LL len(strlen(s+1));
if(!n){
n=1;
for(;n*(n-1)/2!=len;++n);
}
LL now(0);
for(LL j=1;j<=n;++j) for(LL k=j+1;k<=n;++k) G[i][j][k]=s[++now]-'0';
}
fac[0]=fac[1]=1; for(LL i=2;i<=n;++i) fac[i]=fac[i-1]*i;
Dfs(1,0);
printf("%lld",ans);
return 0;
}

CF932E Team Work

## 题意

$\displaystyle \sum\limits_{i=1}^n C_n^ii^k (n≤10^9,k≤5000)$

## 做法

$\displaystyle \sum\limits_{i=1}^n C_n^ii^k$

$\displaystyle =\sum\limits_{i=1}^nC_n^i\sum\limits_{j=0}^iC_i^j\begin{Bmatrix}k\\j\end{Bmatrix}j!$

$=\displaystyle \sum\limits_{i=1}^n \frac{n!}{(n-i)!}\sum\limits_{j=0}^i\frac{\begin{Bmatrix}k\\j\end{Bmatrix}}{(i-j)!}$

$=\displaystyle \sum\limits_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\sum\limits_{i=j}^n\frac{n!}{(n-i)!}\frac{1}{(i-j)!}$

$=\displaystyle \displaystyle \sum\limits_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\sum\limits_{i=j}^n\frac{n!}{(n-j)!}\frac{(n-j)!}{(n-i)!(i-j)!}$

$=\displaystyle \sum\limits_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\frac{n!}{(n-j)!}\sum\limits_{i=j}^nC_{n-j}^{i-j}$

$=\displaystyle \sum\limits_{j=0}^{min(n,k)}\begin{Bmatrix}k\\j\end{Bmatrix}\frac{n!}{(n-j)!}2^{n-j}$