P3711 仓鼠的数学题
有关伯努利数的知识可以看我的上一篇题解链接(写的超详细)。
F(x)=∑k=0nSk(x)ak原本定义的Sk(x)=∑i=0xik根据伯努利数的定义Sk′(x)=∑i=0x−1ik则我们求F(x)=∑k=0nSk′(x)ak,答案即为F(x+1)考虑先求F(x)=∑k=0nak1k+1∑i=0kCk+1iBixk−i+1∑k=0nakk+1∑i=0k(k+1)!i!(k+1−i)!Bixk−i+1∑k=0nakk!∑i=0kBii!xk−i+1(k−i+1)!∑i=1n+1xii!∑k=i−1n(akk!)(Bk+1−i(k+1−i)!)设f(n)=akk!,g(n)=Bnn!,另g(n)=g(n−i)即翻转一下∑i=1n+1xii!∑k=i−1nf(k)g(n+i−k)容易发现后面是一个卷积,所以可以优化F(x) = \sum_{k = 0} ^{n} S_k(x) a_k\\ 原本定义的S_k(x) = \sum_{i = 0} ^{x} i ^ k\\ 根据伯努利数的定义S'_k(x) = \sum_{i = 0} ^{x - 1} i ^ k\\ 则我们求F(x) = \sum_{k = 0} ^{n} S'_k(x) a_k, 答案即为F(x + 1)\\ 考虑先求F(x) = \sum_{k = 0} ^{n} a_k \frac{1}{k + 1} \sum\limits_{i = 0} ^{k}C_{k + 1} ^{i}B_i x ^{k - i + 1}\\ \sum_{k = 0} ^{n} \frac{a_k}{k + 1} \sum_{i = 0} ^{k} \frac{(k + 1)!}{i!(k + 1 - i)!} B_i x ^{k - i + 1}\\ \sum_{k = 0} ^{n} a_k k! \sum_{i = 0} ^{k} \frac{B_i}{i!}\frac{x ^{k - i + 1}}{(k - i + 1)!}\\ \sum_{i = 1} ^{n + 1} \frac{x ^{i}}{i!} \sum_{k = i - 1} ^{n} \left(a_k k!\right) \left( \frac{B_{k + 1 - i}}{(k + 1 - i)!} \right)\\ 设f(n) = a_k k!, g(n) = \frac{B_{n}}{n!}, 另g(n) = g(n - i)即翻转一下\\ \sum_{i = 1} ^{n + 1} \frac{x ^ i}{i!} \sum_{k = i - 1} ^{n} f(k) g(n + i - k)\\ 容易发现后面是一个卷积,所以可以优化\\ F(x)=k=0∑nSk(x)ak原本定义的Sk(x)=i=0∑xik根据伯努利数的定义Sk′(x)=i=0∑x−1ik则我们求F(x)=k=0∑nSk′(x)ak,答案即为F(x+1)考虑先求F(x)=k=0∑nakk+11i=0∑kCk+1iBixk−i+1k=0∑nk+1aki=0∑ki!(k+1−i)!(k+1)!Bixk−i+1k=0∑nakk!i=0∑ki!Bi(k−i+1)!xk−i+1i=1∑n+1i!xik=i−1∑n(akk!)((k+1−i)!Bk+1−i)设f(n)=akk!,g(n)=n!Bn,另g(n)=g(n−i)即翻转一下i=1∑n+1i!xik=i−1∑nf(k)g(n+i−k)容易发现后面是一个卷积,所以可以优化
下面的n=n+1n = n + 1n=n+1,由上易知F(x)F(x)F(x)是一个n+1n + 1n+1次多项式。
设F(x)=∑i=0naixiF(x+1)=∑i=0nai(x+1)i∑i=0nai∑j=0iCjixj∑i=0nai∑j=0ii!j!(i−j)!xj∑i=0nxii!∑j=inajj!1(j−i)!另f(n)=ann!,g(n)=1n!,再翻转g(n)∑i=0nxii!∑j=inf(j)g(n+i−j)后面也同样是一个卷积,所以可以优化设F(x) = \sum_{i = 0} ^{n} a_i x ^ i\\ F(x + 1) = \sum_{i = 0} ^{n} a_i (x + 1) ^ i\\ \sum_{i = 0} ^{n} a_i \sum_{j = 0} ^{i} C_{j} ^{i} x ^ j\\ \sum_{i = 0} ^{n} a_i \sum_{j = 0} ^{i} \frac{i!}{j!(i - j)!} x ^ j\\ \sum_{i = 0} ^{n} \frac{x ^ i}{i!} \sum_{j = i} ^{n} a_j j! \frac{1}{(j - i)!}\\ 另f(n) = a_n n!, g(n) = \frac{1}{n!},再翻转g(n)\\ \sum_{i = 0} ^{n} \frac{x ^{i}}{i!} \sum_{j = i} ^{n} f(j) g(n + i - j)\\ 后面也同样是一个卷积,所以可以优化\\ 设F(x)=i=0∑naixiF(x+1)=i=0∑nai(x+1)ii=0∑naij=0∑iCjixji=0∑naij=0∑ij!(i−j)!i!xji=0∑ni!xij=i∑najj!(j−i)!1另f(n)=ann!,g(n)=n!1,再翻转g(n)i=0∑ni!xij=i∑nf(j)g(n+i−j)后面也同样是一个卷积,所以可以优化
#include <bits/stdc++.h>using namespace std;const int N = 1e6 + 10, mod = 998244353;int r[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 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;}
}int fac[N], ifac[N], a[N], f[N], g[N], B[N], n;void init() {fac[0] = 1;for (int i = 1; i < N; i++) {fac[i] = 1ll * fac[i - 1] * i % mod;}ifac[N - 1] = quick_pow(fac[N - 1], mod - 2);for (int i = N - 2; i >= 0; i--) {ifac[i] = 1ll * ifac[i + 1] * (i + 1) % mod;}
}int main() {// freopen("in.txt", "r", stdin);// freopen("out.txt", "w", stdout);// ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);init();scanf("%d", &n);for (int i = 0; i <= n; i++) {scanf("%d", &a[i]);}for (int i = 0; i <= n + 1; i++) {B[i] = ifac[i + 1];}polyinv(B, g, n + 2);for (int i = 0; i <= n; i++) {f[i] = 1ll * a[i] * fac[i] % mod;}reverse(g, g + n + 2);int lim = 1;while (lim < 2 * n + 10) {lim <<= 1;}get_r(lim);NTT(f, lim, 1), NTT(g, lim, 1);for (int i = 0; i < lim; i++) {f[i] = 1ll * f[i] * g[i] % mod;}NTT(f, lim, -1);for (int i = 1; i <= n + 1; i++) {a[i] = 1ll * ifac[i] * f[n + i] % mod;}a[0] = 0;n++;for (int i = 0; i < lim; i++) {f[i] = g[i] = 0;}for (int i = 0; i <= n; i++) {f[i] = 1ll * a[i] * fac[i] % mod;g[i] = ifac[n - i];}NTT(f, lim, 1), NTT(g, lim, 1);for (int i = 0; i < lim; i++) {f[i] = 1ll * f[i] * g[i] % mod;}NTT(f, lim, -1);for (int i = 0; i <= n; i++) {printf("%lld%c", 1ll * ifac[i] * f[n + i] % mod, i == n ? '\n' : ' ');}return 0;
}