思路

设F(x)的第x项系数为权值和为x的答案

题目中要求权值必须在集合中出现,这个不好处理,考虑再设一个C,C的第x项如果是1代表x出现在值域里,如果是0,代表x没有出现在值域里,然后由于二叉树可以分别对左右子树处理,所以

\[F_k=\sum_{i=1}^k C_i \sum_{j=0}^{k-i}F_j F_{k-i-j}
\]

\[F_0=1
\]

可以看出这是一个卷积的形式

\[F=1+C*F*F
\]

然后解一个一元二次方程

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

因为\(C_0=0\),\(F_0=1\),所以去掉负号

然后上多项式求逆和多项式开方即可

代码

#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
using namespace std;
const int MAXN = 300000;
const int MOD = 998244353;
const int G = 3;
const int invG = 332748118;
struct Poly{
int t,data[MAXN];
Poly(){};
};
int pow(int a,int b){
int ans=1;
while(b){
if(b&1)
ans=(1LL*ans*a)%MOD;
a=(1LL*a*a)%MOD;
b>>=1;
}
return ans;
}
void NTT(Poly &a,int opt,int n){
int lim=0;
while((1<<(lim))<n){
lim++;
}
n=(1<<lim);
for(int i=0;i<n;i++){
int t=0;
for(int j=0;j<lim;j++)
if((i>>j)&1)
t|=(1<<(lim-j-1));
if(i<t)
swap(a.data[i],a.data[t]);
}
for(int i=2;i<=n;i<<=1){
int len=i/2;
int tmp=pow((opt)?G:invG,(MOD-1)/i);
for(int j=0;j<n;j+=i){
int arr=1;
for(int k=j;k<j+len;k++){
int t=(1LL*a.data[k+len]*arr)%MOD;
a.data[k+len]=(1LL*a.data[k]-t+MOD)%MOD;
a.data[k]=(1LL*a.data[k]+t)%MOD;
arr=(1LL*arr*tmp)%MOD;
}
}
}
if(!opt){
int invN = pow(n,MOD-2);
for(int i=0;i<n;i++){
a.data[i]=(1LL*a.data[i]*invN)%MOD;
}
}
}
void save(Poly &a,int top){
for(int i=top+1;i<=a.t;i++)
a.data[i]=0;
a.t=top;
}
void mul(Poly &a,Poly b){//a=a*b
int num=a.t+b.t,lim=0;
while((1<<lim)<=(num+2))
lim++;
lim=(1<<lim);
NTT(a,1,lim);
NTT(b,1,lim);
for(int i=0;i<lim;i++)
a.data[i]=(1LL*a.data[i]*b.data[i])%MOD;
NTT(a,0,lim);
a.t=num;
for(int i=num+1;i<lim;i++)
a.data[i]=0;
}
void Inv(Poly a,Poly &b,int dep,int &midlen){
if(dep==1){
b.data[0]=pow(a.data[0],MOD-2);
b.t=dep-1;
return;
}
Inv(a,b,(dep+1)>>1,midlen);
static Poly tmp;
while((dep<<1)>midlen)
midlen<<=1;
for(int i=0;i<dep;i++)
tmp.data[i]=a.data[i];
for(int i=dep;i<midlen;i++)
tmp.data[i]=0;
NTT(tmp,1,midlen);
NTT(b,1,midlen);
for(int i=0;i<midlen;i++)
b.data[i]=1LL*b.data[i]*((2-1LL*tmp.data[i]*b.data[i])%MOD+MOD)%MOD;
NTT(b,0,midlen);
for(int i=dep;i<midlen;i++)
b.data[i]=0;
b.t=dep-1;
}
void sqrt(Poly a,Poly &b,int &midlen,int dep){
if(dep==1){
b.data[0]=1;
b.t=dep-1;
return;
}
sqrt(a,b,midlen,(dep+1)>>1);
while((dep<<1)>(midlen))
midlen<<=1;
static Poly tmp1,tmp2,tmp3;
tmp1=b;tmp3=b;
save(tmp1,dep-1);
save(tmp2,-1);
save(tmp3,dep-1);
int midlent=1;
for(int i=0;i<dep;i++)
tmp1.data[i]=(tmp1.data[i]*2)%MOD;
Inv(tmp1,tmp2,dep,midlent);
mul(b,tmp3);
for(int i=0;i<dep;i++)
b.data[i]=(b.data[i]+a.data[i])%MOD;
mul(b,tmp2);
for(int i=dep;i<midlen;i++)
b.data[i]=0;
b.t=dep-1;
}
Poly c,C;
int n,m;
signed main(){
scanf("%lld %lld",&n,&m);
c.t=100000;
for(int i=1;i<=n;i++){
int x;
scanf("%lld",&x);
c.data[x]=1;
}
for(int i=0;i<=c.t;i++)
c.data[i]=((-4LL*c.data[i])%MOD+MOD)%MOD;
c.data[0]=(1+c.data[0])%MOD;
int midlen=1;
sqrt(c,C,midlen,c.t+1);
C.data[0]=(1+C.data[0])%MOD;
midlen=1;
save(c,-1);
Inv(C,c,C.t+1,midlen);
for(int i=1;i<=m;i++)
printf("%lld\n",(c.data[i]*2)%MOD);
return 0;
}

