[CF438E] The Child and Binary Tree
link $solution:$ 考虑朴素 $dp$,设 $f_i$ 为权值为 $i$ 的二叉树个数,$C_i$ 表示 $i$ 出现的次数。 则 $$f_i=\sum_{j=0}^i C_j\sum_{k=0}^{i-j} f_k\times f_{i-j-k}$$ 时间复杂度 $O(n^3)$ 。 而简单思考发现 $j+k+(i-j-k)=i$ ,其实整个 $dp$ 过程是三个式子的卷积。 考虑将$f,c$ 写成生成函数。 则 $$F=1+C\times F^2$$ $+1$ 是因为 $f_0=1$ 。 则 $F=\dfrac{2}{1+\sqrt{1-4F}}$ 。 直接多项式操作即可,时间复杂度 $O(n\log n)$ 。 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #define int long long #define mod 998244353 using namespace std; inline int read(){ int f=1,ans=0;char c=getchar(); while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){ans=ans*10+c-'0';c