设为首页 加入收藏

TOP

cf438E. The Child and Binary Tree(生成函数 多项式开根 多项式求逆)(一)
2019-03-13 18:08:31 】 浏览:56
Tags:cf438E. The Child and Binary Tree 生成 函数 多项

题意

链接

Sol

生成函数博大精深Orz

我们设\(f(i)\)表示权值为\(i\)的二叉树数量,转移的时候可以枚举一下根节点

\(f(n) = \sum_{w \in C_1 \dots C_n} \sum_{j=0}^{n-w} f(j) f(n-w-j)\)

\(T =n-w\),后半部分变为\(\sum_{j=0}^T f(j) f(T-j)\),是个标准的卷积形式。

对于第一重循环我们可以设出现过的数的生成函数\(C(x)\)

可以得到\(f = C * f * f + 1\),+1是因为\(f[0] = 1\)

可以解得\(f = \frac{1\pm\sqrt{1-4G}}{2G} = \frac{2}{1\pm\sqrt{1-4C}}\)

现在问题来了,我们是要取\(+\)还是取\(-\)

结论是取\(+\),因为当取\(-\)时,C中x的取值趋向于\(0\)时分母会无意义

举个例子(来自cf讨论区)

\(C = 2x - 4x^2\)\(+\sqrt{1-4C} = 1 - 4x, -\sqrt{1-4C} = -1+4x\)

后者带入得到\(F = \frac{2}{4x}\),这玩意儿显然是无解的,因为多项式有逆元的充要条件是常数项在模意义下有逆元,然而这玩意儿的常数项是0.。

感觉做这种题直接还是要先推一推暴力dp的式子吧,不然直接用生成函数推根本无从下手。。

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out&qu
		    

ot;,"w",stdout);} using namespace std; const int MAXN = 1e6 + 10, INF = 1e9 + 1; const double eps = 1e-9, pi = acos(-1); inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int N, M, a[MAXN], b[MAXN], c[MAXN], d[MAXN]; namespace Poly { int rev[MAXN], GPow[MAXN], A[MAXN], B[MAXN], C[MAXN], lim, INV2; const int G = 3, mod = 998244353; template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;} template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} int fp(int a, int p, int P = mod) { int base = 1; for(; p; p >>= 1, a = 1ll * a * a % P) if(p & 1) base = 1ll * base * a % P; return base; } int GetLen(int x) { int lim = 1; while(lim <= x) lim <<= 1; return lim; } int GetOrigin(int x) {//¼ÆËãÔ­¸ù static int q[MAXN]; int tot = 0, tp = x - 1; for(int i = 2; i * i <= tp; i++) if(!(tp % i)) {q[++tot] = i;while(!(tp % i)) tp /= i;} if(tp > 1) q[++tot] = tp; for(int i = 2, j; i <= x - 1; i++) { for(j = 1; j <= tot; j++) if(fp(i, (x - 1) / q[j], x) == 1) break; if(j == tot + 1) return i; } } void Init(/*int P,*/ int Lim) { //mod = P; G = GetOrigin(mod); Gi = fp(G, mod - 2); INV2 = fp(2, mod - 2); for(int i = 1; i < Lim; i++) GPow[i] = fp(G, (mod - 1) / i); } void NTT(int *A, int lim, int opt) { int len = 0; for(int N = 1; N < lim; N <<= 1) ++len; for(int i = 1; i <= lim; i++) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << (len - 1)); for(int i = 0; i <= lim; i++) if(i < rev[i]) swap(A[i], A[rev[i]]); for(int mid = 1; mid < lim; mid <<= 1) { int Wn = GPow[mid << 1]; for(int i = 0; i < lim; i += (mid << 1)) { for(int j = 0, w = 1; j < mid; j++, w = mul(w, Wn)) { int x = A[i + j], y = mul(w, A[i + j + mid]); A[i + j] = add(x, y), A[i + j + mid] = add(x, -y); } } } if(opt == -1) { reverse(A + 1, A + lim); int Inv = fp(lim, mod - 2); for(int i = 0; i <= lim; i++) mul2(A[i], Inv); } } void Mul(int *a, int *b, int N, int M) { memset(A, 0, sizeof(A)); memset(B, 0, sizeof(B)); int lim = 1, len = 0; while(lim <= N + M) len++, lim <<= 1; for(int i = 0; i <= N; i++) A[i] =
编程开发网
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇【1】简单计算器(栈) 下一篇洛谷P4841 城市规划(生成函数 多..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

array(4) { ["type"]=> int(8) ["message"]=> string(24) "Undefined variable: jobs" ["file"]=> string(32) "/mnt/wp/cppentry/do/bencandy.php" ["line"]=> int(217) }