标签:type i++ ble text fas lang stdout har 处理
有一个非常玄学的公式:
$ m^n=\sum_{i=0}^nC_m^i\times S(n,i)\times i! $
看一下$\text{yyb}$的解释:
$m^n$理解为把$n$个不同的球放到$m$个不同的盒子中去。
那么我们枚举有几个盒子非空,用第二类斯特林数乘阶乘计算放置的方案数,最后求和就是结果。
所以原式等于:
$$ \sum_{i=1}^nC_n^i\sum_{j=0}^iC_i^j\times S(k,j)\times j! \\ =\sum_{i=1}^n\frac{n!}{i!(n-i)!}\sum_{j=0}^i\frac{S(k,j)}{(i-j)!} \\ =\sum_{j=0}^kS(k,j)\sum_{i=j}^n\frac{n!}{(n-i)!}*\frac{1}{(i-j)!} \\ =\sum_{j=0}^{k}S(k,j)\sum_{i=0}^n\frac{n!}{(n-j)!}\frac{(n-j)!}{(n-i)!(i-j)!} \\ =\sum_{j=0}^{k}S(k,j)\frac{n!}{(n-j)!}\sum_{i=0}^nC_{n-j}^{i-j} \\ =\sum_{j=0}^{k}S(k,j)\frac{n!}{(n-j)!}2^{n-j} $$
暴力$\text{O}(k^2)$预处理即可
#include<cstdio>
#include<cstring>
#include<cctype>
#include<algorithm>
#define RG register
#define file(x) freopen(#x".in", "r", stdin);freopen(#x".out", "w", stdout);
#define clear(x, y) memset(x, y, sizeof(x))
inline int read()
{
int data = 0, w = 1; char ch = getchar();
while(ch != ‘-‘ && (!isdigit(ch))) ch = getchar();
if(ch == ‘-‘) w = -1, ch = getchar();
while(isdigit(ch)) data = data * 10 + (ch ^ 48), ch = getchar();
return data * w;
}
const int Mod(1e9 + 7), Inv2(500000004), maxn(5010);
inline int fastpow(int x, int y)
{
int ans = 1;
while(y)
{
if(y & 1) ans = 1ll * ans * x % Mod;
x = 1ll * x * x % Mod, y >>= 1;
}
return ans;
}
int n, k, S[maxn][maxn], ans;
int main()
{
#ifndef ONLINE_JUDGE
file(cpp);
#endif
n = read(), k = read(), S[0][0] = 1;
for(RG int i = 1; i <= k; i++)
for(RG int j = 1; j <= k; j++)
S[i][j] = (S[i - 1][j - 1] + 1ll * S[i - 1][j] * j % Mod) % Mod;
for(RG int j = 0, pow2 = fastpow(2, n), fac = 1; j <= std::min(n, k);
pow2 = 1ll * pow2 * Inv2 % Mod, fac = 1ll * fac * (n - j) % Mod, ++j)
ans = (ans + 1ll * S[k][j] * pow2 % Mod * fac % Mod) % Mod;
printf("%d\n", ans);
return 0;
}
标签:type i++ ble text fas lang stdout har 处理
原文地址:https://www.cnblogs.com/cj-xxz/p/10191857.html