遇事不决找规律:考虑 n 阶闪现树的生成函数!
注意:在第 i 步标红色的项就是在第 i+1 步生成新树的项!在某个节点生成新树,相当于在它上面乘了一个 (1+2x+4x2+x)。其中 4x2+x 是叶子节点,还能继续生成新树。
F1(x)=1+2x+4x2+x
F2(x)=1+2x+4x2(1+2x+4x2+x)+x(1+2x+4x2+x)=1+2x+(4x2+x)(1+2x)+(4x2+x)(4x2+x)。
F3(x)=1+2x+(4x2+x)(1+2x)+(4x2+x)[(4x2+x)(1+2x)+(4x2+x)(4x2+x)]=1+2x+(4x2+x)(1+2x)+(4x2+x)2(1+2x)+(4x2+x)2(4x2+x)。
你发现规律了吗?
就是 Fn(x)=(4x2+x)n+(1+2x)i=0∑n−1(4x2+x)i。后半部分套用等比数列求和公式,得到:
深度为 p 的答案为生成函数 Fn(x)=4x2+x−1(4x2+x)n−1(2x+1)+(4x2+x)n 中 xp 项的系数。
这个结论手玩一下应该能得到。对笔者来说难点在于在 O(n) 的时间内计算这个式子。
首先这个 (4x2+x)n 中 xp 的系数是 (p−nn)4p−n(p−n≥0)。这个应该毫无疑问。然后就是考虑剩下那一坨分式 G(x)=4x2+x−1(4x2+x)n−1(2x+1)。
记 A(x)=4x2+x−12x+1,于是记 G(x)=A(x)(4x2+x)n−A(x)。
考虑 A(x)=a0+a1x+a2x2+⋯,我们很有探究一下 an 的递推式或者通项公式的欲望,这里我们考察它的递推式。
由于 (4x2+x−1)A(x)=2x+1,我们展开 A(x),对比系数即可发现:
- a0=−1,a1=−3,an=an−1+4an−2(n≥2)。
然后那个单独的 A(x) 也可以消掉了。
再记 C(x)=A(x)(1+4x)n,同样考虑 C(x)=c0+c1x+c2x2+⋯,那么 G(x) 中 xp 的系数为 cp−n−ap。
接下来开始求解 cm。
根据 C(x)=A(x)(1+4x)n,得 (4x2+x−1)C(x)=(1+2x)(1+4x)n。然后展开左式和右式对比系数。这样做的目的是为了求解 cm 的递推式。
等式左边可化为 4∑m≥0cmxm+2+∑m≥0cmxm+1−∑m≥0cmxm。
等式右边可化为 (1+2x)∑j=0nbjxj=∑j=0nbjxj+2∑j=0nbjxj+1。(注:我们记 bj=(jn)4j。)
对比 x0,x1,xm 的系数可知,c0=−1,c1=−4n−3,cm=4cm−2+cm−1−(bm+2bm−1)(m≥2)。
接下来讲解算法流程:
- 计算 bj=(jn)4j。
- 用递推式计算 ai(i≤r)。
- 用递推式计算 ci(i≤n+1)。(观察等式的绿色部分,最高次数就是 n+1。)
- 对于每个 p,答案为 cp−n−ap+bp−n。
注意,当 p−n<0 时,规定 cp−n=bp−n=0。
代码有点丑,凑合着看吧。
C++#include<bits/stdc++.h>
using namespace std;
#define int long long
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define per(x,y,z) for(int x=y;x>=z;x--)
const int mod=998244353,N=1e7+7;
int ans,n,l,r,b[N],fac[N],invf[N],a[N<<1],c[N];
inline int fastmi(int a,int b){
int mul=1;
while(b){
if(b&1) mul=mul*a%mod;
a=a*a%mod;
b>>=1;
}
return mul;
}
inline int Cn(int tt){
return fac[n]*invf[tt]%mod*invf[n-tt]%mod;
}
void init(){
fac[0]=1;
rep(i,1,N-1) fac[i]=fac[i-1]*i%mod;
invf[N-1]=fastmi(fac[N-1],mod-2)%mod;
per(i,N-2,0) invf[i]=(i+1)*invf[i+1]%mod;
int pw4=1;
rep(j,0,n) b[j]=Cn(j)*pw4%mod,pw4*=4,pw4%=mod;
a[0]=mod-1,a[1]=mod-3;
rep(i,2,2*n+1){
int tmp1=4*a[i-2]%mod;
int tmp2=(tmp1+a[i-1])%mod;
a[i]=(mod+tmp2)%mod;
}
c[0]=mod-1,c[1]=mod-4*n-3;
rep(i,2,n+1){
int tmp1=4*c[i-2]%mod;
int tmp2=(b[i]+2*b[i-1]%mod)%mod;
int tmp3=(tmp1-tmp2+mod)%mod;
int tmp4=(tmp3+c[i-1]+mod)%mod;
c[i]=(mod+tmp4)%mod;
}
}
signed main()
{
cin>>n>>l>>r;
init();
rep(p,l,r){
int ctt=(p-n>=0)?c[p-n]:0,btt=(p-n>=0)?b[p-n]:0;
ans=ans^((2*mod+ctt-a[p]+btt)%mod);
}
cout<<ans<<"\n";
return 0;
}