题面
题解
\(O(n^2)\)预处理伯努利数
不知道伯努利数是什么的可以看看这篇文章
不过这个数据范围拉格朗日差值应该也没问题……吧……大概……
//minamoto
#include<bits/stdc++.h>
#define R register
#define ll long long
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
ll read(){
R ll res,f=1;R char ch;
while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
return res*f;
}
char sr[1<<21],z[20];int K=-1,Z=0;
inline void Ot(){fwrite(sr,1,K+1,stdout),K=-1;}
void print(R int x){
if(K>1<<20)Ot();if(x<0)sr[++K]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++K]=z[Z],--Z);sr[++K]='\n';
}
const int N=2005,P=1e9+7;
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
R int res=1;
for(;y;y>>=1,x=mul(x,x))if(y&1)res=mul(res,x);
return res;
}
int fac[N],ifac[N],inv[N],B[N];
inline int C(R int n,R int m){return m>n?0:1ll*fac[n]*ifac[m]%P*ifac[n-m]%P;}
ll n;int k,T,nw,q,res;
void init(int n){
B[0]=fac[0]=fac[1]=ifac[0]=ifac[1]=inv[0]=inv[1]=1;
fp(i,2,n){
fac[i]=mul(fac[i-1],i),
inv[i]=1ll*(P-P/i)*inv[P%i]%P,
ifac[i]=mul(ifac[i-1],inv[i]);
}
fp(i,1,n){
fp(j,0,i-1)B[i]=dec(B[i],mul(B[j],C(i+1,j)));
B[i]=mul(B[i],inv[i+1]);
}
}
int main(){
// freopen("testdata.in","r",stdin);
init(N-1);
for(int T=read();T;--T){
n=read(),k=read(),nw=q=(n+1)%P,res=0;
for(R int i=k;~i;--i,nw=mul(nw,q))res=add(res,1ll*B[i]*C(k+1,i)%P*nw%P);
res=mul(res,inv[k+1]);
print(res);
}
return Ot(),0;
}