Solution
约定:将 翻转一下变为两个集合的按位或相等; 表示 的 。
考虑到原式即为
其中 的乘法均定义为指数做按位或。直接做即可获得 的做法,注意到这是或卷积,考虑 FWT。将 FWT 显式地写出来即可得到答案的表达式:
观察到 B 性质即 ,猜测做法中存在 的部分。结合后面乘积部分中一个 的贡献只能为 、 或 ,尝试对于每一种贡献算次数。整理后不难得到答案即为
这就看起来非常可做了。设辅助数组 :
枚举 ,不难发现此时 的作用就是算一个贡献系数,那么进一步的答案即为:
可以用 And 卷积做这个事情,这样即可在 时间内通过 B 性质。
接着考虑拓展到一般情况,尝试维护形如 的形式。相乘相除是容易的,考虑相加。注意到最终 ,且只有 时有贡献,所以在相加时可以只保留次数小的那一项。于是我们在 时间内解决了本题。
Code
bool Mst;
#include<bits/stdc++.h>
using namespace std;
using ui=unsigned int;
using ll=long long;
using ull=unsigned long long;
using i128=__int128;
using u128=__uint128_t;
using pii=pair<int,int>;
#define fi first
#define se second
#define popc __builtin_popcount
constexpr int N=20,M=1<<N,mod=998244353,_inv2=(mod-1)>>1;
inline ll add(ll x,ll y){return (x+=y)>=mod&&(x-=mod),x;}
inline ll Add(ll &x,ll y){return x=add(x,y);}
inline ll sub(ll x,ll y){return (x-=y)<0&&(x+=mod),x;}
inline ll Sub(ll &x,ll y){return x=sub(x,y);}
inline ll qpow(ll a,ll b){
ll res=1;
for(;b;b>>=1,a=a*a%mod)
if(b&1)res=res*a%mod;
return res;
}
struct node{
ll a,b;
node(){a=1,b=0;}
node(ll x){a=x+!x,b=!x;}
node(ll _a,ll _b){a=_a,b=_b;}
inline ll get()const{return !b?a:0;}
inline node inv()const{return node(qpow(a,mod-2),-b);}
};
inline node operator +(const node &u,const node &v){
node w(0,min(u.b,v.b));
if(u.b==w.b)Add(w.a,u.a);
if(v.b==w.b)Add(w.a,v.a);
return w;
}
inline node operator -(const node &u,const node &v){
node w(0,min(u.b,v.b));
if(u.b==w.b)Add(w.a,u.a);
if(v.b==w.b)Sub(w.a,v.a);
return w;
}
inline node operator *(const node &u,const node &v){
return node(u.a*v.a%mod,u.b+v.b);
}
inline node operator /(const node &u,const node &v){
return u*v.inv();
}
inline node operator +=(node &u,const node &v){
return u=u+v;
}
inline node operator -=(node &u,const node &v){
return u=u-v;
}
inline node operator *=(node &u,const node &v){
return u=u*v;
}
inline node operator /=(node &u,const node &v){
return u=u/v;
}
int n,m;ll pw[N+1],_pw[N+1],a[M];node f[M],g[M];
inline void Or(node *F){
for(int i=1;i<m;i<<=1)
for(int j=0;j<m;j+=i<<1)
for(int k=0;k<i;k++)
F[i|j|k]*=F[j|k];
}
inline void And(node *F){
for(int i=1;i<m;i<<=1)
for(int j=0;j<m;j+=i<<1)
for(int k=0;k<i;k++)
F[j|k]+=F[i|j|k];
}
inline void IAnd(node *F){
for(int i=1;i<m;i<<=1)
for(int j=0;j<m;j+=i<<1)
for(int k=0;k<i;k++)
F[j|k]-=F[i|j|k];
}
void Solve(){
cin>>n,m=1<<n;
for(int i=0;i<m;i++)cin>>a[i];
reverse(a,a+m);
for(int i=0;i<m;i++){
f[i]=add(a[i],1);
g[i]=add(add(a[i],a[i]),1)/(f[i]*f[i]);
}
Or(f),Or(g);
pw[0]=_pw[0]=1;
for(int i=1;i<=n;i++){
pw[i]=pw[i-1]*2%mod;
_pw[i]=_pw[i-1]*_inv2%mod;
}
for(int i=0;i<m;i++)f[i]*=_pw[popc(i)];
And(f);
for(int i=0;i<m;i++)f[i]*=f[i];
IAnd(f);
ll ans=0;
for(int i=0;i<m;i++)Add(ans,(f[i]*g[i]*pw[popc(i)]).get());
cout<<m*ans%mod<<'\n';
}
bool Med;
int main(){
cerr<<abs(&Mst-&Med)/1048576.0<<endl;
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int _id,_Test;cin>>_id>>_Test;
while(_Test--)Solve();
return 0;
}