跳到正文

个人记录

「美团 CodeM 决赛」bt

Elegia约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月9日
篇幅
约 2 分钟
获赞
2

版本

  1. v1当前

简要题意:给定 n,mn,m,对所有 0≤k≤m0\le k\le m 求

∑T is binary tree,∣T∣=n∑u,v∈leaf⁡(T),u≤vlen⁡(u,v)k\sum_{T\text{ is binary tree} , |T|=n}\sum_{u,v\in \operatorname{leaf}(T),u\le v} \operatorname{len}(u,v)^k

(其中 len 是节点数量)

设 F,G,HF,G,H 是树的数量,对祖先路径的统计,对全体路径的统计,得

{F=x(1+F)2G=x(1+2(1+F)Get)H=x(et+2(1+F)H+G2e3t)\begin{cases} F = x(1+F)^2\\ G = x ( 1 + 2(1+F)G \mathrm{e}^t)\\ H = x ( \mathrm{e}^t + 2(1+F)H + G^2 \mathrm{e}^{3t}) \end{cases}

简单代换,有

H=xet1−4x+x3e3t1−4x(1−et1−4x)2H = \frac{x\mathrm{e}^t}{\sqrt{1-4x}} + \frac{x^3\mathrm{e}^{3t}}{\sqrt{1-4x} (1-\mathrm{e}^t\sqrt{1-4x})^2}

暂作换元 y=et−1y=\mathrm{e}^t-1,记 λ=1−4x\lambda=\sqrt{1-4x},有

H=xet/λ+x3(1+y)3λ(1−(y+1)(1−λ))2=xet/λ+x3(1+y)3λ3(1−(1/λ−1)y)2\begin{aligned} H &= x\mathrm{e}^t/\lambda + \frac{x^3(1+y)^3}{\lambda(1-(y+1)(1-\lambda))^2}\\ & = x\mathrm{e}^t/\lambda + \frac{x^3(1+y)^3}{\lambda^3(1-(1/\lambda - 1)y)^2} \end{aligned}

而 [xn]λ−k[x^n]\lambda^{-k} 很好计算。易得 O(T+m2)O(T+m^2) 的代码实现。同时易得 O(M(T)+R(m))O(\mathsf{M}(\sqrt T) + \mathsf{R}(m)) 的理论复杂度。

原文
存档评论1 条抓取于