P4389 付公主的背包
还是搞不明白生成函数是什么东西……
首先设对于体积为\(v\)的物品,它的生成函数为\(f(x)=\sum_{i\geq 0} x^{vi}\),那么答案的生成函数就是所有的物品的生成函数的乘积,复杂度为\(O(nm\log n)\)
于是考虑把所有生成函数取\(\ln\)相加再\(\exp\)回去,设\(g(x)=\ln(f(x))\),因为有\(f(x)=\frac{1}{1-x^v}\)
所以$$g'(x)=\frac{f'(x)}{f(x)}$$
\[g'(x)=(1-x^v)f'(x)
\]
\[g'(x)=(1-x^v)\sum_{i\geq 0}vix^{vi-1}
\]
\[g'(x)=\sum_{i\geq0}v(i-(i-1))x^{vi-1}
\]
\[g'(x)=\sum_{i\geq0}vx^{vi-1}
\]
然后积分$$g(x)=\sum_{i \geq 1} \frac{1}{i}x^{vi}$$
然后用多项式\(\exp\)求出\(f\)就可以了
//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)
#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++;}
int read(){
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]='\n';
}
const int N=5e5+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 r[N],O[N],A[N],B[N],C[N],D[N],F[N],G[N],H[N];
int n,m,x,cnt[N];
void NTT(int *A,int ty,int len){
int 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));
fp(i,0,lim-1)if(i<r[i])swap(A[i],A[r[i]]);
for(R int mid=1;mid<lim;mid<<=1){
int I=(mid<<1),Wn=ksm(ty==1?3:Gi,(P-1)/I);O[0]=1;
fp(i,1,mid-1)O[i]=mul(O[i-1],Wn);
for(R int j=0;j<lim;j+=I)for(R int k=0;k<mid;++k){
int x=A[j+k],y=mul(O[k],A[j+k+mid]);
A[j+k]=add(x,y),A[j+k+mid]=dec(x,y);
}
}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 (void)(b[0]=ksm(a[0],P-2));
Inv(a,b,len>>1);fp(i,0,len-1)A[i]=a[i],B[i]=b[i];
NTT(A,1,len<<1),NTT(B,1,len<<1);
fp(i,0,(len<<1)-1)A[i]=mul(A[i],mul(B[i],B[i]));
NTT(A,-1,len<<1);fp(i,0,len-1)b[i]=dec(mul(b[i],2),A[i]);
fp(i,0,(len<<1)-1)A[i]=B[i]=0;
}
void Direv(int *a,int *b,int len){
fp(i,1,len-1)b[i-1]=mul(a[i],i);b[len-1]=0;
}
void Inter(int *a,int *b,int len){
fp(i,1,len-1)b[i]=mul(a[i-1],ksm(i,P-2));b[0]=0;
}
void Ln(int *a,int *b,int len){
Direv(a,C,len),Inv(a,D,len);
NTT(C,1,len<<1),NTT(D,1,len<<1);
fp(i,0,(len<<1)-1)C[i]=mul(C[i],D[i]);
NTT(C,-1,len<<1),Inter(C,b,len);
fp(i,0,(len<<1)-1)C[i]=D[i]=0;
}
void Exp(int *a,int *b,int len){
if(len==1)return (void)(b[0]=1);
Exp(a,b,len>>1),Ln(b,F,len);
F[0]=dec(a[0]+1,F[0]);fp(i,1,len-1)F[i]=dec(a[i],F[i]);
NTT(F,1,len<<1),NTT(b,1,len<<1);
fp(i,0,(len<<1)-1)b[i]=mul(b[i],F[i]);
NTT(b,-1,len<<1);fp(i,len,(len<<1)-1)b[i]=F[i]=0;
}
int main(){
// freopen("testdata.in","r",stdin);
n=read(),m=read();
fp(i,1,n)x=read(),++cnt[x];
fp(i,1,m)if(cnt[i]){
for(R int j=i;j<=m;j+=i)
G[j]=add(G[j],mul(cnt[i],mul(i,ksm(j,P-2))));
}int len=1;while(len<=m)len<<=1;
Exp(G,H,len);fp(i,1,m)print(H[i]);
return Ot(),0;
}
深深地明白自己的弱小