题解:CF2268E Kia Kio and Tree of Life

· · 题解

\text{Link}

思路

首先算给你 n 个点有多少棵合法的树,设为 C_i。枚举根的位置,得到递推式 C_i=\sum_{i=1}^n C_{i-1}C_{n-i}。你发现这不是我们卡特兰数递推式吗,所以 C_i=\frac 1 {n+1}\binom{2n}{n}。

我们断开一条边,一定是把原序列分为了一个子树和子树外的点。根据这个建树方法,我们容易发现任意一个子树都是序列上的一个区间。于是我们对于每个区间算贡献,设 b_i 为 a_i 的前缀异或和,则区间 [l,r] 的贡献为:

C_{r-l+1}C_{n-(r-l+1)}((b_r \operatorname{xor} b_{l-1})+(b_n \operatorname{xor} b_r \operatorname{xor} b_{l-1}))

解释一下,首先区间内随便建树,然后找到原树上深度最深的子树包含 [l,r] 的点,这个点一定是区间内的子树的父亲。

于是我们得到了一个 O(n^2) 的做法。

因为贡献是异或的,考虑拆位,然后 b 变成了一个 01 序列。

b_n=1

如果 b_n=1,((b_r \operatorname{xor} b_{l-1})+(b_n \operatorname{xor} b_r \operatorname{xor} b_{l-1})) 一定为 1,也就是所有情况的每条边都会有 1 的贡献,所以这部分的贡献是 C_n (n-1)。

b_n=0

如果 b_n=0,当 b_{l-1}=b_r 时贡献为 0,否则贡献为 2。设 f_i 为有多少个长度为 i 的区间满足 b_{l-1} \ne b_r,则答案为 \sum_{i=1}^n f_i C_i C_{n-i+1}。

现在考虑如何快速计算 f_i。

::::info[Code] ```cpp #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; } ``` :::: 时间复杂度 $O(n \log n \log V)$,$V$ 为 $a$ 的值域。 然后交上去发现没过,做两遍差卷积的常数太大了(也可能是我写丑了?),考虑优化一遍卷积。 令 $c_i=(-1)^{[b_i=0]}$,把 $c$ 跟它自己做一遍差卷积,求出 $g_i$ 代表长度为 $i$ 的 $b_{l-1}=b_r$ 的个数减 $b_{l-1} \ne b_r$ 的个数,设$b_{l-1}=b_r$ 的个数为 $x$,$b_{l-1} \ne b_r$ 的个数为 $y$,有如下式子: $$ \begin{cases} x+y=n-i+1\\ x-y=g_i \end{cases} $$ 直接解这个二元一次方程,然后拿 $y$ 去算答案即可。这样我们只需要做一遍差卷积。 时间复杂度 $O(n \log n \log V)$。 ::::info[Code] ```cpp #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; } ``` ::::