多项式求逆
记 。
那么 可以直接求常数项的逆元得出。
那么有:
时间复杂度为 。
多项式取模
求 的结果。
设 ,则记 。容易发现 。
那么,设 ,显然有
由于 ,所以有关系式 。再把 代回去即可。
多项式多点求值
给定长度为 的多项式 和 个非负整数 。对于所有 求 的值。
考虑使用分治。考虑把 分成 和 两部分。
先考虑左半边。构造 ,也即 。再考虑把 表示为 ,也即 ,则有 。而 的长度不会大于 的长度, 的长度又是 的一半,所以就可以用多项式取模求出 并递归处理了。右半边也是同理的。总时间复杂度 。
#include <cstdio>
#include <vector>
#include <algorithm>
#define bit_ceil(x) ((x) <= 1 ? 1 : (1 << (32 - __builtin_clz((x) - 1))))
typedef std::vector<int> poly;
namespace FastIn
{
const int S = 4e6 + 7;
char *p1, *p2, buf[S];
#define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, S, stdin), p1 == p2) ? EOF : *p1++)
inline void read(int &x)
{
x = 0;
char c = gc();
while (c < '0' || c > '9')
c = gc();
for (; c >= '0' && c <= '9'; c = gc())
x = (x << 3) + (x << 1) + (c & 15);
}
#undef gc
}
using FastIn::read;
const int MAXN = 6.4e4 + 7, mod = 998244353;
inline int M(const int &x) { return x >= mod ? x - mod : x; }
inline int qpow(int a, int b)
{
int res = 1;
for (; b; a = (long long)(a) * a % mod, b >>= 1)
(b & 1) && (res = (long long)(res) * a % mod);
return res;
}
inline void NTT(poly &a, const int &rvs)
{
static std::vector<int> rev;
int n = a.size(); (int)(rev.size()) < n && (rev.resize(n), true);
for (int i = 0; i < n; ++i)
if (i < (rev[i] = (rev[i >> 1] >> 1) | ((i & 1) * (n >> 1))))
std::swap(a[i], a[rev[i]]);
for (int len = 1, W; len < n; len <<= 1)
{
W = qpow(rvs == 1 ? 3 : 332748118, (mod - 1) / (len << 1));
for (int i = 0; i < n; i += (len << 1))
for (int j = i, f0, f1, w = 1; j < i + len; ++j, w = (long long)(w) * W % mod)
f0 = a[j], f1 = (long long)(w) * a[j + len] % mod, a[j] = M(f0 + f1), a[j + len] = M(f0 + mod - f1);
}
if (rvs == -1)
for (int i = 0, invn = qpow(n, mod - 2); i < n; ++i)
a[i] = (long long)(a[i]) * invn % mod;
}
inline poly operator*(const poly &A, const poly &B)
{
int n = A.size() + B.size() - 1, len = bit_ceil(n);
poly a = A, b = B;
a.resize(len), b.resize(len), NTT(a, 1), NTT(b, 1);
for (int i = 0; i < len; ++i)
a[i] = (long long)(a[i]) * b[i] % mod;
return NTT(a, -1), a.resize(n), a;
}
inline poly operator*(poly a, const int &b)
{
for (int &i : a)
i = (long long)(i) * b % mod;
return a;
}
inline poly operator-(const poly &A, const poly &B)
{
poly a = A, b = B;
a.resize(std::max(A.size(), B.size())), b.resize(std::max(A.size(), B.size()));
for (int i = 0; i < (int)(a.size()); ++i)
a[i] = M(a[i] + mod - b[i]);
return a;
}
inline poly polyinv(const poly &a, const int &n)
{
poly inv = (poly){qpow(a[0], mod - 2)};
for (int len = 1; len < n; len <<= 1)
{
poly f(a.begin(), a.begin() + std::min(len << 1, (int)(a.size()))), tmp = inv * inv * f;
inv.resize(len << 1), tmp.resize(len << 1), inv = inv * 2 - tmp;
}
return inv.resize(n), inv;
}
inline poly operator%(const poly &A, const poly &B)
{
poly a = A, b = B, q, r;
int n = a.size(), m = b.size();
if (n < m)
return a;
std::reverse(a.begin(), a.end()), std::reverse(b.begin(), b.end()),
q = a * polyinv(b, n - m + 1), q.resize(n - m + 1),
std::reverse(a.begin(), a.end()), std::reverse(b.begin(), b.end()), std::reverse(q.begin(), q.end());
return r = a - b * q, r.resize(m - 1), r;
}
int n, m, a[MAXN];
poly f;
namespace SgT
{
poly tr[MAXN << 2];
#define ls rt << 1
#define rs (rt << 1) | 1
#define lson l, mid, rt << 1
#define rson mid + 1, r, (rt << 1) | 1
void build(const int &l, const int &r, const int &rt)
{
if (l == r)
return (void)(tr[rt] = (poly){mod - a[l], 1});
int mid = (l + r) >> 1;
build(lson), build(rson), tr[rt] = tr[ls] * tr[rs];
}
void eval(const poly &F, const int &l, const int &r, const int &rt)
{
poly f = F % tr[rt]; // f(x) = g(x)q(x) + r(x) 对应到此处是 F(x) = tr[rt](x)q(x) + r(x)
if (l == r) // 递归到最底层,f 的长度一定为 1,因为 tr[rt] 的长度是 2,所以 f 的长度是 2 - 1 = 1
return (void)(a[l] = f[0]);
int mid = (l + r) >> 1;
eval(f, lson), eval(f, rson);
}
#undef ls
#undef rs
#undef lson
#undef rson
}
int main()
{
read(n), read(m), f.resize(n + 1);
for (int i = 0; i <= n; ++i)
read(f[i]);
for (int i = 1; i <= m; ++i)
read(a[i]);
SgT::build(1, m, 1), SgT::eval(f, 1, m, 1);
for (int i = 1; i <= m; ++i)
printf("%d\n", a[i]);
return 0;
}
// 曾被困锁在深渊的囚笼之中,方能珍重默默无闻的花草;曾经孤行于黯然的道路之上,方能留下熠熠发光的文字
// 6