[题解] 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个节点. 树上的边都是有向边,并且不一定是从父亲指向儿子的. 你可以任意翻 ...
随机推荐
- AtCoder Beginner Contest 247 E - Max Min // 容斥原理
原题链接:E - Max Min (atcoder.jp) 题意: 给定一个数组,求满足最大值为X且最小值为Y的区间个数. 思路:容斥原理 因为必须要包含端点,直接求是不容易的.因此考虑去求不一定包含 ...
- Python3.7将普通图片(png)转换为SVG图片格式并且让你的网站Logo(图标)从此”动”起来
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_148 在之前的几篇文章中,介绍了业界中比较火爆的图片技术SVG(Scalable Vector Graphics),比如Iconf ...
- 把酒言欢话聊天,基于Vue3.0+Tornado6.1+Redis发布订阅(pubsub)模式打造异步非阻塞(aioredis)实时(websocket)通信聊天系统
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_202 "表达欲"是人类成长史上的强大"源动力",恩格斯早就直截了当地指出,处在蒙昧时代即低 ...
- Python 车主之家全系车型(包含历史停售车型)配置参数爬虫
本文仅供学习交流使用,如侵立删!demo下载见文末 车主之家全系车型(包含历史停售车型)配置参数爬虫 先上效果图 环境: win10 ,Contos7.4 python3.9.4 pycharm202 ...
- 【ARK UI】HarmonyOS ETS如何创建PixeMap并显示Image组件上
参考资料 图片处理 Context模块 api讲解 image.createPixelMap createPixelMap(number: fd, options: InitializationOp ...
- 4步教你学会使用Linux-Audit工具
摘要:简单来讲audit是Linux上的审计工具,可以用来记录和监控对文件.目录.系统资源的更改:Audit无法直接增强系统的安全性,但是它可以用于发现违反系统安全政策的行为. 本文分享自华为云社区& ...
- SpringBoot 注解简介(持续更新)
虽然工作中交替会使用spring mvc 和spring boot 框架,但实际对spring中的很多注解并不是很了解,本篇将持续更新学习到的spring 注解. Spring 主入口类上的注解 Sp ...
- C++ 遍历磁盘文件 非递归方法 和递归方法
1: 非递归方法: 一起学习 寻找快乐 // File Name: CSearch.h #pragma once #include <vector> #include <atlst ...
- ansible 的安装及常见模块使用
ansible 基础keys的ssh协议配置的 特性:幂等性:一个任务执行1遍和执行n遍效果一样. ansible是个管理软件不是服务,不需要长期运行 一.通过epel源安装ansible, 1.下 ...
- Python自学教程8-数据类型有哪些注意事项
不知不觉,python自学教程已经更新到第八篇了,再有几篇,基本的语法就介绍完了. 今天来总结一下数据类型有哪些需要注意的地方. 元组注意事项 元组是另一种经常使用到的数据类型,看上去和列表差不多.它 ...