多项式求逆
记 。
那么 可以直接求常数项的逆元得出。
那么有:
时间复杂度为 。
多项式取模
求 的结果。
设 ,则记 。容易发现 。
那么,设 ,显然有
由于 ,所以有关系式 。再把 代回去即可。
多项式多点求值
给定长度为 的多项式 和 个非负整数 。对于所有 求 的值。
考虑使用分治。考虑把 分成 和 两部分。
先考虑左半边。构造 ,也即 。再考虑把 表示为 ,也即 ,则有 。而 的长度不会大于 的长度, 的长度又是 的一半,所以就可以用多项式取模求出 并递归处理了。右半边也是同理的。总时间复杂度 。
拉格朗日插值
给出平面上横坐标两两不同的 个点 ,求一个小于 次的多项式 过这些点。
考虑构造一个 满足 。一个满足条件的多项式是 。
那么有
多项式快速插值
给出平面上横坐标两两不同的 个点 ,求一个小于 次的多项式 过这些点。
根据拉格朗日插值公式,我们已经知道了 。
为了转化这个式子,我们引入洛必达法则:
记多项式 ,那么根据洛必达法则有
所以有
可以分治 NTT 在 的时间内求出, 又可以用多点求值在 的时间内求出。
现在考虑分治求出 。对于某个分治区间 ,令
不难发现 ,于是也可递归计算。时间复杂度也是 ,所以总时间复杂度是 。
然后你不难写出以下朴素代码:
#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 = 1e5 + 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] + b[i]);
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;
}
inline poly polyd(const poly &a)
{
poly b(a.size() - 1);
for (int i = 1; i < (int)(a.size()); ++i)
b[i - 1] = (long long)(a[i]) * i % mod;
return b;
}
struct Point { int x, y; } a[MAXN];
int n;
poly f, g;
std::vector<int> g_;
namespace SgT
{
struct SgTNode { poly f, g; } 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].g = (poly){mod - a[l].x, 1});
int mid = (l + r) >> 1;
build(lson), build(rson), tr[rt].g = tr[ls].g * tr[rs].g;
}
void eval(const poly &F, std::vector<int> &res, const int &l, const int &r, const int &rt)
{
poly f = F % tr[rt].g;
if (l == r)
return (void)(res[l] = f[0]);
int mid = (l + r) >> 1;
eval(f, res, lson), eval(f, res, rson);
}
void interp(const int &l, const int &r, const int &rt)
{
if (l == r)
return (void)(tr[rt].f = (poly){(int)((long long)(a[l].y) * qpow(g_[l], mod - 2) % mod)});
int mid = (l + r) >> 1;
interp(lson), interp(rson), tr[rt].f = tr[ls].f * tr[rs].g + tr[ls].g * tr[rs].f;
}
#undef ls
#undef rs
#undef lson
#undef rson
}
int main()
{
read(n), g_.resize(n + 1);
for (int i = 1; i <= n; ++i)
read(a[i].x), read(a[i].y);
SgT::build(1, n, 1), g = polyd(SgT::tr[1].g), SgT::eval(g, g_, 1, n, 1), SgT::interp(1, n, 1);
for (const int &i : SgT::tr[1].f)
printf("%d ", i);
return 0;
}但是非常悲伤的是,此代码常数太大,未能通过此题!
考虑到多项式取模是非常慢的,考虑卡多项式取模。我们设一个阈值 ,当多点求值的区间长度 的时候,就直接暴力展开多项式。随便设了一个 就过了:
#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 = 1e5 + 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] + b[i]);
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;
}
inline poly polyd(const poly &a)
{
poly b(a.size() - 1);
for (int i = 1; i < (int)(a.size()); ++i)
b[i - 1] = (long long)(a[i]) * i % mod;
return b;
}
struct Point { int x, y; } a[MAXN];
int n;
poly f, g;
std::vector<int> g_;
namespace SgT
{
struct SgTNode { poly f, g; } tr[MAXN << 2];
const int B = 325;
#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].g = (poly){mod - a[l].x, 1});
int mid = (l + r) >> 1;
build(lson), build(rson), tr[rt].g = tr[ls].g * tr[rs].g;
}
void eval(const poly &F, std::vector<int> &res, const int &l, const int &r, const int &rt)
{
poly f = F % tr[rt].g;
// if (l == r)
// return (void)(res[l] = f[0]);
if (r - l + 1 <= B)
{
for (int i = l; i <= r; ++i)
for (int j = 0, x = 1; j < (int)(f.size()); ++j, x = (long long)(x) * a[i].x % mod)
res[i] = (res[i] + (long long)(f[j]) * x) % mod;
return;
}
int mid = (l + r) >> 1;
eval(f, res, lson), eval(f, res, rson);
}
void interp(const int &l, const int &r, const int &rt)
{
if (l == r)
return (void)(tr[rt].f = (poly){(int)((long long)(a[l].y) * qpow(g_[l], mod - 2) % mod)});
int mid = (l + r) >> 1;
interp(lson), interp(rson), tr[rt].f = tr[ls].f * tr[rs].g + tr[ls].g * tr[rs].f;
}
#undef ls
#undef rs
#undef lson
#undef rson
}
int main()
{
read(n), g_.resize(n + 1);
for (int i = 1; i <= n; ++i)
read(a[i].x), read(a[i].y);
SgT::build(1, n, 1), g = polyd(SgT::tr[1].g), SgT::eval(g, g_, 1, n, 1), SgT::interp(1, n, 1);
for (const int &i : SgT::tr[1].f)
printf("%d ", i);
return 0;
}
// 躯体在时间的长河里漂流着,逝去的灵魂消散在背后,寄生的灵魂等待在前方。记得者逝去了,但也会有人记得记得者。我们终将被遗忘,但我们会永远被记住。
// 7