CF438E The Child and Binary Tree的更多相关文章

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

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

  2. CF438E The Child and Binary Tree 生成函数、多项式开根

    传送门 设生成函数\(C(x) = \sum\limits_{i=0}^\infty [\exists c_j = i]x^i\),答案数组为\(f_1 , f_2 , ..., f_m\),\(F( ...

  3. cf438E. The Child and Binary Tree(生成函数 多项式开根 多项式求逆)

    题意 链接 Sol 生成函数博大精深Orz 我们设\(f(i)\)表示权值为\(i\)的二叉树数量,转移的时候可以枚举一下根节点 \(f(n) = \sum_{w \in C_1 \dots C_n} ...

  4. CF438E The Child and Binary Tree(生成函数,NTT)

    题目链接:洛谷 CF原网 题目大意:有 $n$ 个互不相同的正整数 $c_i$.问对于每一个 $1\le i\le m$,有多少个不同形态(考虑结构和点权)的二叉树满足每个点权都在 $c$ 中出现过, ...

  5. CF438E The Child and Binary Tree(生成函数+多项式开根+多项式求逆)

    传送门 可以……这很多项式开根模板……而且也完全不知道大佬们怎么把这题的式子推出来的…… 首先,这题需要多项式开根和多项式求逆.多项式求逆看这里->这里,这里讲一讲多项式开根 多项式开方:已知多 ...

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

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

  7. Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]

    CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...

  8. [codeforces438E]The Child and Binary Tree

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

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

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

随机推荐

  1. 远程连接ubuntu mysql出现2003错误 cant connect to mysql(转载)

    不多说直接上代码 1.在控制台输入,进入mysql目录下, sudo su //进入root权限 cd /etc/mysql 2.打开my.cnf文件,找到 bind-address = 127.0. ...

  2. 序列&权限&索引&视图的语句

    create sequence 订单_订单编号_seq -- 创建序列 (成功后在sequence中查询) increment by start with maxvalue nocycle nocac ...

  3. day20 二十、加密模块、操作配置文件、操作shell命令、xml模块

    一.加密模块 1.hashlib模块:加密 ①有解密的加密方式 ②无解密的加密方式:碰撞检查 -- 1)不同数据加密后的结果一定不一致 -- 2)相同数据的加密结果一定是一致的 import hash ...

  4. Codeforces 1099 - A/B/C/D/E/F - (Done)

    链接:https://codeforces.com/contest/1099 A - Snowball - [模拟水题] 题意:有一个雪球从山顶滚落,山坡上有两块石头,每秒钟会一次发生三件事:1.雪球 ...

  5. python_打包成exe

    1. 安装pyinstaller pip install pyinstaller 或通过国内镜像下载(较快): pip install pyinstaller -i http://pypi.douba ...

  6. 从光盘安装ubuntu系统

    参考博客: https://www.jianshu.com/p/7929e4911206

  7. py三个面试小问题

    1.是否遇到过Python的模块间循环引用的问题,如何避免它? 这是代码结构设计的问题,模块依赖和类依赖,如果老是觉得碰到循环引用可能的原因有几点: a.可能是模块的分界线划错地方了 b.可能是把应该 ...

  8. opencart3如何安装模板

    opencart 3模板采用twig模式,安装模板也有点不大一样,随ytkah一起来看看opencart3如何安装模板吧1.下载模板文件,用ftp上传到对应的位置,一般有几个文件夹,比如:admin. ...

  9. Unity3D加密保护解决方案

    精锐5加密锁支持Unity3D代码及资源保护,并提供授权方案 产品简介 可使用Virbox Protector加壳工具对Unity3D代码进行加密.Unity3D使用开源mono C#语法,代码会编译 ...

  10. BMC ipmitool 对linux服务器进行IPMI管理

    IPMI是智能型平台管理接口(Intelligent Platform Management Interface)的缩写,是管理基于 Intel结构的企业系统中所使用的外围设备采用的一种工业标准,该标 ...