您的当前位置:首页正文

「Codeforces438E」The Child and Binary Tree-生成函数+多项式求逆+多项式开方

2024-11-30 来源:个人技术集锦

Description

Solution

f i f_i fi表示点权和为 i i i的二叉树个数, c i c_i ci表示点权为 i i i的点的个数。那么

f i = ∑ i = 0 n c i ∑ j = 0 n − i f j × f n − i − j f_i=\sum_{i=0}^n c_i \sum_{j=0}^{n-i}f_j\times f_{n-i-j} fi=i=0ncij=0nifj×fnij

F i F_i Fi表示答案 f i f_i fi的生成函数, C i C_i Ci表示 c i c_i ci的生成函数。那么可以得到式子

F ( x ) = F 2 ( x ) × C ( x ) + 1 F(x)=F^2(x)\times C(x)+1 F(x)=F2(x)×C(x)+1

表示一个点自己的方案数乘上左右子树的方案数。

#include <bits/stdc++.h>
using namespace std;

typedef long long lint;
const int mod = 998244353, phi = mod - 1, G = 3, inv2 = (mod + 1) / 2;
const int maxn = 500005;

int n, m, N, f[maxn], a[maxn], c[maxn];

inline int gi()
{
    char c = getchar();
    while (c < '0' || c > '9') c = getchar();
    int sum = 0;
    while ('0' <= c && c <= '9') sum = sum * 10 + c - 48, c = getchar();
    return sum;
}

inline int Pow(int x, int k)
{
    int res = 1;
    while (k) {
        if (k & 1) res = (lint)res * x % mod;
        x = (lint)x * x % mod; k >>= 1;
    }
    return res;
}

namespace poly
{

    int len, n, L, R[maxn], A[maxn], B[maxn];

    void NTT(int *a, int f)
    {
        for (int i = 0; i < n; ++i) if (i < R[i]) swap(a[i], a[R[i]]);
        for (int i = 1; i < n; i <<= 1) {
            int wn = Pow(G, phi / (i << 1)), t;
            if (f == -1) wn = Pow(wn, mod - 2);
            for (int j = 0; j < n; j += (i << 1)) {
                int w = 1;
                for (int k = 0; k < i; ++k, w = (lint)w * wn % mod) {
                    t = (lint)a[j + i + k] * w % mod;
                    a[j + i + k] = a[j + k] - t;
                    if (a[j + i + k] < 0) a[j + i + k] += mod;
                    a[j + k] = a[j + k] + t;
                    if (a[j + k] >= mod) a[j + k] -= mod;
                }
            }
        }
    }
    
    void mul(int *a, int *b, int len1, int len2, int *c, int len3)
    {
        for (L = 0, n = 1, len = len1 + len2 - 1; n < len; n <<= 1) ++L;
        for (int i = 0; i < n; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));

        fill(A, A + n, 0); fill(B, B + n, 0);
        for (int i = 0; i < len1; ++i) A[i] = a[i];
        for (int i = 0; i < len2; ++i) B[i] = b[i];
        NTT(A, 1); NTT(B, 1);
        for (int i = 0; i < n; ++i) A[i] = (lint)A[i] * B[i] % mod;
        NTT(A, -1);

        int inv = Pow(n, mod - 2);
        for (int i = 0; i < len3; ++i) c[i] = (lint)A[i] * inv % mod;
    }

    void mul(int *a, int *b, int len1, int *c, int len2)
    {
        for (L = 0, n = 1, len = (len1 << 1) - 1; n < len; n <<= 1) ++L;
        for (int i = 0; i < n; ++i) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));

        fill(A, A + n, 0); fill(B, B + n, 0);
        for (int i = 0; i < len1; ++i) A[i] = a[i], B[i] = b[i];
        NTT(A, 1); NTT(B, 1);
        for (int i = 0; i < n; ++i) A[i] = (lint)A[i] * B[i] % mod * B[i] % mod;
        NTT(A, -1);

        int inv = Pow(n, mod - 2);
        for (int i = 0; i < len2; ++i) c[i] = (lint)A[i] * inv % mod;
    }

    void poly_inv(int *a, int n, int *b)
    {
        static int c[maxn];
        b[0] = Pow(a[0], mod - 2);
        for (int t = 2; (t >> 1) < n; t <<= 1) {
            for (int i = 0; i < t; ++i) c[i] = b[i];
            mul(a, b, t, b, t);
            for (int i = 0; i < t; ++i) b[i] = (2ll * c[i] - b[i] + mod) % mod;
        }
    }

    void poly_sqrt(int *a, int n, int *b)
    {
        static int c[maxn], d[maxn];
        b[0] = a[0];
        for (int t = 2; (t >> 1) < n; t <<= 1) {
            poly_inv(b, t, d);
            mul(b, b, t, t, c, t);
            for (int i = 0; i < t; ++i) c[i] = (lint)(c[i] + a[i]) * inv2 % mod;
            mul(c, d, t, t, b, t);
            fill(c, c + t, 0); fill(d, d + t, 0);
        }
    }
    
}

int main()
{
    n = gi(); m = gi();
    for (int i = 1; i <= n; ++i) ++c[gi()];
    
    for (N = 1; N <= m; N <<= 1);
    c[0] = 1;
    for (int i = 1; i <= N; ++i) if (c[i]) c[i] = mod - (c[i] << 2);
    poly::poly_sqrt(c, N, a);
    ++a[0];
    poly::poly_inv(a, N, f);
    for (int i = 0; i <= N; ++i) f[i] = f[i] * 2 % mod;

    for (int i = 1; i <= m; ++i) printf("%d\n", f[i]);
    
    return 0;
}
显示全文