Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
CF Round250 E. The Child and Binary Tree
题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同。
也就是说:不带标号,孩子有序
\(n,m \le 10^5\)
sro vfk picks orz
和卡特兰数很像啊,\(f_i\)权值为i的方案数,递推式
\]
用OGF表示他
\]
表示一个点的生成函数;
\]
表示二叉树的生成函数。
根据生成函数乘法的定义,
\]
其中1是因为空子树。
二次函数化简+分子有理化后得到
\]
正负号怎么取?
\(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 [多项式开根 生成函数]的更多相关文章
- [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 ...
- Codeforces 438E. The Child and Binary Tree 多项式,FFT
原文链接www.cnblogs.com/zhouzhendong/p/CF438E.html 前言 没做过多项式题,来一道入门题试试刀. 题解 设 $a_i$ 表示节点权值和为 $i$ 的二叉树个数, ...
- [题解] 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}\) ...
- 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 ...
- 【CF438E】The Child and Binary Tree(多项式运算,生成函数)
[CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...
- [codeforces438E]The Child and Binary Tree
[codeforces438E]The Child and Binary Tree 试题描述 Our child likes computer science very much, especiall ...
- [题解] CF438E The Child and Binary Tree
CF438E The Child and Binary Tree Description 给一个大小为\(n\)的序列\(C\),保证\(C\)中每个元素各不相同,现在你要统计点权全在\(C\)中,且 ...
- [BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆)
[BZOJ 3625] [Codeforces 438E] 小朋友的二叉树 (DP+生成函数+多项式开根+多项式求逆) 题面 一棵二叉树的所有点的点权都是给定的集合中的一个数. 让你求出1到m中所有权 ...
- Codeforces 438E The Child and Binary Tree [DP,生成函数,NTT]
洛谷 Codeforces 思路 看到计数和\(998244353\),可以感觉到这是一个DP+生成函数+NTT的题. 设\(s_i\)表示\(i\)是否在集合中,\(A\)为\(s\)的生成函数,即 ...
随机推荐
- python算法运算
>>> b = 10>>> b /= 8>>> b1.25>>> 10 // 81>>> **幂运算 > ...
- Dora.Interception, 为.NET Core度身打造的AOP框架[4]:演示几个典型应用
为了帮助大家更深刻地认识Dora.Interception,并更好地将它应用到你的项目中,我们通过如下几个简单的实例来演示几个常见的AOP应用在Dora.Interception下的实现.对于下面演示 ...
- SVN版本库修改URL路径或者IP地址
服务器的IP地址或者URL变更,版本库服务器的IP也要修改,因为当初安装SVN URL没有使用别名,所以使用的人都要修改客户端的IP,以下是参考网上的资料. 1.Windows TortoiseSVN ...
- [原创]消灭eclipse中运行启动的错误:“找不到或无法加载主类”问题
最近一直遇到这个问题且根据网上的文章做法基本无法通过,故将自己的解决步骤记录及分享给大家. 一:环境必须要配置好. 试试在dos界面输入:java.javac 分别这两个命令是否能执行,如果都能执行恭 ...
- mysql 安装以及配置
MySQL是一个小巧玲珑但功能强大的数据库,目前十分流行.但是官网给出的安装包有两种格式,一个是msi格式,一个是zip格式的.很多人下了zip格式的解压发现没有setup.exe,面对一堆文件一头雾 ...
- 注释中不允许出现字符串 "--"。
问题: 在启动tomcat时会出现如上错误,同时有可能会出现xml无法解析等错误 解决办法: 注释中不能出现字符串 "--",即需要把xml文件中多余的“--”去掉,例如: < ...
- python与MySQL
一.python与mysql交互 因版本不同python操作mysql有两个模块,python3不再支持MySQL-python,模块使用都一样: python2.7:MySQL-python pyt ...
- salesforce零基础学习(八十五)streaming api 简单使用(接近实时获取你需要跟踪的数据的更新消息状态)
Streaming API参考链接: https://trailhead.salesforce.com/en/modules/api_basics/units/api_basics_streaming ...
- 使用 ServiceAnt 更好地解耦你的程序
今天要厚着脸皮给大家推荐一个自己做的通信中间件--ServiceAnt,目前已经在我们团队的两个产品线上投入了使用. ServiceAnt是什么 它最初的定位是ESB(企业服务总线),但目前还没有达到 ...
- Java的NIO
1. 基本 概念 IO 是主存和外部设备 ( 硬盘.终端和网络等 ) 拷贝数据的过程. IO 是操作系统的底层功能实现,底层通过 I/O 指令进行完成. 所有语言运行时系统提供执行 I/O 较高级 ...