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));
}

最新文章

  1. Leetcode: Insert Delete GetRandom O(1) - Duplicates allowed
  2. JS对象类型的确定
  3. Calendar中add函数和roll函数的用法及区别
  4. Add Digits 解答
  5. RH033读书笔记(3)-Lab 4 Browsing the Filesystem
  6. ibatis实战之基础环境搭建
  7. 邪恶改装:TPYBoard制作廉价WIFI干扰器
  8. 使用AspNetPager控件分页
  9. Android给控件添加触摸回调
  10. Selenium webdriver定位iframe里面元素
  11. Linux查看本机IP:curl cip.cc
  12. Windows XP解决显示桌面图标消失的问题
  13. sass中的循环判断条件语句
  14. 02:MongoDB操作
  15. HTTP/1.1新建会话失败 解决方法及分析
  16. PHP Jquery 代码操作 内容 属性 样式 事件 Json数据
  17. 如何在 Azure 门户中将托管数据磁盘附加到 Windows VM
  18. java接口对接——别人调用我们接口获取数据
  19. dataTables常用参数
  20. 自己实现简单的RSA秘钥生成与加解密(Java )

热门文章

  1. The Cats&#39; Feeding Spots
  2. [CSP-S模拟测试]:reverse(数位DP)
  3. List of yellow pages
  4. Oracle命令行模式,批量执行SQL脚本
  5. Ehcache配置项及持久化到硬盘
  6. HDU 4321 Arcane Numbers 2
  7. 常用的Android关键词定位方法
  8. WEB服务端安全---认证会话与访问控制
  9. URL的‘#’号
  10. spring事务——try{...}catch{...}中事务不回滚的几种处理方式(转载)