Description
Calculate:
Prefix Knowledge
Sterling numbers the second kind
Sterling numbers the second kind, which is written in
, is defined as the number of ways to put different balls into same boxes.It can be calculated as below:
Solution
Use Stirling numbers the second kind to change the form of
We have:
Proof:
Let's consider a problem: What's the number of ways to put
different balls into different boxes. Obviously the answer is
. In another way, we may index
that represents the number of boxes that have balls in, then the ways in this case is . And because the boxes are different , we should multiply Thus, the answer is also
So now we have
Then we have:
Then change the form of
Notice that this is the answer to the problem:
What's the number of ways to choose
objects(same) from , then (different) from ?
We may first index
Thus we have $\sum {x\in[i,N]}C {N}^x\cdot C x^i\cdot i!=A {N}^i\cdot 2^{N-i}$
After,
We may notice that when
Thus, the problem can be solved in
Code
const int N = 5005;
const int MOD = 1e9+7;
int s[N][N];
int n, k;
int power(int base, int exp) {
if (exp < 0) return 0;
int res=1;
while (exp) {
if (exp&1) (res*=base)%=MOD;
(base*=base) %= MOD; exp >>=1; }
return res; }
int inv(int u) { return power(u,MOD-2); }
int nAr(int n, int r) {
int res=1;
rep (i,r) (res*=(n-i)) %=MOD;
return res; }
void initStirling() {
s[1][1] = 1;
repi (i,2,N-1) repa (j,i) s[i][j] = (s[i-1][j-1] + s[i-1][j]*j) % MOD; }
void init() {
scanf(II,&n,&k);
initStirling();
}
void solve() {
int ans=0;
rep (j,k+1) {
(ans+= nAr(n,j) * power(2,n-j) % MOD * s[k][j] % MOD) %= MOD; }
printf(IN,ans);
}