P4705 玩游戏
有ansk=∑i=1n∑j=1m(ai+bj)knm先舍弃nm不管ansk=∑r=0k∑i=1n∑j=1mCkrairbjk−r=∑r=0k∑i=1n∑j=1mk!r!(k−r)!airbjk−r=k!∑r=0k(1r!∑i=1nair)(1(k−r)!∑j=1mbjk−r)不难发现这就是一个卷积的形式了,但是我们现在还不知道∑i=1nair,∑i=1mbir,(r∈[0,k])设A(x)为∑i=1nair的生成函数,同理可得B(x)A(x)=∑r≥0xr∑i=1nair=∑i=1n∑r≥0xrair=∑i=1n11−aix=n−x∑i=1n−ai1−aix=n−x∑i=1n(ln(1−aix))′=n−x(ln∏i=1n(1−aix))′有ans_k = \frac{\sum\limits_{i = 1} ^{n} \sum\limits_{j = 1} ^{m} (a_i + b_j) ^ k}{nm}\\ 先舍弃nm不管\\ ans_k = \sum_{r = 0} ^{k} \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} C_{k} ^{r} a_i ^ rb_j ^{k - r}\\ = \sum_{r = 0} ^{k} \sum_{i = 1} ^{n} \sum_{j = 1} ^{m} \frac{k!}{r!(k - r)!}a_i ^{r}b_{j} ^{k - r}\\ = k!\sum_{r = 0} ^{k} \left(\frac{1}{r!} \sum_{i = 1} ^{n}a_i ^ r\right) \left(\frac{1}{(k - r)!} \sum_{j = 1} ^{m} b_j ^ {k - r}\right)\\ 不难发现这就是一个卷积的形式了,但是我们现在还不知道\sum_{i = 1} ^{n} a_i ^ r, \sum_{i = 1} ^{m} b_i ^ r, (r \in[0, k])\\ 设A(x)为\sum_{i = 1} ^{n} a_i ^ r的生成函数,同理可得B(x)\\ A(x) = \sum_{r \geq 0} x ^ r \sum_{i = 1} ^{n} a_i ^ r\\ = \sum_{i = 1} ^{n} \sum_{r \geq 0} x ^ r a_i ^ r\\ = \sum_{i = 1} ^{n} \frac{1}{1 - a_i x}\\ = n - x \sum_{i = 1} ^{n} \frac{-a_i}{1 - a_i x}\\ = n - x \sum_{i = 1} ^{n} (\ln(1 - a_ix))'\\ = n - x(\ln \prod_{i = 1} ^{n}(1 - a_ix))'\\ 有ansk=nmi=1∑nj=1∑m(ai+bj)k先舍弃nm不管ansk=r=0∑ki=1∑nj=1∑mCkrairbjk−r=r=0∑ki=1∑nj=1∑mr!(k−r)!k!airbjk−r=k!r=0∑k(r!1i=1∑nair)((k−r)!1j=1∑mbjk−r)不难发现这就是一个卷积的形式了,但是我们现在还不知道i=1∑nair,i=1∑mbir,(r∈[0,k])设A(x)为i=1∑nair的生成函数,同理可得B(x)A(x)=r≥0∑xri=1∑nair=i=1∑nr≥0∑xrair=i=1∑n1−aix1=n−xi=1∑n1−aix−ai=n−xi=1∑n(ln(1−aix))′=n−x(lni=1∏n(1−aix))′
对于∏i=1n(1−aix)=∏i=1mid(1−aix)∏i=mid+1n(1−aix)\prod\limits_{i = 1} ^{n} (1 - a_ix) = \prod\limits_{i = 1} ^{mid}(1 - a_ix) \prod\limits_{i = mid + 1} ^{n} (1 - a_ix)i=1∏n(1−aix)=i=1∏mid(1−aix)i=mid+1∏n(1−aix),所以递归合并,复杂度O(nlognlogn)O(n \log n \log n)O(nlognlogn),
然后再对上面求得的取对数ln\lnln,复杂度O(nlogn)O(n \log n)O(nlogn),求一次导,可得A(x)A(x)A(x)系数,然后再乘上inv[i!]inv[i!]inv[i!],再做一次NTTNTTNTT,即可得到答案。
这道题还真是有亿点点细节呀……
#include <bits/stdc++.h>using namespace std;const int mod = 998244353, inv2 = mod + 1 >> 1;namespace Quadratic_residue {struct Complex {int r, i;Complex(int _r = 0, int _i = 0) : r(_r), i(_i) {}};int I2;Complex operator * (const Complex &a, Complex &b) {return Complex((1ll * a.r * b.r % mod + 1ll * a.i * b.i % mod * I2 % mod) % mod, (1ll * a.r * b.i % mod + 1ll * a.i * b.r % mod) % mod);}Complex quick_pow(Complex a, int n) {Complex ans = Complex(1, 0);while (n) {if (n & 1) {ans = ans * a;}a = a * a;n >>= 1;}return ans;}int get_residue(int n) {mt19937 e(233);if (n == 0) {return 0;}if(quick_pow(n, (mod - 1) >> 1).r == mod - 1) {return -1;}uniform_int_distribution<int> r(0, mod - 1);int a = r(e);while(quick_pow((1ll * a * a % mod - n + mod) % mod, (mod - 1) >> 1).r == 1) {a = r(e);}I2 = (1ll * a * a % mod - n + mod) % mod;int x = quick_pow(Complex(a, 1), (mod + 1) >> 1).r, y = mod - x;if(x > y) swap(x, y);return x;}
}const int N = 1e6 + 10;int r[N], inv[N], b[N], c[N], d[N], e[N], t[N];int quick_pow(int a, int n) {int ans = 1;while (n) {if (n & 1) {ans = 1ll * a * ans % mod;}a = 1ll * a * a % mod;n >>= 1;}return ans;
}void get_r(int lim) {for (int i = 0; i < lim; i++) {r[i] = (i & 1) * (lim >> 1) + (r[i >> 1] >> 1);}
}void get_inv(int n) {inv[1] = 1;for (int i = 2; i <= n; i++) {inv[i] = 1ll * (mod - mod / i) * inv[mod % i] % mod;}
}void NTT(int *f, int lim, int rev) {for (int i = 0; i < lim; i++) {if (i < r[i]) {swap(f[i], f[r[i]]);}}for (int mid = 1; mid < lim; mid <<= 1) {int wn = quick_pow(3, (mod - 1) / (mid << 1));for (int len = mid << 1, cur = 0; cur < lim; cur += len) {int w = 1;for (int k = 0; k < mid; k++, w = 1ll * w * wn % mod) {int x = f[cur + k], y = 1ll * w * f[cur + mid + k] % mod;f[cur + k] = (x + y) % mod, f[cur + mid + k] = (x - y + mod) % mod;}}}if (rev == -1) {int inv = quick_pow(lim, mod - 2);reverse(f + 1, f + lim);for (int i = 0; i < lim; i++) {f[i] = 1ll * f[i] * inv % mod;}}
}void polyinv(int *f, int *g, int n) {if (n == 1) {g[0] = quick_pow(f[0], mod - 2);return ;}polyinv(f, g, n + 1 >> 1);for (int i = 0; i < n; i++) {t[i] = f[i];}int lim = 1;while (lim < 2 * n) {lim <<= 1;}get_r(lim);NTT(t, lim, 1);NTT(g, lim, 1);for (int i = 0; i < lim; i++) {int cur = (2 - 1ll * g[i] * t[i] % mod + mod) % mod;g[i] = 1ll * g[i] * cur % mod;t[i] = 0;}NTT(g, lim, -1);for (int i = n; i < lim; i++) {g[i] = 0;}
}void polysqrt(int *f, int *g, int n) {if (n == 1) {g[0] = Quadratic_residue::get_residue(f[0]);return ;}polysqrt(f, g, n + 1 >> 1);polyinv(g, b, n);int lim = 1;while (lim < 2 * n) {lim <<= 1;}get_r(lim);for (int i = 0; i < n; i++) {t[i] = f[i];}NTT(g, lim, 1);NTT(b, lim, 1);NTT(t, lim, 1);for (int i = 0; i < lim; i++) {g[i] = (1ll * inv2 * g[i] % mod + 1ll * inv2 * b[i] % mod * t[i] % mod) % mod;b[i] = t[i] = 0;}NTT(g, lim, -1);for (int i = n; i < lim; i++) {g[i] = 0;}
}void derivative(int *a, int *b, int n) {for (int i = 0; i < n; i++) {b[i] = 1ll * a[i + 1] * (i + 1) % mod;}
}void integrate(int *a, int n) {for (int i = n - 1; i >= 1; i--) {a[i] = 1ll * a[i - 1] * inv[i] % mod;}a[0] = 0;
}void polyln(int *f, int *g, int n) {polyinv(f, b, n);derivative(f, g, n);int lim = 1;while (lim < 2 * n) {lim <<= 1;}get_r(lim);NTT(g, lim, 1);NTT(b, lim, 1);for (int i = 0; i < lim; i++) {g[i] = 1ll * g[i] * b[i] % mod;b[i] = 0;}NTT(g, lim, -1);for (int i = n; i < lim; i++) {g[i] = 0;}integrate(g, n);
}void polyexp(int *f, int *g, int n) {if (n == 1) {g[0] = 1;return ;}polyexp(f, g, n + 1 >> 1);int lim = 1;while (lim < 2 * n) {lim <<= 1;}polyln(g, d, n);for (int i = 0; i < n; i++) {t[i] = (f[i] - d[i] + mod) % mod;}t[0] = (t[0] + 1) % mod;get_r(lim);NTT(g, lim, 1);NTT(t, lim, 1);for (int i = 0; i < lim; i++) {g[i] = 1ll * g[i] * t[i] % mod;t[i] = d[i] = 0;}NTT(g, lim, -1);for (int i = n; i < lim; i++) {g[i] = 0;}
}/*b存放多项式逆,c存放多项式开根,d存放多项式对数ln,e存放多项式指数exp,t作为中间转移数组,如果要用到polyinv,得提前调用get_inv(n)先预先得到我们想要得到的逆元范围。
*/int n, m, T, sa[N], sb[N], f1[20][N], f2[20][N], fac[N], ifac[N];void solve(int *f, int l, int r, int cur) {if (l == r) {f1[cur][0] = 1, f1[cur][1] = mod - f[l];return ;}int mid = l + r >> 1, len1 = mid - l + 1, len2 = r - mid;solve(f, l, mid, cur + 1);for (int i = 0; i <= len1; i++) {f2[cur + 1][i] = f1[cur + 1][i];f1[cur + 1][i] = 0;}solve(f, mid + 1, r, cur + 1);int lim = 1;while (lim <= r - l + 1) {lim <<= 1;}get_r(lim);NTT(f1[cur + 1], lim, 1);NTT(f2[cur + 1], lim, 1);for (int i = 0; i < lim; i++) {f1[cur][i] = 1ll * f1[cur + 1][i] * f2[cur + 1][i] % mod;f1[cur + 1][i] = f2[cur + 1][i] = 0;}NTT(f1[cur], lim, -1);
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);scanf("%d %d", &n, &m);for (int i = 1; i <= n; i++) {scanf("%d", &sa[i]);}for (int i = 1; i <= m; i++) {scanf("%d", &sb[i]);}scanf("%d", &T);int maxn = max({n, m, T});fac[0] = ifac[0] = 1;for (int i = 1; i <= 2 * maxn; i++) {fac[i] = 1ll * fac[i - 1] * i % mod;}ifac[2 * maxn] = quick_pow(fac[2 * maxn], mod - 2);for (int i = 2 * maxn - 1; i >= 1; i--) {ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;}get_inv(4 * maxn);solve(sa, 1, maxn, 0);for (int i = 0; i <= maxn; i++) {sa[i] = f1[0][i];f1[0][i] = 0;}polyln(sa, d, maxn + 1);for (int i = 0; i <= maxn; i++) {sa[i] = mod - 1ll * i * d[i] % mod;sa[i] = 1ll * sa[i] * ifac[i] % mod;d[i] = 0;}sa[0] = (sa[0] + n) % mod;solve(sb, 1, maxn, 0);for (int i = 0; i <= maxn; i++) {sb[i] = f1[0][i];f1[0][i] = 0;}polyln(sb, d, maxn + 1);for (int i = 0; i <= maxn; i++) {sb[i] = mod - 1ll * i * d[i] % mod;sb[i] = 1ll * sb[i] * ifac[i] % mod;d[i] = 0;}sb[0] = (sb[0] + m) % mod;int lim = 1;while (lim <= 2 * maxn) {lim <<= 1;}get_r(lim);NTT(sa, lim, 1);NTT(sb, lim, 1);for (int i = 0; i < lim; i++) {sa[i] = 1ll * sa[i] * sb[i] % mod;}NTT(sa, lim, -1);int inv = quick_pow(1ll * n * m % mod, mod - 2);for (int i = 1; i <= T; i++) {printf("%d\n", 1ll * inv * sa[i] % mod * fac[i] % mod);}return 0;
}