题面

传送门

题解

不知道伯努利数是什么的可以先去看看这篇文章

多项式求逆预处理伯努利数就行

因为这里模数感人,所以得用\(MTT\)

//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;
}
const int N=5e5+5,K=50005,P=1e9+7;const double Pi=acos(-1.0);
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;
}
struct cp{
double x,y;
cp(){}
cp(R double xx,R double yy):x(xx),y(yy){}
inline cp operator +(const cp &b)const{return cp(x+b.x,y+b.y);}
inline cp operator -(const cp &b)const{return cp(x-b.x,y-b.y);}
inline cp operator *(const cp &b)const{return cp(x*b.x-y*b.y,x*b.y+y*b.x);}
}w[2][N];
int r[N],inv[N],ifac[N],fac[N],B[N],A[N],c[N],d[N],lim,l,n,k,res,nw;
inline void init(int len){
lim=1,l=0;while(lim<len)lim<<=1,++l;
fp(i,0,lim-1)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
}
void FFT(cp *A,int ty){
fp(i,0,lim-1)if(i<r[i])swap(A[i],A[r[i]]);
for(R int mid=1;mid<lim;mid<<=1)
for(R int j=0;j<lim;j+=(mid<<1))
fp(k,0,mid-1){
R cp x=A[j+k],y=A[j+k+mid]*w[ty][mid+k];
A[j+k]=x+y,A[j+k+mid]=x-y;
}
if(!ty){
R double k=1.0/lim;
fp(i,0,lim-1)A[i].x*=k;
}
}
void MTT(int *a,int *b,int len,int *c){
init(len<<1);
static cp A[N],B[N],C[N],D[N],E[N],G[N],F[N];
fp(i,0,len-1){
A[i].x=a[i]>>15,B[i].x=a[i]&32767,
C[i].x=b[i]>>15,D[i].x=b[i]&32767,
A[i].y=B[i].y=C[i].y=D[i].y=0;
}fp(i,len,lim-1)A[i]=B[i]=C[i]=D[i]=cp(0,0);
FFT(A,1),FFT(B,1),FFT(C,1),FFT(D,1);
fp(i,0,lim-1)E[i]=A[i]*C[i],F[i]=A[i]*D[i]+B[i]*C[i],G[i]=B[i]*D[i];
FFT(E,0),FFT(F,0),FFT(G,0);
fp(i,0,lim-1)c[i]=(((ll)(E[i].x+0.5)%P<<30)+((ll)(F[i].x+0.5)<<15)+((ll)(G[i].x+0.5)))%P;
}
void Inv(int *a,int *b,int len){
if(len==1)return b[0]=ksm(a[0],P-2),void();
Inv(a,b,len>>1);
MTT(a,b,len,c),MTT(c,b,len,d);
fp(i,0,len-1)b[i]=dec(add(b[i],b[i]),d[i]);
}
inline void init(){
for(R int i=1;i<(1<<17);i<<=1)fp(k,0,i-1)
w[1][i+k]=cp(cos(Pi*k/i),sin(Pi*k/i)),w[0][i+k]=cp(cos(Pi*k/i),-sin(Pi*k/i));
B[0]=ifac[0]=ifac[1]=inv[0]=inv[1]=fac[0]=fac[1]=1;
fp(i,2,K+5){
fac[i]=mul(fac[i-1],i),
inv[i]=mul(P-P/i,inv[P%i]),
ifac[i]=mul(ifac[i-1],inv[i]);
}
fp(i,0,K)A[i]=ifac[i+1];
Inv(A,B,1<<16);
fp(i,0,K)B[i]=mul(B[i],fac[i]);
}
inline int C(R int n,R int m){return m>n?0:1ll*fac[n]*ifac[m]%P*ifac[n-m]%P;}
int main(){
// freopen("testdata.in","r",stdin);
init();
for(int T=read();T;--T){
n=read()%P,k=read(),res=0,nw=n+1;
for(R int i=k;~i;--i,nw=mul(nw,n+1))res=add(res,1ll*C(k+1,i)*B[i]%P*nw%P);
res=mul(res,inv[k+1]),printf("%d\n",res);
}
return 0;
}
04-30 21:12