[题解] 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个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
随机推荐
- 吐泡泡_via牛客网
题目 链接:https://ac.nowcoder.com/acm/contest/28537/E 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...
- pinia 入门及使用
自上月从上海结束工作回来 在家闲来无事 想写点东西打发时间 也顺便学习学习新的技术.偶然发现了 pinia 据说比vuex好用些 所以便搭了个demo尝试着用了下 感觉确实不错,于是便有了这篇随笔. ...
- Stream流的特点_只能使用一次和Stream流中的常用方法_map
Stream流的特点_只能使用一次 public class FilterStudy04 { public static void main(String[] args) { //创建一个Stream ...
- 别再用 System.currentTimeMillis 统计耗时了,太 Low,试试 Spring Boot 源码在用的 StopWatch吧,够优雅!
大家好,我是二哥呀! 昨天,一位球友问我能不能给他解释一下 @SpringBootApplication 注解是什么意思,还有 Spring Boot 的运行原理,于是我就带着他扒拉了一下这个注解的源 ...
- linux学习(小白篇)
当前服务器:centos 7 shell命令框:xshell 文件预览及上传:xftp (界面化软件,非常好用) 数据库连接:navicat 此文是在学习linux时做一个指令合集,方便自己查阅 进文 ...
- python base64编码和解码图片
简介 在实际项目中,可能需要对图片进行大小的压缩,较为常见的方法则是将图片转换为base64的编码,本文就python编码和解码图片做出一定的介绍. 代码 import base64 import o ...
- 利用Docker挂载Nginx-rtmp(服务器直播流分发)+FFmpeg(推流)+Vue.js结合Video.js(播放器流播放)来实现实时网络直播
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_75 众所周知,在视频直播领域,有不同的商家提供各种的商业解决方案,其中比较靠谱的服务商有阿里云直播,腾讯云直播,以及又拍云和网易云 ...
- 金瓯无缺江河一统|Win10系统基于Docker和Python3搭建并维护统一认证系统OpenLdap
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_180 OpenLdap(Lightweight Directory Access Protocol)是什么?它其实是一个开源的.具 ...
- SDK和API的直接区别
狭义的说法,在实际工作中, 如果对方需要你提供一个api,是指一个工程提供给另外一个工程的接口(一般是基于http协议). 如果对方需要你提供一个sdk,是指基于对方工程的编程语言,提供一个代码包.在 ...
- Ubuntu 20.04安装Docker
Docker学习系列文章 入门必备:十本你不容错过的Docker入门到精通书籍推荐 day1.全面的Docker快速入门教程 day2.CentOS 8.4安装Docker day3.Windows1 ...