S(n,m):把n个球放入m个无区别的盒子里的方案数
通解:(图片在某位佬的博客偷的)
O(n)输出某个S(n,m)的代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 1e6+100;
const int inf = 0x3f3f3f3f;
const ll INF = 2e18;
const int mod = 1e9+7;
const int MOD = 998244353;
/*
第二类斯特林数,把n个球放入m个无区别的盒子里
*/
ll fac[N],infac[N];
int len;
ll ksm(int x,int y){
ll res = 1;
while(y){
if(y & 1)res = res * x % mod;
x = x * x % mod;
y >>= 1;
}
return res;
}
void init(){
fac[0] = 1;
for(int i=1;i<=len;i++)
fac[i] = fac[i-1] * i % mod;\
infac[len] = ksm(fac[len], mod-2);
for(int i=len-1;i>=0;i--){
infac[i] = infac[i+1] * (i+1) % mod;
}
}
ll C(int n,int m){
if(m < 0 || m > n)return 0;
return fac[n] * infac[m] % mod * infac[n-m] % mod;
}
ll stl(int n,int m){
ll res = 0;
for(int k=0;k<=m;k++){
ll t = C(m,k) * ksm(m-k, n) % mod;
if(k & 1)res = (res - t + mod) % mod;
else res = (res + t) % mod;
}
return res * infac[m] % mod;
}
void solve(){
int n,m;
cin>>n>>m;
cout<<stl(n,m)<<endl;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);cout.tie(nullptr);
int t=1;
//cin>>t;
while(t--)solve();
return 0;
}
它的格式很像卷积,可以利用FFT,实现O(nlogn)求出某一行,现在还太菜了遇不到,画个饼,等大二暑假再学 。