题解:P13843 集合幂级数 exp(非素数模数)

· · 题解

根据题目描述根据泰勒展开,有:

\mathrm e^{F(x)}=\sum_{n\ge 0}\frac{F^n(x)}{n!}

然后我们尝试瞪出这个东西的组合意义为:若 [x^s]F(x) 为子集 s 的方案数,那么 [x^s]\mathrm e^{F(x)} 为把 s 划分为任意个子集的方案数。

定义 G(x)=\mathrm e^{F(x)},f_s 为 [x^s]F(x),g_s 为 [x^s]G(x)。

根据组合意义,我们有 g_\varnothing=1。

定义 \operatorname{lowbit}(x) 为 x 的二进制下最低位。特别的,我们钦定 \operatorname{lowbit}(0) =0。

对于 s \neq \varnothing,考虑枚举其中一个点所在的子集,则有如下式子。

g_s=f_s+\sum_{i \subsetneq s,\operatorname{lowbit}(i)=\operatorname{lowbit}(s)} f_i \times g_{s-i}

容易发现这是一个半在线的子集卷积,可以直接用类似集合幂级数求逆的方法,枚举 |s| 计算答案,但是这个题有个问题,需要保证 \operatorname{lowbit}(i)=\operatorname{lowbit}(s)。

考虑魔改一下 fwt,定义 FWT(f)_s=\sum_{i|s=s} f_i,FWT_2(f)=\sum_{i|s=s,\operatorname{lowbit}(i)=\operatorname{lowbit}(s)} f_i。

类似正常的或卷积,我们发现有如下式子:

[x^s]FWT_2(a) \times FWT(b)\\ = \sum_{i|s=s,\operatorname{lowbit}(i)=\operatorname{lowbit}(s)} a_i \times \sum_{j|s=s} b_j\\ =\sum_{(i|j)|s=s,\operatorname{lowbit}(i)=\operatorname{lowbit}(s)} a_i \times b_j

容易发现这正是我们想要的东西,于是拿这个魔改的卷积拼上一个半在线的子集卷积就行了,时间复杂度 O(n^22^n)。

Code

#include<bits/stdc++.h>
namespace fast_IO{
    #define IOSIZE 100000
        char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
    #define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
    #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    #define isdigit(ch) (ch>47&&ch<58)
    #define isspace(ch) (ch<33)
        template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
        template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
        template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
        inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
        inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
        inline void print(char x) { putchar(x); }
        inline void print(char *x) { while (*x) putchar(*x++); }
        inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
        inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
        inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
        inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
        inline void print(bool b) { putchar(b+48); }
        template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
        template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
        struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
        template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
        template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
    #define cout io
    #define cin io
}using namespace fast_IO;
using namespace std;
#define int unsigned long long
int n,limit;
vector<int>f[21],g[21];
#define lowbit(x) ((x)&(-x))
#define pop __builtin_popcount
inline void OR(int limit,vector<int>&a,int opt){
    for(int len=2;len<=limit;len<<=1)
    for(int i=0;i<limit;i+=len)
    for(int j=i;j<i+(len>>1);++j)
      a[j+(len>>1)]=a[j+(len>>1)]+a[j]*opt;
}
inline void OR_low(int limit,vector<int>&a,int opt){
    for(int len=2;len<=limit;len<<=1)
    for(int i=0;i<limit;i+=len)
    for(int j=i+1;j<i+(len>>1);++j)
      a[j+(len>>1)]=a[j+(len>>1)]+a[j]*opt;
}
signed main(){
    cin>>n;
    limit=(1<<n); 
    for(int i=0;i<=n;++i)f[i].resize(limit),g[i].resize(limit);
    for(int i=0;i<limit;++i)cin>>f[pop(i)][i];
    for(int i=0;i<=n;++i)OR_low(limit,f[i],1);
    g[0][0]=1;
    OR(limit,g[0],1);
    for(int i=1;i<=n;++i){
      for(int j=0;j<i;++j)
      for(int s=0;s<limit;++s)
        g[i][s]=g[i][s]+f[j][s]*g[i-j][s];
      OR_low(limit,g[i],-1);
      for(int s=0;s<limit;++s){
        if(pop(s)==i)g[i][s]=f[i][s]+g[i][s];
        else g[i][s]=0;
      }
      OR(limit,g[i],1);
    }
    for(int i=0;i<limit;++i)cout<<g[pop(i)][i]<<" ";
    cout<<"\n";
    return 0;
}