https://www.lydsy.com/JudgeOnline/problem.php?id=3625

愉快地列式子。设\(F[i]\)表示权值为\(i\) 的子树的方案数,\(A[i]\)为\(i\)在不在集合中。

\[F[n]=\sum_{i=0}^n \sum_{j=0}^{n-i}F[i]\cdot F[j]\cdot A[n-i-j]
\]

初始状态\(F[0]=1\)。

我们把\(F,A\)看成多项式。

\[F(x)-1=F^2(x)\cdot A(x)\\
A(x)\cdot F^2(x)-F(x)+1=0\\
F(x)=\frac{1\pm\sqrt{1-4A(x)}}{2A(x)}
\]

因为\(A[0]=0\)而\(F[0]=1\),如果取\(+\)号,末位会不符,舍出。因此只能取\(-\)。

这样

\[\begin{align*}
F(x)&=\frac{1-\sqrt{1-4A(x)}}{2A(x)}\\
&=\frac{(1-\sqrt{1-4A(x)})(1+\sqrt{1-4A(x)})}{2A(x)(1+\sqrt{1-4A(x)})}\\
&=\frac{4A(x)}{2A(x)(1+\sqrt{1-4A(x)})}\\
&=\frac{2}{1+\sqrt{1-4A(x)}}
\end{align*}
\]

这样就变成多项式开根+求逆的板子了。


(刚开始转码风,可能有些地方不太自然,也有可能有些地方仍然保留着就码风没有注意)

#include<bits/stdc++.h>

#define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back
#define isbreak dbg("*") template<typename A, typename B> inline char SMAX(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char SMIN(A &a, const B &b) {return b < a ? a = b , 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<int, int> pii; template<typename I>
inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 4e5 + 7;
const int P = 998244353;
const int G = 3;
const int Gi = 332748118;
const int Inv2 = 499122177; int n, m, x, a[N]; inline void SADD(int &x, int y) {x += y;x >= P ? x -= P : 0;}
inline int SMOD(int x) {return x >= P ? x - P : x;}
inline int fpow(int x,int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
} namespace DFT {
int A[N], B[N], C[N]; inline void NTT(int *a, int n, int f) {
for (int i = 0, j = 0; i < n; ++i) {
if (i > j) std::swap(a[i], a[j]);
for (int l = n >> 1; (j ^= l) < l; l >>= 1);
}
for (int i = 1; i < n; i <<= 1) {
int w = fpow(f > 0 ? G : Gi, (P - 1) / (i << 1));
for (int j = 0; j < n; j += i << 1)
for (int k = 0, e = 1; k < i; ++k, e = (ll)e * w % P){
int x = a[j + k], y = (ll)e * a[i + j + k] % P;
a[j + k] = SMOD(x + y); a[i + j + k] = SMOD(x + P - y);
}
}
if (f < 0) for (int i = 0, p = fpow(n, P - 2); i < n; ++i) a[i] = (ll)a[i] * p % P;
}
namespace Inv {
int A[N], B[N];
inline void GetInv(int *a, int n, int *b) {
memset(B, 0, sizeof(B)); B[0] = fpow(a[0], P - 2);
for (int deg = 2; deg < (n << 1); deg <<= 1) {
int L = deg << 1;
for (int i = 0; i < deg; ++i) A[i] = a[i];
for (int i = deg; i < L; ++i) A[i] = 0;
NTT(A, L, 1); NTT(B, L, 1);
for (int i = 0; i < L; ++i) B[i] = (ll)B[i] * (2 + P - (ll)B[i] * A[i] % P) % P;
NTT(B, L, -1);
for (int i = deg; i < L; ++i) B[i] = 0;
}
for (int i = 0; i < n; ++i) b[i] = B[i];
}
} using Inv::GetInv; inline void GetSqrt(int *a, int n, int *b) {
B[0] = 1;
for (int deg = 2; deg < (n << 1); deg <<= 1) {
int L = deg << 1;
for (int i = 0; i < deg; ++i) A[i] = a[i];
for (int i = deg; i < L; ++i) A[i] = 0;
GetInv(B, deg, C);
NTT(A, L, 1); NTT(C, L, 1);
for (int i = 0; i < L; ++i) C[i] = (ll)A[i] * C[i] % P;
NTT(C, L, -1);
for (int i = 0; i < L; ++i) B[i] = (ll)(B[i] + C[i]) * Inv2 % P;
for (int i = deg; i < L; ++i) B[i] = 0;
}
for (int i = 0; i < n; ++i) b[i] = B[i];
}
}
using DFT::GetInv;
using DFT::GetSqrt; int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
read(n), read(m);
for (int i = 1; i <= n; ++i) read(x), a[x] = 1;
for (int i = 1; i <= m; ++i) if (a[i]) a[i] = P - SMOD(SMOD(a[i] << 1) << 1);
a[0] = 1;
GetSqrt(a, m + 1, a); SADD(a[0], 1);
GetInv(a, m + 1, a);
for (int i = 1; i <= m; ++i) printf("%d\n", SMOD(a[i] << 1));
}

