[BZOJ3625][Codeforces Round #250]小朋友和二叉树 多项式开根+求逆
https://www.lydsy.com/JudgeOnline/problem.php?id=3625
愉快地列式子。设\(F[i]\)表示权值为\(i\) 的子树的方案数,\(A[i]\)为\(i\)在不在集合中。
\]
初始状态\(F[0]=1\)。
我们把\(F,A\)看成多项式。
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\),如果取\(+\)号,末位会不符,舍出。因此只能取\(-\)。
这样
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]小朋友和二叉树 多项式开根+求逆的更多相关文章
- BZOJ3625 [Codeforces Round #250]小朋友和二叉树(生成函数+多项式开根)
设f(n)为权值为n的神犇二叉树个数.考虑如何递推求这个东西. 套路地枚举根节点的左右子树.则f(n)=Σf(i)f(n-i-cj),cj即根的权值.卷积的形式,cj也可以通过卷上一个多项式枚举.可以 ...
- BZOJ3625: [Codeforces Round #250]小朋友和二叉树
Description 我们的小朋友很喜欢计算机科学,而且尤其喜欢二叉树.考虑一个含有n个互异正整数的序列c[1],c[2],...,c[n].如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合{ ...
- BZOJ 3625: [Codeforces Round #250]小朋友和二叉树
3625: [Codeforces Round #250]小朋友和二叉树 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 304 Solved: 13 ...
- BZOJ 3625 [Codeforces Round #250]小朋友和二叉树 ——NTT 多项式求逆 多项式开根
生成函数又有奇妙的性质. $F(x)=C(x)*F(x)*F(x)+1$ 然后大力解方程,得到一个带根号的式子. 多项式开根有解只与常数项有关. 发现两个解只有一个是成立的. 然后多项式开根.求逆. ...
- 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 \ ...
- [Codeforces Round #250]小朋友和二叉树
题目描述: bzoj luogu 题解: 生成函数ntt. 显然这种二叉树应该暴力薅掉树根然后分裂成两棵子树. 所以$f(x)= \sum_{i \in c} \sum _{j=0}^{x-c} f( ...
- BZOJ 3625:小朋友和二叉树 多项式开根+多项式求逆+生成函数
生成函数这个东西太好用了~ code: #include <bits/stdc++.h> #define ll long long #define setIO(s) freopen(s&q ...
- 【XSY2730】Ball 多项式exp 多项式ln 多项式开根 常系数线性递推 DP
题目大意 一行有\(n\)个球,现在将这些球分成\(k\) 组,每组可以有一个球或相邻两个球.一个球只能在至多一个组中(可以不在任何组中).求对于\(1\leq k\leq m\)的所有\(k\)分别 ...
- 【BZOJ3625】【CF438E】小朋友和二叉树 NTT 生成函数 多项式开根 多项式求逆
题目大意 考虑一个含有\(n\)个互异正整数的序列\(c_1,c_2,\ldots ,c_n\).如果一棵带点权的有根二叉树满足其所有顶点的权值都在集合\(\{c_1,c_2,\ldots ,c_n\ ...
随机推荐
- Android逆向之旅---基于对so中的函数加密技术实现so加固
一.前言 今天我们继续来介绍so加固方式,在前面一篇文章中我们介绍了对so中指定的段(section)进行加密来实现对so加固 http://blog.csdn.net/jiangwei0910410 ...
- shapefile文件数据结构
头部 点 线 面 序号 x,y,... 线 序号 1,2 面 序号 1,2,3 拓扑检查 ... <GIS数据结构与算法>
- 让VirtualBox虚拟机实现开机自动后台运行
转至:http://www.cnblogs.com/top5/archive/2012/01/19/2326234.html 测试环境:Host OS: Windows 7 x64 Guest OS: ...
- java logger的info记录在哪
这个要看你的日志配置文件是怎么配置的,以log4j为例log4j.appender.mainLog=org.apache.log4j.ConsoleAppenderlog4j.appender.mai ...
- Java 设计模式之 策略模式
思维导图: 我们先来看 STRATEGY 设计模式的通俗解释: 跟不同类型的MM约会,要用不同的策略,有的请电影比较好,有的则去吃小吃效果不错,有的去海边浪漫最合适,但目的都是为了得到 MM 的芳心, ...
- Java学习、面试、求职、干货资源精品合集
本系列文章整合了本号发表和转载过的,有关Java学习.进阶.面试.做项目.求职经验等方面的文章,希望对想要找工作,以及正在找工作的你,能够有所帮助. 原创Java学习专题文章: 如何才能够系统地学习J ...
- 提取的js,要先部署在远程,再引入
var meet = { _w: document.documentElement.clientWidth, _h: document.documentElement.clientWidth, ini ...
- 126、TensorFlow Session的执行
# tf.Session.run 方法是一个执行tf.Operation或者计算tf.Tensor的一个主要的机制 # 你可以传递一个或者多个tf.Operation或者tf.Tensor对象来给tf ...
- 解决Win7部分便笺的元数据已被损坏的方法
Win7部分便笺的元数据已被损坏的方法 我们使用键盘上"Win+F"组合键搜索功能,直接找到"inkobj.dll"这个文件,一般会搜索出来好多,先随便选一个. ...
- 《图解设计模式》读书笔记3-2 Prototype模式
目录 Prototype(原型)模式的由来 类图 代码 角色 我的理解 Prototype(原型)模式的由来 创建一个实例,可以关键字new创建.但有时候,我们需要在不指定类名的前提下生成实例,比如: ...