题解:P13275 [NOI2025] 集合
一个场外选手的心路历程。
noi 结束第二天下午,我们到龙叔叔家做客,叔叔指导漂亮姐姐画素描,为了感受一些艺术气息,我也找了个帮忙削笔的理由坐在旁边看。只可惜我并没有很多的艺术细菌,很快感到无聊的我想到可以看一看最近结束的 noi,于是我在 luogu 上搜索 noi2025,打开了默认顺序的第一道题。
由于我有将近三个星期没有认真想过题了,所以一开始进行了很多无效思考,我先阐述我的有效思考部分:
首先把与换成或,因为我喜欢。
然后考虑把朴素 dp 的两维状压在一起,那么这个 dp 本质上就是对
考虑这些多项式的性质,第
感觉这个应该能拿分了,于是削了一根刚刚画钝的炭笔,姐姐也快把那个不锈钢水壶花完了,感觉那个反光的部分特别像,太有水平了。
通过特殊性质 b 发现,
我感觉这个做法好厉害,应该能拿很多分了,可惜由于无法避免的需要做
代码:
#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;
}
后来我打开题解瞄了一眼,发现了一个伟大的式子:
我恍然大悟!
首先把或换成与,虽然我不喜欢。
把原题的式子通过上述容斥可得到:
发现后面的求和式子与我们暴力中 FWT 之后的多项式等价!所以我们不需要枚举所有
把
枚举交就是
后面做或卷积即可,但是还要解决维护
容易想到维护一个关于
于是我们通过了这道题!
代码:
#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;
}
感受了一下姐姐的素描作品,感觉会画画的都是神人了,西红柿放在不锈钢旁边的反光状态和真的一模一样,被震撼到了。姐姐夸我削笔削的很尖也不会断,好开心。
后来龙叔叔展示了一下他年轻时候的作品,由于我语言能力匮乏,只能想到用震撼来形容,像电视里面画徒手花假钞一样,每一笔仔细看都能看到笔触,但是放在一起就是一个完全不违和的人像,不愧是艺术大师。
吃了饭之后就回家了,饭菜很好吃。