洛谷5月月赛T30212 玩游戏 【分治NTT + 多项式求ln】
题目链接
题解
式子很容易推出来,二项式定理展开后对于\(k\)的答案即可化简为如下:
\[k!(\sum\limits_{i = 0}^{k} \frac{\sum\limits_{x = 1}^{n} a_x^{i}}{i!} \centerdot \frac{\sum\limits_{x = 1}^{n} b_x^{k - i}}{(k - i)!})
\]
是一个卷积的形式
我们只需对所有\(k\)预处理出\(\sum\limits_{i = 1}^{n} a_i^{k}\),\(b\)也是类似的
月赛时并不会,暴力预处理便滚粗了,,
考虑泰勒展开,有这样一个式子:
\[ln(1 + x) = \sum\limits_{i = 0}^{\infty} (-1)^{i} \frac{x^{i + 1}}{i + 1}
\]
我们令\(x = ax\)
则
\[ln(1 + ax) = \sum\limits_{i = 0}^{\infty} (-1)^{i} \frac{a^{i + 1}}{i + 1}x^{i + 1}
\]
出现了我们想要的\(a_i^{k}\)
我们只需求出
\[\sum\limits_{i = 1}^{n} ln(1 + a_ix) = ln(\prod\limits_{i = 1}^{n} (1 + a_ix))
\]
则\(x^k\)对应的系数就是\(\frac{(-1)^{k - 1}\sum\limits_{i = 1}^{n}a_i^{k}}{k}\)
分治\(NTT\) + 多项式求\(ln\)即可
复杂度\(O(nlog^2n)\)
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 400005,maxm = 100005,INF = 1000000000;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
const int G = 3,P = 998244353;
int R[maxn];
inline int qpow(int a,LL b){
int re = 1;
for (; b; b >>= 1,a = 1ll * a * a % P)
if (b & 1) re = 1ll * re * a % P;
return re;
}
void NTT(int* a,int n,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 gn = qpow(G,(P - 1) / (i << 1));
for (int j = 0; j < n; j += (i << 1)){
int g = 1,x,y;
for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
x = a[j + k],y = 1ll * g * a[j + k + i] % P;
a[j + k] = (x + y) % P,a[j + k + i] = ((x - y) % P + P) % P;
}
}
}
if (f == 1) return;
int nv = qpow(n,P - 2); reverse(a + 1,a + n);
for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
int n,m,a[maxn],b[maxn],c[maxn],A[maxn],B[maxn],cv[maxn],N;
int fac[maxn],fv[maxn],inv[maxn];
void init(){
fac[0] = fac[1] = fv[0] = fv[1] = inv[0] = inv[1] = 1;
for (int i = 2; i <= 100000; i++){
fac[i] = 1ll * fac[i - 1] * i % P;
inv[i] = 1ll * (P - P / i) * inv[P % i] % P;
fv[i] = 1ll * fv[i - 1] * inv[i] % P;
}
}
int F[30][maxn],deg[maxn],cnt;
void solve(int l,int r){
if (l == r){
deg[++cnt] = 1;
F[cnt][0] = 1; F[cnt][1] = c[l];
return;
}
int mid = l + r >> 1;
solve(l,mid); solve(mid + 1,r);
int n = 1,L = 0,a = cnt - 1,b = cnt,m = deg[a] + deg[b];
while (n <= m) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = deg[a] + 1; i < n; i++) F[a][i] = 0;
for (int i = deg[b] + 1; i < n; i++) F[b][i] = 0;
NTT(F[a],n,1); NTT(F[b],n,1);
for (int i = 0; i < n; i++) F[a][i] = 1ll * F[a][i] * F[b][i] % P;
NTT(F[a],n,-1);
cnt--;
deg[cnt] = m;
for (int i = m + 1; i < n; i++) F[cnt][i] = 0;
}
void Der(int* a,int n){
for (int i = 0; i < n; i++) a[i] = 1ll * a[i + 1] * (i + 1) % P;
a[n] = 0;
}
void Int(int* a,int n){
for (int i = n + 1; i; i--) a[i] = 1ll * a[i - 1] * inv[i] % P;
a[0] = 0;
}
void Inv(int* a,int* b,int deg){
if (deg == 1){b[0] = qpow(a[0],P - 2); return;}
Inv(a,b,(deg + 1) >> 1);
int n = 1,L = 0;
while (n < (deg << 1)) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = 0; i < deg; i++) c[i] = a[i];
for (int i = deg; i < n; i++) c[i] = 0;
NTT(c,n,1); NTT(b,n,1);
for (int i = 0; i < n; i++)
b[i] = 1ll * ((2ll - 1ll * b[i] * c[i] % P) % P + P) % P * b[i] % P;
NTT(b,n,-1);
for (int i = deg; i < n; i++) b[i] = 0;
}
void Getln(int* a,int* b,int deg){
Inv(a,cv,deg);
Der(a,deg);
int n = 1,L = 0;
while (n <= (deg << 1)) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = deg; i < n; i++) a[i] = 0;
NTT(a,n,1); NTT(cv,n,1);
for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * cv[i] % P;
NTT(a,n,-1);
for (int i = 0; i <= deg; i++) b[i] = a[i];
Int(b,deg);
}
int main(){
init();
n = read(); m = read(); int v = qpow(1ll * n * m % P,P - 2);
REP(i,n) a[i] = read();
REP(i,m) b[i] = read();
N = read();
REP(i,n) c[i] = a[i];
solve(1,n);
//REP(i,n) printf("%d ",F[1][i]); puts("");
for (int i = n + 1; i <= N; i++) F[1][i] = 0;
Getln(F[1],A,N);
for (int i = 1; i <= N; i++){
if (!(i & 1)) A[i] = P - A[i];
A[i] = 1ll * A[i] * i % P * fv[i] % P;
}
//REP(i,N) printf("%d ",A[i]); puts("");
A[0] = n;
cls(cv);
REP(i,m) c[i] = b[i]; cnt = 0;
solve(1,m);
for (int i = m + 1; i <= N; i++) F[1][i] = 0;
Getln(F[1],B,N);
for (int i = 1; i <= N; i++){
if (!(i & 1)) B[i] = P - B[i];
B[i] = 1ll * B[i] * i % P * fv[i] % P;
}
//REP(i,N) printf("%d ",B[i]); puts("");
B[0] = m;
int n = 1,L = 0;
while (n <= (N << 1)) n <<= 1,L++;
for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
for (int i = N + 1; i < n; i++) A[i] = B[i] = 0;
NTT(A,n,1); NTT(B,n,1);
for (int i = 0; i < n; i++) A[i] = 1ll * A[i] * B[i] % P;
NTT(A,n,-1);
for (int i = 1; i <= N; i++)
printf("%lld\n",1ll * A[i] * fac[i] % P * v % P);
return 0;
}