[BZOJ3625][Codeforces Round #250]小朋友和二叉树 多项式开根+求逆的更多相关文章

  1. BZOJ3625 [Codeforces Round #250]小朋友和二叉树(生成函数+多项式开根)

    设f(n)为权值为n的神犇二叉树个数.考虑如何递推求这个东西. 套路地枚举根节点的左右子树.则f(n)=Σf(i)f(n-i-cj),cj即根的权值.卷积的形式,cj也可以通过卷上一个多项式枚举.可以 ...

  2. BZOJ3625: [Codeforces Round #250]小朋友和二叉树

    Description 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树.考虑一个含有n个互异正整数的序列c[1],c[2],...,c[n].如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合{ ...

  3. BZOJ 3625: [Codeforces Round #250]小朋友和二叉树

    3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 304  Solved: 13 ...

  4. BZOJ 3625 [Codeforces Round #250]小朋友和二叉树 ——NTT 多项式求逆 多项式开根

    生成函数又有奇妙的性质. $F(x)=C(x)*F(x)*F(x)+1$ 然后大力解方程,得到一个带根号的式子. 多项式开根有解只与常数项有关. 发现两个解只有一个是成立的. 然后多项式开根.求逆. ...

  5. bzoj 3625: [Codeforces Round #250]小朋友和二叉树【NTT+多项式开根求逆】

    参考:https://www.cnblogs.com/2016gdgzoi509/p/8999460.html 列出生成函数方程,g(x)是价值x的个数 \[ f(x)=g(x)*f^2(x)+1 \ ...

  6. [Codeforces Round #250]小朋友和二叉树

    题目描述: bzoj luogu 题解: 生成函数ntt. 显然这种二叉树应该暴力薅掉树根然后分裂成两棵子树. 所以$f(x)= \sum_{i \in c} \sum _{j=0}^{x-c} f( ...

  7. BZOJ 3625:小朋友和二叉树 多项式开根+多项式求逆+生成函数

    生成函数这个东西太好用了~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&q ...

  8. 【XSY2730】Ball 多项式exp 多项式ln 多项式开根 常系数线性递推 DP

    题目大意 一行有\(n\)个球,现在将这些球分成\(k\) 组,每组可以有一个球或相邻两个球.一个球只能在至多一个组中(可以不在任何组中).求对于\(1\leq k\leq m\)的所有\(k\)分别 ...

  9. 【BZOJ3625】【CF438E】小朋友和二叉树 NTT 生成函数 多项式开根 多项式求逆

    题目大意 考虑一个含有\(n\)个互异正整数的序列\(c_1,c_2,\ldots ,c_n\).如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合\(\{c_1,c_2,\ldots ,c_n\ ...

随机推荐

  1. php substr_replace()函数 语法

    php substr_replace()函数 语法 作用:替换字符串中某串为另一个字符串大理石平台价格 语法:substr_replace(string,replacement,start,lengt ...

  2. 五一清北学堂培训之数据结构(Day 1&Day 2)

    Day 1 前置知识: 二进制 2.基本语法 3.时间复杂度 正片       1.枚举 洛谷P1046陶陶摘苹果  入门题没什么好说的 判断素数:从2枚举到sqrt(n),若出现n的因子,则n是合数 ...

  3. machine learning 之 Recommender Systems

    整理自Andrew Ng的machine learning 课程 week 9. 目录: Problem Formulation(问题的形式) Content Based Recommendation ...

  4. flutter输入颜色枚举卡顿假死

    AndroidStudio 3.3.2 遇到 flutter输入颜色枚举卡顿假死,目前没好的解决方案,可以设置显示时间或者关闭popup窗口显示文档,这样就不会卡顿了 下面示例代码在输入 Colors ...

  5. Mybatis入门之MyBatis基础

    一.MyBatis概述 1.ORM模型简介 ORM:对象关系映射(Object Relation Mapping) 1)传统JDBC程序的设计缺陷(实际项目不使用) a.大量配置信息硬编码 b.大量的 ...

  6. mongo可视化工具adminMongo安装

    git环境搭建下载地址:https://git-scm.com/downloads 此处,安装环境为windows操作系统,所以选择windows版本下载一直下一步,直至安装完成找到安装git的目录下 ...

  7. ab工具进行压力测试

    简介与安装 ab:Apache Benchmark,只要我们安装了Apache,就能够在Apache的安装目录中找到它. yum | apt 安装的Apache  ab的目录一般为/usr/bin 也 ...

  8. Bootstrap 学习笔记13 附加导航插件

    附加导航代码: <style> a:focus { outline: none; } .nav-pills { width: 150px; } .nav-pills.affix { top ...

  9. CentOS7和Ubuntu18.10下运行Qt Creator出现cannot find -lGL的问题的解决方案

    解决方法:缺少相应的opengl的库,需要安装opengl库 一.Ubuntu下解决Qt5.11.1 cannot find -lGL 有两种原因: 一种是没有按照libGL库,那么就安装: sudo ...

  10. Arrays -数组工具类,数组转化字符串,数组排序等

    package cn.learn.basic; import java.util.Arrays; /* java.util.Arrays是一个与数组相关的工具类,含有大量静态方法,用来实现数组常见的操 ...