令 fi,j,0/1f_{i,j,0/1}fi,j,0/1 表示在/不在 iii 的子树内,与 iii 距离模 100071000710007 余 jjj 的点的个数。
这个做法如果空间不爆掉,理论上可在 O(n×mod)O(n\times mod)O(n×mod) 的复杂度内解决该问题,卡常可能能过。
现在问题是如何处理空间?以及我推的转移是否正确?
fx,j,0=∑t∈sonxft,j−1,0+[j=0]f_{x,j,0}=\sum\limits_{t\in son_x}f_{t,j-1,0}+[j=0]fx,j,0=t∈sonx∑ft,j−1,0+[j=0]
对于 fx,j,1f_{x,j,1}fx,j,1 ,选择一个儿子,令其为 toptoptop 。
fx,j,1=ftop,j−1,1−∑t∈sonx&&t≠topft,j−3,0−[j=1]f_{x,j,1}=f_{top,j-1,1}-\sum\limits_{t\in son_x\&\&t\ne top}f_{t,j-3,0}-[j=1]fx,j,1=ftop,j−1,1−t∈sonx&&t=top∑ft,j−3,0−[j=1]