通过伯努利数求自然数幂和。第一次接触伯努利数,打表套模板。。
参考:http://blog.csdn.net/acdreamers/article/details/38929067
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MOD = 1000000007;
const int MAXN = 2010;
LL Inv[MAXN];
LL C[MAXN][MAXN];
LL B[MAXN];
LL Fac[MAXN];
void init()
{
Inv[1] = 1;
for(int i = 2; i < MAXN; ++i)
Inv[i] = (MOD - MOD/i)*Inv[MOD%i]%MOD;
C[0][0] = 1;
for(int i = 1; i < MAXN; ++i)
{
C[i][0] = C[i][i] = 1;
for(int j = 1; j < i; ++j)
C[i][j] = (C[i-1][j]%MOD + C[i-1][j-1]%MOD)%MOD;
}
B[0] = 1;
for(int i = 1; i < MAXN; ++i)
{
LL ans = 0;
for(int j = 0; j < i; ++j)
ans = (ans%MOD + C[i+1][j]*B[j]%MOD)%MOD;
B[i] = ((-1*ans*Inv[i+1])%MOD + MOD)%MOD;
}
}
LL solve(LL k)
{
LL ans = Inv[k+1];
LL sum = 0;
for(int i = 1; i <= k+1; ++i)
{
sum += C[k+1][i]*Fac[i]%MOD * B[k+1-i]%MOD;
sum %= MOD;
}
ans *= sum;
ans %= MOD;
return ans;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int T;
init();
cin >> T;
LL N,K;
while(T--)
{
LL res = 0;
cin >> N >> K;
N %= MOD;
Fac[0] = 1;
for(int i = 1; i < MAXN; ++i)
Fac[i] = Fac[i-1]*(N+1)%MOD;
cout << solve(K) << endl;
}
return 0;
}