CodeForces 438E The Child And Binary Tree NTT模板 生成函数应用

求有多少个二叉树使每个点权值均在给定集合C中而且权值和为

fs 表示总权值为s的合格二叉树个数。
于是有

f0=1fs=wCifi×fswi

发现右边和卷积有点像。
于是令
gk=ifi×fki

由于 g(k)=f(k)f(k) ,即数列 g 是数列 f 的自卷积(自己乱起的名字不要在意)。
由卷积法则可知,数列 g 的生成函数与 f 的生成函数的关系是: G(x)=F2(x)

另外还有

fs=wCgsw

如果令序列 g 右移w位的新序列为 hw ,那么有
fs=wChw[s]

那么 f 数列就是一些 hw 数列的和了。
hw 数列的生成函数是 Hw(x)=G(x)xci ,于是 f 数列的生成函数
F(x)=F2(x)(xc1+xc2++xcn)+1

1表示 f0=1
如果令
C(x)=i=1nxci
那么
F(x)=C(x)F2(x)+1

解得
F(x)=114C(x)2C(x)=21+14C(x)

剩下的问题就是计算 F(x) 了。
多项式开方 多项式求逆。。
复杂度是 O(nlogn) 还是 O(nlog2n) 的呢。。

VFK神的题解好详细的啦。
http://codeforces.com/blog/entry/12513

还有前置技能
http://picks.logdown.com/posts/189620-the-inverse-element-of-polynomial
http://picks.logdown.com/posts/197262-polynomial-division
http://picks.logdown.com/posts/202388-square-root-of-polynomial

真的写了很久。。

#include 
#include 
#define FOR(i,j,k) for(i=j;i<=k;++i)
#define rep(i,j,k) for(i=j;i
#define gmod(i) (((i)%mod+mod)%mod)
const int N = 262144, mod = 998244353, inv2 = 499122177;
using namespace std;
typedef long long ll;
ll qpow(ll x, int y) {
    ll z = 1;
    for (; y; x = x * x % mod, y /= 2)
        if (y & 1) z = z * x % mod;
    return z;
}
namespace NTT {
    int n, rev[N], inv_n, m = -1;
    void init(int c) {
        int k = -1, i;
        if (m == c) return; else m = c;
        for (n = 1; n <= m; n <<= 1) ++k;
        inv_n = qpow(n, mod - 2);
        rep(i,0,n) rev[i] = (rev[i >> 1] >> 1) | ((i & 1) << k);
    }
    void ntt(int *a, int f) {
        int h, i, j;
        rep(i,0,n) if (i < rev[i]) swap(a[i], a[rev[i]]);
        for (h = 2; h <= n; h *= 2) {
            int wn = qpow(3, (mod - 1) / h);
            for (i = 0; i < n; i += h) {
                int w = 1;
                rep(j,0,h/2) {
                    int u = a[i + j], t = 1ll * a[i + j + h / 2] * w % mod;
                    a[i + j + h / 2] = (u - t + mod) % mod;
                    a[i + j] = (u + t) % mod;
                    w = 1ll * w * wn % mod;
                }
            }
        }
        if (f) {
            rep(i,1,n/2) swap(a[i], a[n - i]);
            rep(i,0,n) a[i] = 1ll * a[i] * inv_n % mod;
        }
    }
}
void inv(int *a, int *b, int n) {
    static int t[N];
    int i;
    if (n == 1) { b[0] = qpow(a[0], mod - 2); return; }
    inv(a, b, n / 2);
    rep(i,0,n) t[i] = a[i]; rep(i,n,2*n) t[i] = 0;
    NTT::init(n);
    NTT::ntt(t, 0); NTT::ntt(b, 0);
    rep(i,0,NTT::n) t[i] = (ll) b[i] * gmod(2ll - (ll) t[i] * b[i] % mod) % mod;
    NTT::ntt(t, 1);
    rep(i,0,n) b[i] = t[i]; rep(i,n,2*n) b[i] = 0;
}
void sqrt(int *a, int *b, int n) {
    static int t[N], b1[N];
    if (n == 1) { b[0] = 1; return; }
    int i;
    sqrt(a, b, n / 2);
    rep(i,0,n) b1[i] = 0;
    inv(b, b1, n);
    rep(i,0,n) t[i] = a[i]; rep(i,n,2*n) t[i] = 0;
    NTT::init(n);
    NTT::ntt(t, 0), NTT::ntt(b, 0), NTT::ntt(b1, 0);
    rep(i,0,NTT::n) t[i] = inv2 * ((b[i] + (ll) b1[i] * t[i] % mod) % mod) % mod;
    NTT::ntt(t, 1);
    rep(i,0,n) b[i] = t[i]; rep(i,n,2*n) b[i] = 0;
}
int main() {
    static int c[N], sc[N], ic[N];
    int i, x, n, m, l;
    scanf("%d%d", &n, &m);
    FOR(i,1,n) scanf("%d", &x), ++c[x];
    c[0] = gmod(1 - c[0]);
    FOR(i,1,m) c[i] = gmod(-4 * c[i]);
    for (l = 1; l <= m; l <<= 1);
    sqrt(c, sc, l);
    (++sc[0]) %= mod;
    inv(sc, ic, l);
    FOR(i,0,m) ic[i] = 2ll * ic[i] % mod;
    FOR(i,1,m) printf("%d\n", ic[i]);
    return 0;
} 

E. The Child and Binary Tree

Our child likes computer science very much, especially he likes binary trees.

Consider the sequence of n distinct positive integers: c1, c2, …, cn. The child calls a vertex-weighted rooted binary tree good if and only if for every vertex v, the weight of v is in the set {c1, c2, …, cn}. Also our child thinks that the weight of a vertex-weighted tree is the sum of all vertices’ weights.

Given an integer m, can you for all s(1sm) calculate the number of good vertex-weighted rooted binary trees with weight s? Please, check the samples for better understanding what trees are considered different.

We only want to know the answer modulo 998244353 (7 × 17 × 223 + 1, a prime number).

Input

The first line contains two integers n,m ( 1n105;1m105 ). The second line contains n space-separated pairwise distinct integers c1,c2,,cn . ( 1ci105 ).

Output

Print m lines, each line containing a single integer. The i-th line must contain the number of good vertex-weighted rooted binary trees whose weight exactly equal to i. Print the answers modulo 998244353 (7 × 17 × 223 + 1, a prime number).

Examples

input

2 3
1 2

output

1
3
9

input

3 10
9 4 3

output

0
0
1
1
0
2
4
2
6
15

input

5 10
13 10 6 4 15

output

0
0
0
1
0
1
0
2
0
5

Note

In the first example, there are 9 good vertex-weighted rooted binary trees whose weight exactly equal to 3:

你可能感兴趣的:(快速变换,计数问题,CodeForces)