# CodeChefSeries Sum （伯努利数+生成函数+FFT）

## 题解

cjj拿着这题来问咱，咱发现自己跟个白痴一样啥也不会……

\begin{aligned} f(x,k) &=\sum_{i=1}^n(x+a_i)^k\\ &=\sum_{i=1}^n\sum_{j=0}^k{k\choose j}x^j{a_i}^{k-j}\\ &=\sum_{j=0}^k{k\choose j}x^j\sum_{i=1}^n{a_i}^{k-j}\\ \end{aligned}

$g(t,k)=\sum_{x=0}^t\sum_{j=0}^k{k\choose j}x^j\sum_{i=1}^n{a_i}^{k-j}$

${g(t,k)\over k!}=\sum_{j=0}^k{\sum_{x=0}^tx^j\over j!}{\sum_{i=1}^n{a_i}^{k-j}\over (k-j)!}$

## 左边

\begin{aligned} \left[x^k\right]F &=\sum_{i=0}^ti^k\\ &=t^k+{1\over k+1}\sum_{i=0}^k{k+1\choose i}B_it^{k+1-i}\\ &=t^k+k!\sum_{i=0}^k{B_i\over i!}{t^{k+1-i}\over (k+1-i)!} \end{aligned}

## 右边

\begin{aligned} A(x)=\sum_{i=0}^\infty x^i{\sum_{k=1}^n{a_k}^i\over i!} \end{aligned}

\begin{aligned} A(x) &=\sum_{i=0}^\infty x^i\sum_{j=1}^n{a_j}^i\\ &=\sum_{j=1}^n\sum_{i=0}^\infty {a_j}^ix^i\\ &=\sum_{i=1}^n{1\over 1-a_ix}\\ &=\sum_{i=1}^n {a_i}^0+{a_i}^1x^1+{a_i}^2x^2+... \end{aligned}

\begin{aligned} G(x) &=\sum_{i=1}^n{-a_i\over 1-a_ix}\\ &=\sum_{i=1}^n-{a_i}^1-{a_i}^2x-{a_i}^3x^2-...\\ \end{aligned}

\begin{aligned} G(x) &=\sum_{i=1}^n{-a_i\over 1-a_ix}\\ &=\sum_{i=1}^n\ln'\left(1-a_ix\right)\\ &=\ln'\left(\prod_{i=1}^n (1-a_ix)\right) \end{aligned}

//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)
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;
}
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 C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
void print(R int x){
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]=' ';
}
const int N=(1<<18)+5,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(R double xx=0,R double yy=0){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);}
}rt[2][N<<1];
int r[21][N],inv[N],fac[N],ifac[N],B[N],A[N],d,lim;
inline void init(R int len){lim=1,d=0;while(lim<len)lim<<=1,++d;}
void FFT(cp *A,int ty){
fp(i,0,lim-1)if(i<r[d][i])swap(A[i],A[r[d][i]]);
cp t;
for(R int mid=1;mid<lim;mid<<=1)
for(R int j=0;j<lim;j+=(mid<<1))
fp(k,0,mid-1)
A[j+k+mid]=A[j+k]-(t=A[j+k+mid]*rt[ty][mid+k]),
A[j+k]=A[j+k]+t;
if(!ty){
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],F[N],G[N],H[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]=0;
FFT(A,1),FFT(B,1),FFT(C,1),FFT(D,1);
fp(i,0,lim-1)F[i]=A[i]*C[i],G[i]=A[i]*D[i]+B[i]*C[i],H[i]=B[i]*D[i];
FFT(F,0),FFT(G,0),FFT(H,0);
fp(i,0,lim-1)c[i]=(((ll)(F[i].x+0.5)%P<<30)+((ll)(G[i].x+0.5)<<15)+((ll)(H[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);
static int c[N],d[N];
MTT(a,b,len,c),MTT(b,c,len,d);
}
void Ln(int *a,int *b,int len){
static int A[N],B[N];
fp(i,1,len-1)A[i-1]=mul(a[i],i);A[len-1]=0;
Inv(a,B,len),MTT(A,B,len,A);
fp(i,1,len-1)b[i]=mul(A[i-1],inv[i]);b[0]=0;
}
void Pre(){
fp(d,1,18)fp(i,1,(1<<d)-1)r[d][i]=(r[d][i>>1]>>1)|((i&1)<<(d-1));
inv[0]=fac[0]=ifac[0]=inv[1]=fac[1]=ifac[1]=1;
fp(i,2,262144){
fac[i]=mul(fac[i-1],i),
inv[i]=mul(P-P/i,inv[P%i]),
ifac[i]=mul(ifac[i-1],inv[i]);
}
for(R int i=1;i<=262144;i<<=1)fp(k,0,i-1)
rt[1][i+k]=cp(cos(Pi*k/i),sin(Pi*k/i)),rt[0][i+k]=cp(cos(Pi*k/i),-sin(Pi*k/i));
fp(i,0,65535)A[i]=ifac[i+1];
Inv(A,B,1<<16);
fp(i,0,65535)B[i]=mul(B[i],fac[i]);
}
int D[21][N],a[N];
void solve(int d,int l,int r){
if(l==r)return D[d][0]=1,D[d][1]=P-a[l],void();
int mid=(l+r)>>1;
solve(d,l,mid),solve(d+1,mid+1,r);
init(max(mid-l+1,r-mid)+1);
int len=lim;
fp(i,mid-l+2,len-1)D[d][i]=0;
fp(i,r-mid+1,len-1)D[d+1][i]=0;
MTT(D[d],D[d+1],len,D[d]);
fp(i,r-l+2,(len<<1)-1)D[d][i]=0;
}
void calc(int *a,int *b,int n,int t){
static int A[N],B[N];
solve(1,1,n);
init(t+1);int len=lim;
fp(i,0,n)A[i]=D[1][i];
fp(i,n+1,len-1)A[i]=0;
Ln(A,B,len);
fp(i,1,len-1)B[i-1]=mul(B[i],i);B[len-1]=0;
b[0]=n;
fp(i,1,t)b[i]=mul(P-B[i-1],ifac[i]);
}
int ak[N],bk[N],bin[N],F[N],G[N],n,k,t;
void MAIN(){
bin[0]=1;fp(i,1,k+k)bin[i]=mul(bin[i-1],t);
int len=1;while(len<k+2)len<<=1;
fp(i,0,len-1)F[i]=mul(B[i],ifac[i]),G[i]=mul(bin[i],ifac[i]);
G[0]=0;
MTT(F,G,len,F);
ak[0]=t+1;
calc(a,bk,n,k);
len=1;while(len<k)len<<=1;
fp(i,k+1,len-1)ak[i]=bk[i]=0;
MTT(ak,bk,len,ak);
fp(i,0,k)print(mul(ak[i],fac[i]));
}
int main(){
//	freopen("testdata.in","r",stdin);
Pre();