思路
首先算给你 个点有多少棵合法的树,设为 。枚举根的位置,得到递推式 。你发现这不是我们卡特兰数递推式吗,所以 。
我们断开一条边,一定是把原序列分为了一个子树和子树外的点。根据这个建树方法,我们容易发现任意一个子树都是序列上的一个区间。于是我们对于每个区间算贡献,设 为 的前缀异或和,则区间 的贡献为:
解释一下,首先区间内随便建树,然后找到原树上深度最深的子树包含 的点,这个点一定是区间内的子树的父亲。
于是我们得到了一个 的做法。
因为贡献是异或的,考虑拆位,然后 变成了一个 序列。
如果 , 一定为 ,也就是所有情况的每条边都会有 的贡献,所以这部分的贡献是 。
如果 ,当 时贡献为 ,否则贡献为 。设 为有多少个长度为 的区间满足 ,则答案为 。
现在考虑如何快速计算 。
一个 序列,这个性质非常好。我们把 的个数改为 的个数加上 的个数,这个直接做两遍差卷积即可。
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;
const int N=2e6+10,mod=998244353,g=3,ig=332748118;
inline int add(int a,int b){return ((a+=b)>=mod)?(a-mod):a;}
inline int norm(int a,int b){return ((a-=b)<0)?(a+=mod):a;}
inline int qpow(int a,int b){
int res=1;
while(b){
if(b&1)res=1ull*res*a%mod;
a=1ull*a*a%mod;
b>>=1;
}
return res;
}
int n,a[N],fac[N],inv[N],rev[N],c[N];
inline int C(int n,int m){
if(n<m)return 0;
return 1ull*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
inline void NTT(int limit,vector<int>&a,bool opt){
for(int i=0;i<limit;++i){
if(i<rev[i])
swap(a[i],a[rev[i]]);
}
for(int len=2;len<=limit;len<<=1){
int wn;
if(opt)wn=qpow(g,(mod-1)/len);
else wn=qpow(ig,(mod-1)/len);
for(int i=0;i<limit;i+=len)
for(int j=i,w=1;j<i+(len>>1);++j){
int res=1ull*w*a[j+(len>>1)]%mod;
a[j+(len>>1)]=norm(a[j],res);
a[j]=add(a[j],res);
w=1ull*w*wn%mod;
}
}
}
inline vector<int> mul(vector<int>f,vector<int>g){
int limit=1;
while(limit<(int)(f.size()+g.size()-1))limit<<=1;
for(int i=0;i<limit;++i)rev[i]=(rev[i>>1]>>1)|((i&1)?(limit>>1):0);
f.resize(limit);
g.resize(limit);
NTT(limit,f,1);
NTT(limit,g,1);
for(int i=0;i<limit;++i)f[i]=1ull*f[i]*g[i]%mod;
NTT(limit,f,0);
int inv=qpow(limit,mod-2);
for(int i=0;i<limit;++i)f[i]=1ull*f[i]*inv%mod;
return f;
}
inline int check(int k){
int res=0;
vector<int>f(n+1),g;
for(int i=1;i<=n;++i)f[i]=((a[i]>>k)&1)^f[i-1];
g=f;
reverse(g.begin(),g.end());
for(int i=0;i<=n;++i)g[i]^=1;
vector<int>h=mul(f,g);
for(int i=1;i<=n;++i)res=add(res,1ull*c[i]*c[n-i]%mod*h[n+i]%mod);
for(int i=0;i<=n;++i)f[i]^=1,g[i]^=1;
h=mul(f,g);
for(int i=1;i<=n;++i)res=add(res,1ull*c[i]*c[n-i]%mod*h[n+i]%mod);
return res;
}
inline void solve(){
cin>>n;
int S=0;
for(int i=1;i<=n;++i)cin>>a[i],S^=a[i];
int ans=0;
for(int j=0;j<18;++j){
if((S>>j)&1)ans=add(ans,(1ull<<j)*c[n]%mod*(n-1)%mod);
else ans=add(ans,(1ull<<(j+1))*check(j)%mod);
}
cout<<ans<<"\n";
}
signed main(){
fac[0]=inv[0]=1;
for(int i=1;i<N;++i)fac[i]=1ull*i*fac[i-1]%mod;
inv[N-1]=qpow(fac[N-1],mod-2);
for(int i=N-2;i>=1;--i)inv[i]=1ull*inv[i+1]*(i+1)%mod;
for(int i=0;i<=1e6;++i)c[i]=1ull*C(i<<1,i)*inv[i+1]%mod*fac[i]%mod;
int T;
cin>>T;
while(T--)solve();
return 0;
}时间复杂度 , 为 的值域。
然后交上去发现没过,做两遍差卷积的常数太大了(也可能是我写丑了?),考虑优化一遍卷积。
令 ,把 跟它自己做一遍差卷积,求出 代表长度为 的 的个数减 的个数,设 的个数为 , 的个数为 ,有如下式子:
直接解这个二元一次方程,然后拿 去算答案即可。这样我们只需要做一遍差卷积。
时间复杂度 。
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;
const int N=4e5+10,mod=998244353,g=3,ig=332748118;
inline int add(int a,int b){return ((a+=b)>=mod)?(a-mod):a;}
inline int norm(int a,int b){return ((a-=b)<0)?(a+=mod):a;}
inline int qpow(int a,int b){
int res=1;
while(b){
if(b&1)res=1ull*res*a%mod;
a=1ull*a*a%mod;
b>>=1;
}
return res;
}
int n,a[N],fac[N],inv[N],rev[N],c[N];
inline int C(int n,int m){
if(n<m)return 0;
return 1ull*fac[n]*inv[m]%mod*inv[n-m]%mod;
}
inline void NTT(int limit,vector<int>&a,bool opt){
for(int i=0;i<limit;++i){
if(i<rev[i])
swap(a[i],a[rev[i]]);
}
for(int len=2;len<=limit;len<<=1){
int wn;
if(opt)wn=qpow(g,(mod-1)/len);
else wn=qpow(ig,(mod-1)/len);
for(int i=0;i<limit;i+=len)
for(int j=i,w=1;j<i+(len>>1);++j){
int res=1ull*w*a[j+(len>>1)]%mod;
a[j+(len>>1)]=norm(a[j],res);
a[j]=add(a[j],res);
w=1ull*w*wn%mod;
}
}
}
inline void mul(vector<int>&f,vector<int>&g){
int limit=1;
while(limit<(int)(f.size()+g.size()-1))limit<<=1;
for(int i=0;i<limit;++i)rev[i]=(rev[i>>1]>>1)|((i&1)?(limit>>1):0);
f.resize(limit);
g.resize(limit);
NTT(limit,f,1);
NTT(limit,g,1);
for(int i=0;i<limit;++i)f[i]=1ull*f[i]*g[i]%mod;
NTT(limit,f,0);
int inv=qpow(limit,mod-2);
for(int i=0;i<limit;++i)f[i]=1ull*f[i]*inv%mod;
}
inline int check(int k){
int res=0;
vector<int>f(n+1),g;
for(int i=1;i<=n;++i)f[i]=((a[i]>>k)&1)^f[i-1];
for(int i=0;i<=n;++i){
if(!f[i])
f[i]=mod-1;
}
g=f;
reverse(g.begin(),g.end());
mul(f,g);
for(int i=1;i<=n;++i){
int a=n-i+1,b=f[i+n],x,y;
x=1ull*add(a,b)*((mod+1)>>1)%mod;
y=norm(a,x);
res=add(res,1ull*y*c[i]%mod*c[n-i]%mod);
}
return res;
}
inline void solve(){
cin>>n;
int S=0;
for(int i=1;i<=n;++i)cin>>a[i],S^=a[i];
int ans=0;
for(int j=0;j<18;++j){
if((S>>j)&1)ans=add(ans,(1ull<<j)*c[n]%mod*(n-1)%mod);
else ans=add(ans,(1ull<<(j+1))*check(j)%mod);
}
cout<<ans<<"\n";
}
signed main(){
fac[0]=inv[0]=1;
for(int i=1;i<N;++i)fac[i]=1ull*i*fac[i-1]%mod;
inv[N-1]=qpow(fac[N-1],mod-2);
for(int i=N-2;i>=1;--i)inv[i]=1ull*inv[i+1]*(i+1)%mod;
for(int i=0;i<=2e5;++i)c[i]=1ull*C(i<<1,i)*inv[i+1]%mod*fac[i]%mod;
int T;
cin>>T;
while(T--)solve();
return 0;
}