题解:P13275 [NOI2025] 集合

· · 题解

一个场外选手的心路历程。

noi 结束第二天下午,我们到龙叔叔家做客,叔叔指导漂亮姐姐画素描,为了感受一些艺术气息,我也找了个帮忙削笔的理由坐在旁边看。只可惜我并没有很多的艺术细菌,很快感到无聊的我想到可以看一看最近结束的 noi,于是我在 luogu 上搜索 noi2025,打开了默认顺序的第一道题。

由于我有将近三个星期没有认真想过题了,所以一开始进行了很多无效思考,我先阐述我的有效思考部分:

首先把与换成或,因为我喜欢。

然后考虑把朴素 dp 的两维状压在一起,那么这个 dp 本质上就是对 2^n 个 4^n 维多项式做或卷积。

考虑这些多项式的性质,第 s 个多项式 f 形如 f_0=1,f_s=a_s,f_{s\times 2^n}=a_s,其他位置都是 0,显然在 FWT 之后的多项式只有四种状态。预处理出 f_s=\prod_{t\subseteq s} (a_t+1),g_s=\prod_{t\subseteq s} (2a_t+1)。枚举下标 s+t\times 2^n,这个位置 FWT 之后卷积的值是 \frac{g_{s\cap t}}{f^2_{s\cap t}}\times f_s\times f_t。

感觉这个应该能拿分了,于是削了一根刚刚画钝的炭笔,姐姐也快把那个不锈钢水壶花完了,感觉那个反光的部分特别像,太有水平了。

通过特殊性质 b 发现,f_s=0 的时候不能直接做除法,不过这也是好处理的,维护 b \times {eps}^a,这样就可以做乘除了。

我感觉这个做法好厉害,应该能拿很多分了,可惜由于无法避免的需要做 4^n 的 FWT,所以只拿到了朴素的 36 分,后来知道 36 分是大众分,于是有点破防,拼尽全力只有大众分。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=(1<<20)+10,M=(1<<24)+10;
const int mod=998244353;
LL qpow(LL a,LL b){
    LL sum=1;
    for(;b;b>>=1,a=a*a%mod) if(b&1) sum=sum*a%mod;
    return sum;
}
int n,m;
LL a[N];
struct node{
    LL a,b; // b*eps^a
    node(LL aa=0,LL bb=1){a=aa,b=bb;}
    node operator + (const node &E){
        return node((a+E.a)%mod,b*E.b%mod);
    }
    node operator - (const node &E){
        return node((a-E.a)%mod,b*qpow(E.b,mod-2)%mod);
    }
};
node f[N],g[N];
LL A[M];
void read(){
    cin>>n; m=(1<<n);
    for(int i=0;i<m;i++) cin>>a[i^(m-1)];
}
void FWT(int m,node a[]){
    for(int l=1;l<m;l<<=1){
        for(int i=0;i<m;i+=(l<<1)){
            for(int j=0;j<l;j++){
                a[i+j+l]=a[i+j+l]+a[i+j];
            }
        }
    }
}
void IFWT(int m,LL a[]){
    for(int l=1;l<m;l<<=1){
        for(int i=0;i<m;i+=(l<<1)){
            for(int j=0;j<l;j++){
                (a[i+j+l]-=a[i+j])%=mod;
            }
        }
    }
}
void init(){
    for(int i=0;i<m;i++){
        LL w=(a[i]+1)%mod;
        if(w==0) f[i]=node(1,1);
        else f[i]=node(0,w);
    }
    FWT(m,f);
    for(int i=0;i<m;i++){
        LL w=(a[i]*2+1)%mod;
        if(w==0) g[i]=node(1,1);
        else g[i]=node(0,w);
    }
    FWT(m,g);
    for(int i=0;i<m;i++){
        g[i]=g[i]-f[i]-f[i];
    }
    for(int i=0;i<m*m;i++){
        int s=(i>>n),t=(i^(s<<n));
        node p=g[s&t]+f[s]+f[t];
        if(p.a>0) A[i]=0;
        else A[i]=p.b;
    }
    IFWT(m*m,A);
    LL ans=0;
    for(int s=0;s<m;s++){
        (ans+=A[s|(s<<n)])%=mod;
    }
    ans=(ans+mod)%mod;
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    // freopen("a.in","r",stdin);
    // freopen("a.out","w",stdout);
    int tid,T=1;
    cin>>tid>>T;
    while(T--){
        read();
        init();
    }
    cerr<<1.0*clock()/CLOCKS_PER_SEC<<'\n';
    return 0;
}

后来我打开题解瞄了一眼,发现了一个伟大的式子:

[P=Q]=\sum_{A\subseteq P,B\subseteq Q} (-1)^{|A|+|B|}2^{|A\cap B|}.

我恍然大悟!

首先把或换成与,虽然我不喜欢。

把原题的式子通过上述容斥可得到:

