【TopCoder SRM 686】—CyclesNumber(斯特林数)

传送门


一个置换可以看做若干个循环拼起来

ans=i=1nsn,iimans=\sum_{i=1}^{n}s_{n,i}i^m

暴力推一波式子
ans=i=1nsn,ij=0mSm,j(ij)j!ans=\sum_{i=1}^{n}s_{n,i}\sum_{j=0}^{m}S_{m,j}{i\choose j}j!

=j=0mSm,jj!i=1nsn,i(ij)=\sum_{j=0}^{m}S_{m,j}j!\sum_{i=1}^{n}s_{n,i}{i\choose j}

又有:

i=1nsn,i(ij)=sn+1,j+1\sum_{i=1}^ns_{n,i}{i\choose j}=s_{n+1,j+1}

证明:

考虑组合意义
先把nn个数分成ii个环
从里面先挑去jj个环
将剩下的环和n+1n+1从小到大依次串起来可以得到唯一的一个新环
也就是把n+1n+1个数分成j+1j+1个环

好像也可以生成函数暴力证

然后
ans=j=0mSm,jj!sn+1,j+1ans=\sum_{j=0}^mS_{m,j}j!s_{n+1,j+1}

O(m2+nm)O(m^2+nm)预处理斯特林数就可以做到O(m)O(m)回答每个询问

#include<bits/stdc++.h>
using namespace std;
const int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ob==ib)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ob==ib)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0,f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
#define ll long long
#define re register
#define pii pair<int,int>
#define fi first
#define se second
#define pb push_back
#define pob pop_back
#define cs const
#define poly vector<int>
cs int mod=1e9+7;
inline int add(int a,int b){return a+b>=mod?a+b-mod:a+b;}
inline void Add(int &a,int b){a=add(a,b);}
inline int dec(int a,int b){return a>=b?a-b:a-b+mod;}
inline void Dec(int &a,int b){a=dec(a,b);}
inline int mul(int a,int b){return 1ll*a*b>=mod?1ll*a*b%mod:a*b;}
inline void Mul(int &a,int b){a=mul(a,b);}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,a=mul(a,a))(b&1)?(res=mul(res,a)):0;return res;}
inline void chemx(int &a,int b){a<b?a=b:0;}
inline void chemn(int &a,int b){a>b?a=b:0;}
cs int N=100005,M=305;
int n,m;
int S[M][M],s[N][M],fac[N];
inline void init(){
	fac[0]=1;
	for(int i=1;i<M;i++)fac[i]=mul(fac[i-1],i);
	S[0][0]=s[0][0]=1;
	for(int i=1;i<M;i++)
	for(int j=1;j<M;j++)
	S[i][j]=add(S[i-1][j-1],mul(S[i-1][j],j));
	for(int i=1;i<N;i++)
	for(int j=1;j<M;j++)
	s[i][j]=add(s[i-1][j-1],mul(s[i-1][j],i-1));
}
inline int solve(int n,int m){
	int res=0;
	for(int i=0;i<=m;i++)Add(res,mul(mul(S[m][i],fac[i]),s[n+1][i+1]));
	return res;
}
class CyclesNumber{
	public :
		inline poly getExpectation(poly n,poly m){
			init();poly res;
			for(int i=0;i<n.size();i++)
				res.pb(solve(n[i],m[i]));
			return res;
		}
};

posted @ 2019-08-21 18:58  Stargazer_cykoi  阅读(153)  评论(0编辑  收藏  举报