[题解] 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}\)
令多项式\(D=\sum_{i=0}^m [i在c中出现过]x^i\),\(F(x)为f的普通生成函数\),根据转移式发现F其实等于F卷积上F再卷积上D,再加上一个1,因为转移式转移不到\(f_0\)。
所以
F&=F^2D+1\\
DF^2-F+1&=0\\
D^2F^2-DF+D&=0\\
(DF-\frac12)^2+D-\frac14&=0(配方)\\
(DF-\frac12)^2&=\frac14-D\\
DF-\frac12&=\pm \sqrt{\frac14-D}\\
\end{align}
\]
有两解,但是\(f_i\)肯定只有一种可能啊所以肯定有一个解不合法。
当取正号时:
\(DF-\frac12=\sqrt{\frac14-D}\),\(DF=\frac{\sqrt{1-4D}+1}{2}\)
等式右边的常数项为1,但是\(D\)的常数项为0,\(F\)的常数项为1,所以\(DF\)的常数项为0,矛盾。
所以只能取负号。
DF&=\frac{1-\sqrt{1-4D}}{2}\\
F&=\frac{(1-\sqrt{1-4D})(1+\sqrt{1-4D})}{2D(1+\sqrt{1-4D})}\\
F&=\frac{2}{1+\sqrt{1-4D}}\\
\end{align}\\
\]
接下来直接多项式开根+求逆就行了。时间复杂度\(O(n logn)\)。
小知识:多项式能求逆的充要条件是常数项不为0,常数项>0则一定能开根。
点击查看代码
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define repn(i,n) for(int i=1;i<=n;++i)
#define LL long long
#define pii pair <LL,LL>
#define fi first
#define se second
#define mpr make_pair
#define pb push_back
using namespace std;
const LL MOD=998244353;
LL qpow(LL x,LL a)
{
LL res=x,ret=1;
while(a>0)
{
if((a&1)==1) ret=ret*res%MOD;
a>>=1;
res=res*res%MOD;
}
return ret;
}
namespace poly
{
vector <LL> rev;
void ntt(vector <LL> &a,LL G)
{
LL nn=a.size(),gn,g,x,y;vector <LL> tmp=a;
rep(i,nn) a[i]=tmp[rev[i]];
for(int len=1;len<nn;len<<=1)
{
gn=qpow(G,(MOD-1)/(len<<1));
for(int i=0;i<nn;i+=(len<<1))
{
g=1;
for(int j=i;j<i+len;++j,(g*=gn)%=MOD)
{
x=a[j];y=a[j+len]*g%MOD;
a[j]=(x+y)%MOD;a[j+len]=(x-y+MOD)%MOD;
}
}
}
}
vector <LL> convolution(vector <LL> a,vector <LL> b,LL G)
{
LL nn=1,bt=0,sv=a.size()+b.size()-1;while(nn<a.size()+b.size()-1) nn<<=1LL,++bt;
while(a.size()<nn) a.pb(0);while(b.size()<nn) b.pb(0);
rev.clear();
rep(i,nn)
{
rev.pb(0);
rev[i]=(rev[i>>1]>>1)|((i&1)<<(bt-1));
}
ntt(a,G);ntt(b,G);
rep(i,nn) (a[i]*=b[i])%=MOD;
ntt(a,qpow(G,MOD-2));
while(a.size()>sv) a.pop_back();
LL inv=qpow(nn,MOD-2);
rep(i,a.size()) (a[i]*=inv)%=MOD;
return a;
}
vector <LL> inverse(vector <LL> a,LL G)
{
if(a.size()==1) return vector <LL>{qpow(a[0],MOD-2)};
vector <LL> aa=a;while(aa.size()>(a.size()+1)>>1) aa.pop_back();
vector <LL> bb=inverse(aa,G);
LL nn=1,bt=0,sv=a.size();while(nn<a.size()*2) nn<<=1LL,++bt;
while(a.size()<nn) a.pb(0);while(bb.size()<nn) bb.pb(0);
rev.clear();
rep(i,nn)
{
rev.pb(0);
rev[i]=(rev[i>>1]>>1)|((i&1)<<(bt-1));
}
ntt(a,G);ntt(bb,G);
rep(i,nn) a[i]=(2LL-a[i]*bb[i]%MOD+MOD)*bb[i]%MOD;
ntt(a,qpow(G,MOD-2));
while(a.size()>sv) a.pop_back();
LL inv=qpow(nn,MOD-2);
rep(i,a.size()) (a[i]*=inv)%=MOD;
return a;
}
vector <LL> sqrt1(vector <LL> a,LL G)//常数项为1
{
if(a.size()==1) return vector <LL>{1};
vector <LL> aa=a;while(aa.size()>(a.size()+1)>>1) aa.pop_back();
vector <LL> bb=sqrt1(aa,G);while(bb.size()<a.size()) bb.pb(0);
vector <LL> bbb=inverse(bb,G);
LL nn=1,bt=0,sv=a.size();while(nn<a.size()*2) nn<<=1LL,++bt;
while(a.size()<nn) a.pb(0);while(bb.size()<nn) bb.pb(0);while(bbb.size()<nn) bbb.pb(0);
rev.clear();
rep(i,nn)
{
rev.pb(0);
rev[i]=(rev[i>>1]>>1)|((i&1)<<(bt-1));
}
LL mul=qpow(2,MOD-2);
ntt(a,G);ntt(bb,G);ntt(bbb,G);
rep(i,nn) a[i]=mul*(bb[i]+bbb[i]*a[i]%MOD)%MOD;
ntt(a,qpow(G,MOD-2));
while(a.size()>sv) a.pop_back();
LL inv=qpow(nn,MOD-2);
rep(i,a.size()) (a[i]*=inv)%=MOD;
return a;
}
}
LL n,m;
vector <LL> c;
int main()
{
cin>>n>>m;
rep(i,100001) c.pb(0);
LL x;
rep(i,n)
{
scanf("%lld",&x);
c[x]=1;
}
c[0]=1;
repn(i,100000) c[i]=MOD-c[i]*4LL;
c=poly::sqrt1(c,3);
(c[0]+=1LL)%=MOD;
c=poly::inverse(c,3);
rep(i,c.size()) (c[i]+=c[i])%=MOD;
repn(i,m) printf("%lld\n",c[i]);
return 0;
}
[题解] Codeforces 438 E The Child and Binary Tree DP,多项式,生成函数的更多相关文章
- Codeforces 438E The Child and Binary Tree [DP,生成函数,NTT]
洛谷 Codeforces 思路 看到计数和\(998244353\),可以感觉到这是一个DP+生成函数+NTT的题. 设\(s_i\)表示\(i\)是否在集合中,\(A\)为\(s\)的生成函数,即 ...
- 【CF438E】The Child and Binary Tree(多项式运算,生成函数)
[CF438E]The Child and Binary Tree(多项式运算,生成函数) 题面 有一个大小为\(n\)的集合\(S\) 问所有点权都在集合中,并且点权之和分别为\([0,m]\)的二 ...
- Codeforces 250 E. The Child and Binary Tree [多项式开根 生成函数]
CF Round250 E. The Child and Binary Tree 题意:n种权值集合C, 求点权值和为1...m的二叉树的个数, 形态不同的二叉树不同. 也就是说:不带标号,孩子有序 ...
- [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 ...
- [题解] CF438E The Child and Binary Tree
CF438E The Child and Binary Tree Description 给一个大小为\(n\)的序列\(C\),保证\(C\)中每个元素各不相同,现在你要统计点权全在\(C\)中,且 ...
- [codeforces438E]The Child and Binary Tree
[codeforces438E]The Child and Binary Tree 试题描述 Our child likes computer science very much, especiall ...
- [LeetCode]题解(python):114 Flatten Binary Tree to Linked List
题目来源 https://leetcode.com/problems/flatten-binary-tree-to-linked-list/ Given a binary tree, flatten ...
- [LeetCode]题解(python):110 Balanced Binary Tree
题目来源 https://leetcode.com/problems/balanced-binary-tree/ Given a binary tree, determine if it is hei ...
- Codeforces 219D Choosing Capital for Treeland:Tree dp
题目链接:http://codeforces.com/problemset/problem/219/D 题意: 给你一棵树,n个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
随机推荐
- Hadoop-HA 搭建高可用集群Hadoop Zookeeper
Hadoop Zookeeper 搭建(一) 一.准备工作 VMWARE虚拟机 CentOS 7 系统 虚拟机1:master 虚拟机2:node1 虚拟机3:node2 时间同步 ntpdate n ...
- BootStrapBlazor 安装教程--Server模式
使用模板 使用模板是最简单的办法.因为项目模板里已经包含了BootStrapBlazor的所有需要配置的内容. 首先我们安装项目模板: dotnet new -i Bootstrap.Blazor.T ...
- mysql show操作
SHOW CHARACTER SET 显示所有可用的字符集 SHOW CHARACTER SET; SHOW CHARACTER SET LIKE 'latin%'; SHOW COLLATION 输 ...
- 【原创】Python 极验滑块验证
本文仅供学习交流使用,如侵立删! 记一次 极验滑块验证分析并通过 操作环境 win10 . mac Python3.9 selenium.seleniumwire 分析 最近在做的一个项目登录时会触发 ...
- 从C过渡到C++(1)——GNU/Linux
从C过渡到C++(1)--GNU/Linux 目录 从C过渡到C++(1)--GNU/Linux 大名鼎鼎的GNU/Linux GNU GNU的组成 一点补充 MinGW 运行时库 额外的内容 Min ...
- React报错之React Hook 'useEffect' is called in function
正文从这开始~ 总览 为了解决错误"React Hook 'useEffect' is called in function that is neither a React function ...
- Linux 禁止root远程登录解决办法
linux中root用户是超级管理员,可以针对root用户暴力破解密码,这样很不安全,工作中我们一般禁止root用户直接远程登陆,开设一个或多个普通用户,只允许登陆普通用户,如果有需要用root用户, ...
- Windows批量修改文件
如图我是建立了壁纸文件夹 Windows自带的排序方式 如何不用自带的呢? 在这个文件夹里面建一个.txt文件 如下 ok第二步骤 将UTF-8格式改为ANSI格式 点击文件-另存为ANSI格式-替换 ...
- [Golang] cgo 调用 .so 捕获异常问题
最近需要在 go 中去调用 .so 库去完成一些事情,go 方面,利用 cgo 可以顺利的调用 .so 中的方法,但是有个问题是 go 没法捕获 .so 那边出现的异常.如果 .so 那边异常了,那么 ...
- setContentView源码分析
当我们在Activity中调用setContentView,它到底做了什么呢 override fun onCreate(savedInstanceState: Bundle?) { super.on ...