跳到正文

题解

题解:P15380 闪现树 / grood

Amoribus约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年2月27日
更新
2026年10月28日
篇幅
约 6 分钟
题目
P15380

版本

  1. v1当前

遇事不决找规律:考虑 nn 阶闪现树的生成函数!

注意:在第 ii 步标红色的项就是在第 i+1i+1 步生成新树的项!在某个节点生成新树,相当于在它上面乘了一个 (1+2x+4x2+x)(1+2x+4x^2+x)。其中 4x2+x4x^2+x 是叶子节点,还能继续生成新树。

F1(x)=1+2x+4x2+xF_1(x)=1+2x+\red{4x^2+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)F_2(x)=1+2x+4x^2(1+2x+\red{4x^2+x})+x(1+2x+\red{4x^2+x})=1+2x+(4x^2+x)(1+2x)+(4x^2+x)\red{(4x^2+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)F_3(x)=1+2x+(4x^2+x)(1+2x)+(4x^2+x)[(4x^2+x)(1+2x)+(4x^2+x)\red{(4x^2+x)}]=1+2x+(4x^2+x)(1+2x)+(4x^2+x)^2(1+2x)+(4x^2+x)^2\red{(4x^2+x)}。

你发现规律了吗?

就是 Fn(x)=(4x2+x)n+(1+2x)∑i=0n−1(4x2+x)iF_n(x)=(4x^2+x)^n+(1+2x)\sum\limits_{i=0}^{n-1}(4x^2+x)^i。后半部分套用等比数列求和公式,得到:

深度为 pp 的答案为生成函数 Fn(x)=(4x2+x)n−14x2+x−1(2x+1)+(4x2+x)nF_n(x)=\dfrac{(4x^2+x)^n-1}{4x^2+x-1}(2x+1)+(4x^2+x)^n 中 xpx^p 项的系数。

这个结论手玩一下应该能得到。对笔者来说难点在于在 O(n)O(n) 的时间内计算这个式子。

首先这个 (4x2+x)n(4x^2+x)^n 中 xpx^p 的系数是 (np−n)4p−n(p−n≥0)\binom{n}{p-n}4^{p-n}(p-n\geq0)。这个应该毫无疑问。然后就是考虑剩下那一坨分式 G(x)=(4x2+x)n−14x2+x−1(2x+1)G(x)=\dfrac{(4x^2+x)^n-1}{4x^2+x-1}(2x+1)。

记 A(x)=2x+14x2+x−1A(x)=\dfrac{2x+1}{4x^2+x-1},于是记 G(x)=A(x)(4x2+x)n−A(x)G(x)=A(x)(4x^2+x)^n-A(x)。

考虑 A(x)=a0+a1x+a2x2+⋯A(x)=a_0+a_1x+a_2x^2+\cdots,我们很有探究一下 ana_n 的递推式或者通项公式的欲望,这里我们考察它的递推式。

由于 (4x2+x−1)A(x)=2x+1(4x^2+x-1)A(x)=2x+1,我们展开 A(x)A(x),对比系数即可发现:

  • a0=−1,a1=−3,an=an−1+4an−2(n≥2)a_0=-1,a_1=-3,a_n=a_{n-1}+4a_{n-2}(n\geq 2)。

然后那个单独的 A(x)A(x) 也可以消掉了。

再记 C(x)=A(x)(1+4x)nC(x)=A(x)(1+4x)^n,同样考虑 C(x)=c0+c1x+c2x2+⋯C(x)=c_0+c_1x+c_2x^2+\cdots,那么 G(x)G(x) 中 xpx^p 的系数为 cp−n−apc_{p-n}-a_p。

接下来开始求解 cmc_m。

根据 C(x)=A(x)(1+4x)nC(x)=A(x)(1+4x)^n,得 (4x2+x−1)C(x)=(1+2x)(1+4x)n\red{(4x^2+x-1)C(x)}=\green{(1+2x)(1+4x)^n}。然后展开左式和右式对比系数。这样做的目的是为了求解 cmc_m 的递推式。

等式左边可化为 4∑m≥0cmxm+2+∑m≥0cmxm+1−∑m≥0cmxm\red{4\sum_{m\ge0} c_m x^{m+2} + \sum_{m\ge0} c_m x^{m+1} - \sum_{m\ge0} c_m x^m}。

等式右边可化为 (1+2x)∑j=0nbjxj=∑j=0nbjxj+2∑j=0nbjxj+1\green{(1+2x)\sum_{j=0}^n b_j x^j = \sum_{j=0}^n b_j x^j + 2\sum_{j=0}^n b_j x^{j+1}}。(注:我们记 bj=(nj)4j\green{b_j}=\binom{n}{j}4^j。)

对比 x0,x1,xmx^0,x^1,x^m 的系数可知,c0=−1,c1=−4n−3,cm=4cm−2+cm−1−(bm+2bm−1)(m≥2)c_0=-1,c_1=-4n-3,c_m=4c_{m-2}+c_{m-1}-(b_m+2b_{m-1})(m\geq 2)。

接下来讲解算法流程:

  • 计算 bj=(nj)4jb_j=\binom{n}{j}4^j。
  • 用递推式计算 ai(i≤r)a_i(i\leq r)。
  • 用递推式计算 ci(i≤n+1)c_i(i\leq n+1)。(观察等式的绿色部分,最高次数就是 n+1n+1。)
  • 对于每个 pp,答案为 cp−n−ap+bp−nc_{p-n}-a_p+b_{p-n}。

注意,当 p−n<0p-n<0 时,规定 cp−n=bp−n=0c_{p-n}=b_{p-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;
}
原文