51nod 1829(函数)
题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1829
本题目相当于:
n个不同的小球,放入到m个可区分的盒子中,且盒子不能
够为空,问方案数?
根据第二类斯特林数.答案就是 \(m!S(n,m)\);
再进行变化得: \(\sum_{i=0}^m (-1)^i(m-i)^n C_{m}^{i}\).
\(C_n^m = \frac{n!}{(n-m)!* m!}\)
#include <stdio.h>
#include <iostream>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <string>
using namespace std;
typedef long long int LL;
const int maxn=1000005,MOD=1e9+7;
int n,m,a[maxn],ans;
LL fac[maxn],inv[maxn],fac_inv[maxn];
LL Pow(LL a,int b)
{
LL res=1;
for(; b; b>>=1,a=a*a%MOD) if(b&1) res=(res*a)%MOD;
return res;
}
LL C(int n,int m)
{
return fac[n]*Pow(fac[n-m]*fac[m]%MOD,MOD-2)%MOD;
}
void init()
{
fac[0]=1;
for(int i=1; i<=1000000; i++) fac[i]=fac[i-1]*i%MOD;
inv[1]=1;
for(int i=2; i<=1000000; i++) inv[i]=(LL)(MOD-MOD/i)*inv[MOD%i]%MOD;
}
int main()
{
init();
scanf("%d%d",&n,&m);
ans=0;
for(int i=0,e=1; i<=m; i++,e*=-1)
ans=(ans+e*Pow(m-i,n)*C(m,i)%MOD)%MOD;
printf("%d\n",(ans+MOD)%MOD);
return 0;
}