luoguP4389 付公主的背包

链接

点击跳转

题解

假设体积为 i i i的物品有 c i c_i ci

生成函数:

F ( x ) = ∏ i = 1 m ( 1 1 − x i ) c i F(x) = \prod_{i=1}^m ( \frac{1}{1-x^i} )^{c_i} F(x)=i=1m(1xi1)ci

取对数

ln ⁡ ( F ( x ) ) = ∑ i = 1 m − c i ln ⁡ ( 1 − x i ) \ln(F(x)) = \sum_{i=1}^{m} -c_i \ln { ( 1-x^i ) } ln(F(x))=i=1mciln(1xi)

B ( x ) = 1 − x i B(x) = 1 - x^i B(x)=1xi

B ′ ( x ) = d d x B ( x ) = − i x i − 1 B'(x) = \frac{d}{dx}B(x) = -ix^{i-1} B(x)=dxdB(x)=ixi1

( ln ⁡ B ( x ) ) ′ = B ′ ( x ) B ( x ) = i x i − 1 1 − x i = − i x i − 1 ( 1 + x i + x 2 i + …   ) (\ln B(x))' = \frac{B'(x)}{B(x)} = \frac{ix^{i-1}}{1-x^i} = -ix^{i-1}(1+x^i + x^{2i} + \dots) (lnB(x))=B(x)B(x)=1xiixi1=ixi1(1+xi+x2i+)

( ln ⁡ B ( x ) ) ′ = − i ∑ k = 1 ∞ x k i − 1 (\ln B(x) )' = -i \sum_{k=1}^\infin x^{ki-1} (lnB(x))=ik=1xki1

不定积分,常数项给 0 0 0

ln ⁡ ( B ( x ) ) = − ∑ k = 1 ∞ x k i k \ln (B(x) ) = - \sum_{k=1}^\infin \frac{ x^{ki} } {k} ln(B(x))=k=1kxki

这样就求出 l n ( F ( x ) ) ln(F(x)) ln(F(x))

e x p exp exp一下,就得到 F ( x ) F(x) F(x)

代码

#include 
#include 
#include 
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
#define mod 998244353ll
struct EasyMath
{
    ll prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    ll fastpow(ll a, ll b, ll c)
    {
        ll t(a%c), ans(1ll);
        for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(!b){x=1,y=0;return;}
        ll xx, yy;
        exgcd(b,a%b,xx,yy);
        x=yy, y=xx-a/b*yy;
    }
    ll inv(ll x, ll p)  //p是素数
    {return fastpow(x%p,p-2,p);}
    ll inv2(ll a, ll p)
    {
        ll x, y;
        exgcd(a,p,x,y);
        return (x+p)%p;
    }
    void shai(ll N)
    {
        ll i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
    ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
    {
        ll M=1, ans=0, n=a.size(), i;
        for(i=0;i<n;i++)M*=m[i];
        for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
        return ans;
    }
}em;
struct NTT
{
    ll n;
    vector<ll> R;
    void init(ll bound)    //bound是积多项式的最高次幂
    {
        ll L(0);
        for(n=1;n<=bound;n<<=1,L++);
        R.resize(n);
        for(ll i=0;i<n;i++)R[i]=(R[i>>1]>>1)|((i&1)<<(L-1));
    }
    void ntt(vector<ll>& a, int opt)
    {
        ll i, j, k, wn, w, x, y, inv(em.fastpow(n,mod-2,mod));
        for(i=0;i<n;i++)if(i>R[i])swap(a[i],a[R[i]]);
        for(i=1;i<n;i<<=1)
        {
            if(opt==1)wn=em.fastpow(3,(mod-1)/(i<<1),mod);
            else wn=em.fastpow(3,(mod-1-(mod-1)/(i<<1)),mod);
            for(j=0;j<n;j+=i<<1)
                for(w=1,k=0;k<i;k++,w=w*wn%mod)
                {
                    x=a[k+j], y=a[k+j+i]*w%mod;
                    a[k+j]=(x+y)%mod, a[k+j+i]=(x-y)%mod;
                }
        }
        if(opt==-1)for(i=0;i<n;i++)(a[i]*=inv)%=mod;
    }
};
struct Poly
{
    vector<ll> v;
    ll n;	//n是最高次项的次数
    Poly(ll N){v.resize(N+1);n=N;}
    Poly(const Poly& p){v=p.v; n=p.n;}
    void resize(ll N){n=N; v.resize(N+1);}
    ll& operator[](ll id){return v[id];}
    void show()
    {
        printf("n=%lld\n",n);
        ll i; rep(i,0,n-1)printf("%lldx^%lld + ",(v[i]+mod)%mod,i);
        printf("%lldx^%lld\n",(v[n]+mod)%mod,n);
    }
};
Poly operator+(Poly A, Poly B)
{
    ll i;
    Poly C(max(A.n,B.n));
	A.resize(C.n), B.resize(C.n);
    rep(i,0,C.n)C[i]=A[i]+B[i];
    return C;
}
Poly operator*(ll x, Poly A)
{
    x%=mod;
    ll i; rep(i,0,A.n)(A[i]*=x)%=mod;
    return A;
}
Poly operator*(Poly A, Poly B)
{
    NTT ntt;
    ll i, n=A.n+B.n;
    ntt.init(A.n+B.n);
    A.resize(ntt.n-1), B.resize(ntt.n-1);
    ntt.ntt(A.v,1), ntt.ntt(B.v,1);
    Poly C(ntt.n-1);
    rep(i,0,C.n)C[i]=(A[i]*B[i])%mod;
    ntt.ntt(C.v,-1);
    C.resize(n);
    return C;
}
Poly operator~(Poly F)
{
    Poly H(0), f(0);
    ll i, j;
    H[0]=em.inv(F[0],mod);
    for(i=1;(i>>1)<=F.n;i*=2)
    {
        f.resize(i-1);
        rep(j,0,min(i-1,F.n))f[j]=F[j];
        Poly t=H*H*f; t.resize(i-1);
        H=2*H+(-1)*t;
    }
    H.resize(F.n);
    return H;
}
Poly ln(Poly A)	//常数项必须为1
{
	Poly B(A.n);
	ll i;
	rep(i,1,A.n)B[i-1]=(A[i]*i)%mod;
	B=B*~A; B.resize(A.n);
	drep(i,A.n,1)B[i]=(B[i-1]*em.inv(i,mod))%mod;
	B[0]=0;
	return B;
}
Poly exp(Poly A)	//常数项必须为0,有Bug,A[j]可能会越界
{
	Poly f(0);
	ll i, j;
	f[0]=1;
	for(i=2;(i>>1)<=A.n;i*=2)
	{
		f.resize(i-1);
		Poly t(i-1), g=ln(f);
		rep(j,0,i-1)t[j]=(A[j]-g[j]); (++t[0])%=mod;
		f=f*t;
	}
	f.resize(A.n);
	return f;
}
int main()
{
    ll n=read(), m=read(), i, k;
    vector<ll> c(m+5), inv(m+5);
    inv[1]=1;rep(i,2,m)inv[i]=inv[mod%i]*(mod-mod/i)%mod;
    rep(i,1,n)c[read()]++;
    Poly p(m);
    rep(i,1,m)
    {
        if(c[i]==0)continue;
        for(k=1;k*i<=m;k++)
        {
            (p[k*i]+=c[i]*inv[k]%mod)%=mod;
        }
    }
    p = exp(p);
    rep(i,1,m)printf("%lld\n",(p[i]+mod)%mod);
    return 0;
}

你可能感兴趣的:(形式幂级数,生成函数)