CF Round250 E. The Child and Binary Tree

题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同。

也就是说:不带标号,孩子有序

\(n,m \le 10^5\)


sro vfk picks orz


和卡特兰数很像啊,\(f_i\)权值为i的方案数,递推式

\[f[i] = \sum_{i\in C} \sum_{j=0}^{m-i}f[j]f[n-i-j]
\]


用OGF表示他

\[C(x)=\sum_{i\in C}x^i
\]

表示一个点的生成函数;

\[F(x) = \sum_{i=0}^m f_i x^i
\]

表示二叉树的生成函数。

根据生成函数乘法的定义,

\[F(x) = F^2(x) C(x) + 1
\]

其中1是因为空子树。


二次函数化简+分子有理化后得到

\[F(x) = \frac{2}{1 \pm \sqrt{1 - 4C(x) }}
\]

正负号怎么取?

\(F(0) = f_0 = 1\),所以只能取+号


然后多项式开根+多项式求逆就行啦!

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = (1<<18) + 5;
inline int read(){
char c=getchar();int x=0,f=1;
while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*f;
}
int P = 998244353, g = 3, inv2 = (P+1)/2;
inline int Pow(ll a, int b) {
ll ans = 1;
for(; b; b>>=1, a=a*a%P)
if(b&1) ans=ans*a%P;
return ans;
} namespace ntt {
int maxlen = 1<<18, rev[N];
ll omega[N], omegaInv[N];
void dft(int *a, int n, int flag) {
for(int i=0; i<n; i++) if(i < rev[i]) swap(a[i], a[rev[i]]); for(int l=2; l<=n; l<<=1) {
int m = l>>1;
ll wn = Pow(g, flag==1 ? (P-1)/l : P-1-(P-1)/l);
for(int *p = a; p != a+n; p += l) {
ll w = 1;
for(int k=0; k<m; k++) {
int t = w * p[k+m] %P;
p[k+m] = (p[k] - t + P) %P;
p[k] = (p[k] + t) %P;
w = w * wn %P;
}
}
}
if(flag == -1) {
ll inv = Pow(n, P-2);
for(int i=0; i<n; i++) a[i] = a[i] * inv %P;
}
} int t[N];
void inverse(int *a, int *b, int l) {
if(l == 1) {b[0] = Pow(a[0], P-2); return;}
inverse(a, b, l>>1);
int n = 1, k = 0; while(n < l<<1) n <<= 1, k++;
for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1));
for(int i=0; i<l; i++) t[i] = a[i]; for(int i=l; i<n; i++) t[i] = 0;
dft(t, n, 1); dft(b, n, 1);
for(int i=0; i<n; i++) b[i] = (ll) b[i] * (2 - (ll) t[i] * b[i] %P + P) %P;
dft(b, n, -1);
for(int i=l; i<n; i++) b[i] = 0;
} int ib[N];
void square_root(int *a, int *b, int l) {
if(l == 1) {b[0] = 1; return;}
square_root(a, b, l>>1); int n = 1, k = 0; while(n < l<<1) n <<= 1, k++;
for(int i=0; i<n; i++) ib[i] = 0;
inverse(b, ib, l);
for(int i=0; i<n; i++) rev[i] = (rev[i>>1]>>1) | ((i&1)<<(k-1)); for(int i=0; i<l; i++) t[i] = a[i]; for(int i=l; i<n; i++) t[i] = 0;
dft(t, n, 1); dft(b, n, 1); dft(ib, n, 1);
for(int i=0; i<n; i++) b[i] = (ll) inv2 * (b[i] + (ll) t[i] * ib[i] %P) %P; dft(b, n, -1);
for(int i=l; i<n; i++) b[i] = 0;
}
} int n, m, c[N], a[N], f[N];
int main() {
freopen("in", "r", stdin);
n=read(); m=read()+1;
c[0] = 1;
for(int i=1; i<=n; i++) c[read()] -= 4;
for(int i=0; i<m; i++) if(c[i] < 0) c[i] += P; int len = 1; while(len < m) len <<= 1;
ntt::square_root(c, a, len);
a[0]++; if(a[0]>=P) a[0]-=P;
ntt::inverse(a, f, len);
for(int i=1; i<m; i++) printf("%d\n", f[i] * 2 %P);
}

Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]的更多相关文章

  1. [bzoj3625][Codeforces 250 E]The Child and Binary Tree(生成函数+多项式运算+FFT)

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 650  Solved: 28 ...

  2. Codeforces 438E. The Child and Binary Tree 多项式,FFT

    原文链接www.cnblogs.com/zhouzhendong/p/CF438E.html 前言 没做过多项式题,来一道入门题试试刀. 题解 设 $a_i$ 表示节点权值和为 $i$ 的二叉树个数, ...

  3. [题解] Codeforces 438 E The Child and Binary Tree DP,多项式,生成函数

    题目 首先令\(f_i\)表示权值和为\(i\)的二叉树数量,\(f_0=1\). 转移为:\(f_k=\sum_{i=0}^n \sum_{j=0}^{k-c_i}f_j f_{k-c_i-j}\) ...

  4. bzoj 3625(CF 438E)The Child and Binary Tree——多项式开方

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3625 http://codeforces.com/contest/438/problem/E ...

  5. 【CF438E】The Child and Binary Tree(多项式运算,生成函数)

    [CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...

  6. [codeforces438E]The Child and Binary Tree

    [codeforces438E]The Child and Binary Tree 试题描述 Our child likes computer science very much, especiall ...

  7. [题解] CF438E The Child and Binary Tree

    CF438E The Child and Binary Tree Description 给一个大小为\(n\)的序列\(C\),保证\(C\)中每个元素各不相同,现在你要统计点权全在\(C\)中,且 ...

  8. [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)

    [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...

  9. Codeforces 438E The Child and Binary Tree [DP,生成函数,NTT]

    洛谷 Codeforces 思路 看到计数和\(998244353\),可以感觉到这是一个DP+生成函数+NTT的题. 设\(s_i\)表示\(i\)是否在集合中,\(A\)为\(s\)的生成函数,即 ...

随机推荐

  1. BZOJ3916: [Baltic2014]friends

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3916 复习一下hash(然后被傻叉错误卡了半天TAT... 取出一个字串:h[r]-h[l-1 ...

  2. 史上最全最强Charles截取手机https协议数据包教程(附上利用此技术制作最近微信比较火的头脑王者辅助外挂)!

    纯原创,思路也是本人花了半个小时整理出来的,整个完成花费了本人半天时间,由于不才刚大学毕业,所以有的编码方面可能不入大牛们的眼,敬请原谅!如有转载请附上本地址,谢谢! 最近微信朋友圈刚刚被跳一跳血洗, ...

  3. 剪邮票dfs+bfs+组合+结构体

    #include<iostream>#include<queue>using namespace std;struct Point{ int x; int y; };queue ...

  4. tinyxml的封装与使用(转载)

    tinyxml是个高效精简的xml解析开源代码. 针对tinyxml直接使用对于对xml不是很熟悉的入门新手来说,有些概念难以理解,因此我将其封装后,供大家使用. 头文件: #include &quo ...

  5. Entity framework 中Where、First、Count等查询函数使用时要注意

    在.Net开发中,Entity framework是微软ORM架构的最佳官方工具.我们可以使用Lambda表达式在Entity framework中DbSet<T>类上直接做查询(比如使用 ...

  6. 什么是命名空间?php命名空间的基本应用分享

    什么是命名空间? php中声明的函数名.类名和常量的名称,在同一次运行中是不能重复的,否则会产生一个致命的错误,常见的解决方法是约定一个前缀.例如 ,在项目开发时,用户 User 模块中的控制器和数据 ...

  7. v-for并判断当前元素是否选中:$set实现响应添加属性

    前言 一直纠结着使用v-for进行列表渲染时如何为当前的元素添加是否选中的标识. 1.v-for进行列表渲染 <div class="lists"> <ul> ...

  8. Ajax 跨域,这应该是最全的解决方案了

    https://segmentfault.com/a/1190000012469713 前言 从刚接触前端开发起,跨域这个词就一直以很高的频率在身边重复出现,一直到现在,已经调试过N个跨域相关的问题了 ...

  9. nginx版本如何选择?

    生产环境使用Stable version:最新稳定版,现在最新的版本是nginx-1.8.1 注意各版本的区别:Nginx官网提供了三个类型的版本 1.Mainline version:Mainlin ...

  10. hive分区(partition)

    网上有篇关于hive的partition的使用讲解的比较好,转载了:一.背景1.在Hive Select查询中一般会扫描整个表内容,会消耗很多时间做没必要的工作.有时候只需要扫描表中关心的一部分数据, ...