Description
求
\[\sum_{i=1}^n C(n,i)\times i^k\]
\(1\leq n\leq 10^9, 1\leq k\leq 5000\)
Solution
[BZOJ 5093]图的价值的弱化版。
看公式推导可以戳链接。
Code
#include <bits/stdc++.h>
using namespace std;
const int N = 5000+5, yzh = 1e9+7;
int S[N][N], n, k, inv[N];
int quick_pow(int a, int b) {
int ans = 1;
while (b) {
if (b&1) ans = 1ll*ans*a%yzh;
b >>= 1, a = 1ll*a*a%yzh;
}
return ans;
}
void work() {
scanf("%d%d", &n, &k);
inv[0] = inv[1] = S[0][0] = 1;
for (int i = 2; i <= k; i++) inv[i] = -1ll*yzh/i*inv[yzh%i]%yzh;
for (int i = 1; i <= k; i++)
for (int j = 1; j <= i; j++)
S[i][j] = (1ll*S[i-1][j-1]+1ll*S[i-1][j]*j%yzh)%yzh;
int ans = 0, C = n, fac = 1;
for (int i = 1; i <= min(k, n); i++) {
(ans += 1ll*fac*C%yzh*S[k][i]%yzh*quick_pow(2, n-i)%yzh) %= yzh;
fac = 1ll*fac*(i+1)%yzh, C = 1ll*C*(n-i)%yzh*inv[i+1]%yzh;
}
printf("%d\n", (ans+yzh)%yzh);
}
int main() {work(); return 0; }