\sum_{A,B} (-1)^{|A|+|B|}2^{|A\cap B|} \sum_{A\subseteq f(S),B\subseteq f(T),T\cap S=\varnothing }\prod_{x\in S\cup T} a_x.

发现后面的求和式子与我们暴力中 FWT 之后的多项式等价!所以我们不需要枚举所有 4^n 个位置,就可以进行转化了!

把 2^{|A\cap B|} 转化为 2^{|A|}\times 2^{|B|}\times \frac{1}{2^{|A\cup B|}},式子中只跟 A,B,A\cup B 有关,转化为

\sum_{A,B}f(A)f(B)g(A\cup B).

枚举交就是

\sum_{C} g(C) \sum_{A\cup B=C}f(A)f(B).

后面做或卷积即可,但是还要解决维护 eps 的问题。

容易想到维护一个关于 eps 的多项式,这是很劣的,因为答案中不会存在 eps^{-n} 状物,并且只需要 eps^0 项,所以只会有一项有用,显然是最低次项。

于是我们通过了这道题!

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=(1<<20)+10,M=(1<<24)+10;
const int mod=998244353;
LL qpow(LL a,LL b){
    LL sum=1;
    for(;b;b>>=1,a=a*a%mod) if(b&1) sum=sum*a%mod;
    return sum;
}
int n,m,popc[N];
LL a[N];
struct node{
    LL a,b; // b*eps^a
    node(LL aa=0,LL bb=1){a=aa,b=bb;}
    node operator + (const node &E) const{
        if(a==E.a) return node(a,(b+E.b)%mod);
        if(a>E.a) return E;
        return node(a,b);
    }
    node operator - (const node &E) const{
        if(a==E.a) return node(a,(b-E.b)%mod);
        if(a>E.a) return E;
        return node(a,b);
    }
    node operator * (const node &E) const{
        return node((a+E.a)%mod,b*E.b%mod);
    }
    node operator / (const node &E) const{
        return node((a-E.a)%mod,b*qpow(E.b,mod-2)%mod);
    }
};
node f[N],g[N];
void read(){
    cin>>n; m=(1<<n);
    for(int i=0;i<m;i++) cin>>a[i];
    for(int i=1;i<m;i++) popc[i]=popc[i>>1]+(i&1);
}
void FWT1(int m,node a[]){
    for(int l=1;l<m;l<<=1){
        for(int i=0;i<m;i+=(l<<1)){
            for(int j=0;j<l;j++){
                a[i+j]=a[i+j+l]*a[i+j];
            }
        }
    }
}
void FWT2(int m,node a[]){
    for(int l=1;l<m;l<<=1){
        for(int i=0;i<m;i+=(l<<1)){
            for(int j=0;j<l;j++){
                a[i+j+l]=a[i+j+l]+a[i+j];
            }
        }
    }
}
void IFWT(int m,node a[]){
    for(int l=1;l<m;l<<=1){
        for(int i=0;i<m;i+=(l<<1)){
            for(int j=0;j<l;j++){
                a[i+j+l]=a[i+j+l]-a[i+j];
            }
        }
    }
}
void init(){
    for(int i=0;i<m;i++){
        LL w=(a[i]+1)%mod;
        if(w==0) f[i]=node(1,1);
        else f[i]=node(0,w);
    }
    FWT1(m,f);
    for(int i=0;i<m;i++){
        LL w=(a[i]*2+1)%mod;
        if(w==0) g[i]=node(1,1);
        else g[i]=node(0,w);
    }
    FWT1(m,g);
    for(int i=0;i<m;i++){
        g[i]=g[i]/f[i]/f[i];
        if(popc[i]&1) f[i].b=mod-f[i].b;
        f[i]=f[i]*node(0,(1<<popc[i]));
        g[i]=g[i]/node(0,(1<<popc[i]));
    }
    FWT2(m,f);
    for(int i=0;i<m;i++) f[i]=f[i]*f[i];
    IFWT(m,f);
    LL ans=0;
    for(int i=0;i<m;i++){
        node p=g[i]*f[i];
        if(p.a==0) (ans+=p.b)%=mod;
    }
    ans=(ans+mod)%mod;
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
 //   freopen("a.in","r",stdin);
 //   freopen("a.out","w",stdout);
    int tid,T=1;
    cin>>tid>>T;
    while(T--){
        read();
        init();
    }
    cerr<<1.0*clock()/CLOCKS_PER_SEC<<'\n';
    return 0;
}

感受了一下姐姐的素描作品,感觉会画画的都是神人了,西红柿放在不锈钢旁边的反光状态和真的一模一样,被震撼到了。姐姐夸我削笔削的很尖也不会断,好开心。

后来龙叔叔展示了一下他年轻时候的作品,由于我语言能力匮乏,只能想到用震撼来形容,像电视里面画徒手花假钞一样,每一笔仔细看都能看到笔触,但是放在一起就是一个完全不违和的人像,不愧是艺术大师。

吃了饭之后就回家了,饭菜很好吃。