\(\color{#0066ff}{ 题目描述 }\)
给定长度为 \(n-1\) 的数组 \(g[1],g[2],..,g[n-1]\),求 \(f[0],f[1],..,f[n-1]\),其中
\(f[i]=\sum_{j=1}^if[i-j]g[j]\)
边界为 \(f[0]=1\) 。答案模 \(998244353\) 。
\(\color{#0066ff}{输入格式}\)
第一行一个正整数 \(n\) 。
第二行共 \(n-1\) 个非负整数 \(g[1],g[2],..,g[n-1]\),用空格隔开。
\(\color{#0066ff}{输出格式}\)
一行共 \(n\) 个非负整数,表示 \(f[0],f[1],..,f[n-1]\) 模 \(998244353\) 的值。
\(\color{#0066ff}{输入样例}\)
4
3 1 2
10
2 456 32 13524543 998244352 0 1231 634544 51
\(\color{#0066ff}{输出样例}\)
1 3 10 35
1 2 460 1864 13738095 55389979 617768468 234028967 673827961 708520894
\(\color{#0066ff}{数据范围与提示}\)
\(2≤n≤10^5\)
\(0\leq g[i]<998244353\)
\(\color{#0066ff}{题解}\)
然而这题可以用多项式求逆过(雾
显然可以看出\(f*g=f-f_0\)
然后。。。\(f=\frac{1}{1-g}\)
求个逆就没了。。
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 4e5 + 10;
const int mod = 998244353;
using std::vector;
int n, len, r[maxn];
LL ksm(LL x, LL y) {
LL re = 1LL;
while(y) {
if(y & 1) re = re * x % mod;
x = x * x % mod;
y >>= 1;
}
return re;
}
void FFT(vector<int> &A, int flag) {
A.resize(len);
for(int i = 0; i < len; i++) if(i < r[i]) std::swap(A[i], A[r[i]]);
for(int l = 1; l < len; l <<= 1) {
int w0 = ksm(3, (mod - 1) / (l << 1));
for(int i = 0; i < len; i += (l << 1)) {
int w = 1, a0 = i, a1 = i + l;
for(int k = 0; k < l; k++, a0++, a1++, w = 1LL * w0 * w % mod) {
int tmp = 1LL * w * A[a1] % mod;
A[a1] = ((A[a0] - tmp) % mod + mod) % mod;
A[a0] = (A[a0] + tmp) % mod;
}
}
}
if(!(~flag)) {
std::reverse(A.begin() + 1, A.end());
int inv = ksm(len, mod - 2);
for(int i = 0; i < len; i++) A[i] = 1LL * A[i] * inv % mod;
}
}
vector<int> operator * (vector<int> A, vector<int> B) {
int tot = A.size() + B.size() - 1;
for(len = 1; len <= tot; len <<= 1);
for(int i = 0; i < len; i++) r[i] = (r[i >> 1] >> 1) | ((i & 1) * (len >> 1));
FFT(A, 1), FFT(B, 1);
vector<int> ans;
for(int i = 0; i < len; i++) ans.push_back(1LL * A[i] * B[i] % mod);
FFT(ans, -1);
ans.resize(tot);
return ans;
}
vector<int> operator - (const vector<int> &A, const vector<int> &B) {
vector<int> ans;
for(int i = 0; i < (int)std::min(A.size(), B.size()); i++) ans.push_back(A[i] - B[i]);
for(int i = A.size(); i < (int)B.size(); i++) ans.push_back(-B[i]);
for(int i = B.size(); i < (int)A.size(); i++) ans.push_back(A[i]);
return ans;
}
vector<int> inv(const vector<int> &A) {
if(A.size() == 1) {
vector<int> ans;
ans.push_back(ksm(A[0], mod - 2));
return ans;
}
vector<int> ans, B = A;
int n = A.size(), _ = (n + 1) >> 1;
B.resize(_);
ans.push_back(2);
B = inv(B);
ans = B * (ans - A * B);
ans.resize(n);
return ans;
}
int main() {
int n = in();
vector<int> a;
a.push_back(1);
for(int i = 1; i < n; i++) a.push_back(mod - in());
a = inv(a);
for(int i = 0; i < n; i++) printf("%d%c", a[i], i == n - 1? '\n' : ' ');
return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10423604.html
时间: 2024-10-08 18:37:36