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 strrpos()函数 语法

    php strrpos()函数 语法 作用:寻找某字符串中某字符最后出现的位置.大理石构件怎么选择 语法:strrpos(string,find,start) 参数: 参数 描述 string 必需. ...

  2. java单双引号转义问题

    JavaScript代码:var str = '<a href="javascript:;" onclick="visaDetail(\'1\',' + value ...

  3. 史上最全最实用HBuilder快捷键大全

    史上最全最实用HBuilder快捷键大全 一.文件操作二.编辑操作三.插入操作四.转义操作五.选择操作六.跳转操作七.查找操作八.运行九.视图一.文件操作新建菜单: ctrl + N新建: ctrl ...

  4. [CF1056E]Check Transcription

    题目:Check Transcription 传送门:http://codeforces.com/contest/1056/problem/E 分析: 1)显然有个$O( \frac{t}{max(c ...

  5. [CSP-S模拟测试]:巨神兵(状压DP)

    题目描述 欧贝利斯克的巨神兵很喜欢有向图,有一天他找到了一张$n$个点$m$条边的有向图.欧贝利斯克认为一个没有环的有向图是优美的,请问这张图有多少个子图(即选定一个边集)是优美的?答案对$1,000 ...

  6. rownum的用法oracle

    SELECT * FROM T WHERE ROWNUM=1 可以查询出来数据, 而SELECT * FROM T WHERE ROWNUM=2不可以查询出来数据. in the case of wh ...

  7. (转)Installing Cloudera Manager and CDH

    转:https://blog.csdn.net/qq_26222859/article/details/79976506 译自官网: Installing Cloudera Manager and C ...

  8. 洛谷P2661 信息传递(最小环,并查集)

    洛谷P2661 信息传递 最小环求解采用并查集求最小环. 只适用于本题的情况.对于新加可以使得两个子树合并的边,总有其中一点为其中一棵子树的根. 复杂度 \(O(n)\) . #include< ...

  9. Python笔记(十六)_else语句、with语句

    else的多种用法 1.try except + else:检测到代码无异常,才执行else 例如: def func(num): count=num//2 while count>1: if ...

  10. (转载)STL map与Boost unordered_map的比较

    原链接:传送门 今天看到 boost::unordered_map,它与 stl::map的区别就是,stl::map是按照operator<比较判断元素是否相同,以及比较元素的大小,然后选择合 ...