【题目链接】
【思路要点】
- 按照题解的做法,我们可以通过多项式开根、多项式求逆来求解本题。
(但是我不会,也不想学)- 我们来考虑一种比较容易的做法,首先:$$dp_s=\sum_{c_i+j+k=s}dp_j*dp_k$$
- 也就是说,我们只有知道了\(s\)较小的\(dp\)值,才能计算出\(s\)较大的\(dp\)值。
- 考虑分治,对于区间\([L,R]\),令\(Mid=\lfloor\frac{L+R}{2}\rfloor\),我们先计算\(s\in[L,Mid]\)的\(dp\)值,然后考虑\(s\in[L,Mid]\)的\(dp\)值对\(s\in[Mid+1,R]\)的\(dp\)值的影响,再计算\(s\in[Mid+1,R]\)的\(dp\)值。
- 在考虑\(s\in[L,Mid]\)的\(dp\)值对\(s\in[Mid+1,R]\)的\(dp\)值的影响时,我们需要计算出\(delta_s=\sum_{c_i+j+k=s}dp_j*dp_k(Max\{j,k\}≥L)\),再将\(delta_s\)加至\(dp_s\)中。
- 分两种情况,首先,如果\(R≥2*L\),稍加分析,我们发现,这实际上代表当前处理的节点是分治树上最靠左的节点,换而言之,\(L=0\),所以,直接NTT卷积三个多项式就可以了。
- 否则,我们发现,\(Min\{j,k\}<L\)始终成立,因此,我们取\(s\in[L,Mid]\)的\(dp\)值和\(s\in[0,R-L]\)的\(dp\)值卷积,将结果每位乘上2,再和\(c_i\)卷积即可。
- 时间复杂度\(O(MLog^2M)\)。
- UPD:现在我学会了多项式开根、多项式求逆,我们来看看怎么解这个题。
- 令多项式\(F_i=f_i\),\(C_i=[i\ exist\ in\ c]\),则有\(F\equiv F*F*C+1(mod\ x^{m+1})\)。
- 解这个方程,有\(F\equiv \frac{1\pm\sqrt{1-4C}}{2C}(mod\ x^{m+1})\)。
- 分式上下同乘\(1\mp\sqrt{1-4C}\),有\(F\equiv \frac{4C}{2C*(1\pm\sqrt{1-4C})}\equiv \frac{2}{1\pm\sqrt{1-4C}}(mod\ x^{m+1})\)。
- 注意到\(1-4C\)常数项为1,因此\(1-\sqrt{1-4C}\)常数项为0,因此它不存在逆元。
- 因此,取\(F\equiv \frac{2}{1+\sqrt{1-4C}}(mod\ x^{m+1})\)。
- 按该式计算\(F\)即可,时间复杂度降至\(O(MLogM)\)。
【代码】
/*Inverse && Sqrt of Polynomial O(NLogN)*/ #include<bits/stdc++.h> using namespace std; const int MAXN = 524288; const int P = 998244353; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } namespace NTT { const int G = 3; int power(int x, int y) { if (y == 0) return 1; int tmp = power(x, y / 2); if (y % 2 == 0) return 1ll * tmp * tmp % P; else return 1ll * tmp * tmp % P * x % P; } int N, Log, home[MAXN]; void NTTinit() { for (int i = 0; i < N; i++) { int ans = 0, tmp = i; for (int j = 1; j <= Log; j++) { ans <<= 1; ans += tmp & 1; tmp >>= 1; } home[i] = ans; } } void NTT(int *a, int mode) { for (int i = 0; i < N; i++) if (home[i] < i) swap(a[i], a[home[i]]); for (int len = 2; len <= N; len <<= 1) { int delta; if (mode == 1) delta = power(G, (P - 1) / len); else delta = power(G, P - 1 - (P - 1) / len); for (int i = 0; i < N; i += len) { int now = 1; for (int j = i, k = i + len / 2; k < i + len; j++, k++) { int tmp = a[j]; int tnp = 1ll * a[k] * now % P; a[j] = (tmp + tnp) % P; a[k] = (tmp - tnp + P) % P; now = 1ll * now * delta % P; } } } if (mode == -1) { int inv = power(N, P - 2); for (int i = 0; i < N; i++) a[i] = 1ll * a[i] * inv % P; } } void times(int *a, int *b, int *c, int limit) { N = 1, Log = 0; while (N < 2 * limit) { N <<= 1; Log++; } for (int i = limit; i < N; i++) a[i] = b[i] = 0; NTTinit(); NTT(a, 1); NTT(b, 1); for (int i = 0; i < N; i++) c[i] = 1ll * a[i] * b[i] % P; NTT(c, -1); } void timesabb(int *a, int *b, int *c, int limit) { N = 1, Log = 0; while (N < 2 * limit) { N <<= 1; Log++; } for (int i = limit; i < N; i++) a[i] = 0; for (int i = limit / 2; i <= N; i++) b[i] = 0; NTTinit(); NTT(a, 1); NTT(b, 1); for (int i = 0; i < N; i++) c[i] = 1ll * a[i] * b[i] % P * b[i] % P; NTT(c, -1); } void inverse(int *a, int *b, int limit) { for (int i = 0; i < 2 * limit; i++) { if (i >= limit) a[i] = 0; b[i] = 0; } b[0] = power(a[0], P - 2); for (int now = 1; now < limit; now <<= 1) { static int c[MAXN], d[MAXN]; for (int i = 0; i < now * 2; i++) c[i] = a[i], d[i] = b[i]; timesabb(c, d, d, now * 2); for (int i = 0; i < now * 2; i++) b[i] = (2ll * b[i] - d[i] + P) % P; } } void getsqrt(int *a, int *b, int limit, int residue) { for (int i = 0; i < 2 * limit; i++) { if (i >= limit) a[i] = 0; b[i] = 0; } b[0] = residue; int inv = power(2, P - 2); for (int now = 1; now < limit; now <<= 1) { static int c[MAXN], d[MAXN]; for (int i = 0; i < now * 2; i++) c[i] = a[i]; inverse(b, d, now * 2); times(c, d, d, now * 2); for (int i = 0; i < now * 2; i++) b[i] = 1ll * (b[i] + d[i]) * inv % P; } } } int n, m; int f[MAXN], c[MAXN], g[MAXN]; int main() { read(n), read(m); for (int i = 1; i <= n; i++) { int x; read(x); c[x] = P - 4; } c[0] = 1; NTT::getsqrt(c, g, m + 1, 1); g[0] = (g[0] + 1) % P; NTT::inverse(g, f, m + 1); for (int i = 1; i <= m; i++) writeln(f[i] * 2 % P); return 0; } /*DivideAndConquer + NTT O(NLog^2N)*/ #include<bits/stdc++.h> using namespace std; const int MAXN = 262144; const int P = 998244353; const int G = 3; template <typename T> void chkmax(T &x, T y) {x = max(x, y); } template <typename T> void chkmin(T &x, T y) {x = min(x, y); } template <typename T> void read(T &x) { x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; } template <typename T> void write(T x) { if (x < 0) x = -x, putchar('-'); if (x > 9) write(x / 10); putchar(x % 10 + '0'); } template <typename T> void writeln(T x) { write(x); puts(""); } namespace NTT { long long power(int x, int y) { if (y == 0) return 1; long long tmp = power(x, y / 2); if (y % 2 == 0) return tmp * tmp % P; else return tmp * tmp % P * x % P; } int N, Log, home[MAXN]; void NTTinit() { for (int i = 0; i < N; i++) { int ans = 0, tmp = i; for (int j = 1; j <= Log; j++) { ans <<= 1; ans += tmp & 1; tmp >>= 1; } home[i] = ans; } } void NTT(long long *a, int mode) { for (int i = 0; i < N; i++) if (home[i] < i) swap(a[i], a[home[i]]); for (int len = 2; len <= N; len <<= 1) { long long delta; if (mode == 1) delta = power(G, (P - 1) / len); else delta = power(G, P - 1 - (P - 1) / len); for (int i = 0; i < N; i += len) { long long now = 1; for (int j = i, k = i + len / 2; k < i + len; j++, k++) { long long tmp = a[j]; long long tnp = a[k] * now % P; a[j] = (tmp + tnp) % P; a[k] = (tmp - tnp + P) % P; now = now * delta % P; } } } if (mode == -1) { long long inv = power(N, P - 2); for (int i = 0; i < N; i++) a[i] = a[i] * inv % P; } } void times(long long *a, long long *b, long long *c, int limit) { N = 1, Log = 0; while (N <= 2 * limit) { N <<= 1; Log++; } for (int i = limit; i < N; i++) a[i] = b[i] = 0; NTTinit(); NTT(a, 1); NTT(b, 1); for (int i = 0; i < N; i++) c[i] = a[i] * b[i] % P; NTT(c, -1); for (int i = limit; i < N; i++) c[i] = 0; } }; bool exist[MAXN]; long long dp[MAXN]; void solve(int l, int r) { if (l == r) return; int mid = (l + r) / 2; solve(l, mid); int len = r - l; static long long a[MAXN], b[MAXN], c[MAXN]; if (len >= l) { for (int i = 0; i <= r; i++) a[i] = b[i] = dp[i]; NTT::times(a, b, c, r + 1); for (int i = 0; i <= r; i++) b[i] = exist[i]; NTT::times(c, b, a, r + 1); for (int i = mid + 1; i <= r; i++) dp[i] = (dp[i] + a[i]) % P; } else { for (int i = l; i <= mid; i++) a[i - l] = dp[i]; for (int i = mid + 1; i <= r; i++) a[i - l] = 0; for (int i = 0; i <= len; i++) b[i] = dp[i]; NTT::times(a, b, c, len + 1); for (int i = 0; i <= len; i++) { c[i] = c[i] * 2 % P; b[i] = exist[i]; } NTT::times(c, b, a, len + 1); for (int i = mid + 1; i <= r; i++) dp[i] = (dp[i] + a[i - l]) % P; } solve(mid + 1, r); } int main() { int n, m; read(n), read(m); for (int i = 1; i <= n; i++) { int x; read(x); exist[x] = true; } dp[0] = 1; solve(0, m); for (int i = 1; i <= m; i++) writeln(dp[i]); return 0; }