# 洛谷P3711 仓鼠的数学题（伯努利数+多项式求逆）

## 题解

$\sum_{i=1}^{n-1}i^k={1\over k+1}\sum_{i=0}^k{k+1\choose i}B_in^{k+1-i}$

\begin{aligned} Ans &=\sum_{k=0}^na_kS_k(x)\\ &=\sum_{k=0}^na_k\left(x^k+{1\over k+1}\sum_{i=0}^k{k+1\choose i}B_ix^{k+1-i}\right)\\ &=\sum_{k=0}^na_kx^k+\sum_{k=0}^na_kk!\sum_{i=0}^k{B_ix^{k+1-i}\over i!(k+1-i)!}\\ \end{aligned}

\begin{aligned} Ans &=\sum_{k=0}^na_kx^k+\sum_{d=1}^{n+1}{x^d\over d!}\sum_{k=d-1}^na_kk!{B_{k+1-d}\over (k+1-d)!} \end{aligned}

\begin{aligned} Ans &=\sum_{k=0}^na_kx^k+\sum_{d=1}^{n+1}{x^d\over d!}\sum_{k=d-1}^na_kk!{G_{n-k-1+d}\over (n-k-1+d)!} \end{aligned}

//minamoto
#include<bits/stdc++.h>
#define R register
#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)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
R int 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]=' ';
}
const int N=(1<<19)+5,P=998244353,Gi=332748118;
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],C[N],A[N],ans[N],O[N],r[N],a[N];
int n,lim,l,len;
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 NTT(int *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){
R int I=(mid<<1),Wn=ksm(ty==1?3:Gi,(P-1)/I),t;O[0]=1;
fp(i,1,mid-1)O[i]=mul(O[i-1],Wn);
for(R int j=0;j<lim;j+=I)fp(k,0,mid-1)
A[j+k+mid]=dec(A[j+k],t=mul(O[k],A[j+k+mid])),
}
if(ty==-1)for(R int i=0,inv=ksm(lim,P-2);i<lim;++i)A[i]=mul(A[i],inv);
}
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);
static int A[N],B[N];init(len<<1);
fp(i,0,len-1)A[i]=a[i],B[i]=b[i];
fp(i,len,lim-1)A[i]=B[i]=0;
NTT(A,1),NTT(B,1);
fp(i,0,lim-1)A[i]=mul(A[i],mul(B[i],B[i]));
NTT(A,-1);
}
void qwq(int len){
B[0]=inv[0]=inv[1]=fac[0]=fac[1]=ifac[0]=ifac[1]=1;
fp(i,2,len+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,len-1)A[i]=ifac[i+1];
Inv(A,B,len);
fp(i,0,len-1)B[i]=mul(B[i],fac[i]);
}
int main(){
//	freopen("testdata.in","r",stdin);
qwq(len);
reverse(B,B+n+1);init((n<<1)+1);
fp(i,n+1,lim-1)B[i]=C[i]=0;
NTT(B,1),NTT(C,1);
fp(i,0,lim-1)B[i]=mul(B[i],C[i]);
NTT(B,-1);
fp(i,0,n+1)B[n+i-1]=mul(B[n+i-1],ifac[i]);
print(a[0]);
fp(i,1,n+1)print(B[n+i-1]);
return Ot(),0;
}

posted @ 2019-03-15 17:50  bztMinamoto  阅读(133)  评论(0编辑  收藏
Live2D