This documentation is automatically generated by online-judge-tools/verification-helper
#include "lib/polynomial/polynomial_interpolation.hpp"
#pragma once
/**
* @brief Polynomial Interpolation (多項式補間)
* @see https://37zigen.com/lagrange-interpolation/
*/
#include "../polynomial/formal_power_series.hpp"
#include "../polynomial/multipoint_evaluation.hpp"
#include <vector>
template <typename T>
FormalPowerSeries<T> polynomialInterpolation(const std::vector<T> &x, const std::vector<T> &y){
using FPS = FormalPowerSeries<T>;
assert(x.size() == y.size());
int n = 1;
while(n < (int) x.size()) n *= 2;
std::vector<FPS> product_tree(n * 2, FPS({T(1)}));
for(int i = 0; i < (int) x.size(); i++){
product_tree[n + i] = {-x[i], T(1)};
}
for(int i = n - 1; i >= 1; i--){
product_tree[i] = product_tree[i * 2] * product_tree[i * 2 + 1];
}
// g = procuct_tree[1]
FPS g_d = product_tree[1].diff();
std::vector<T> p = multipointEvalutation(g_d, x);
std::vector<FPS> f(n * 2, FPS({T(0)}));
for(int i = 0; i < (int) x.size(); i++){
f[n + i] = FPS({y[i] / p[i]});
}
for(int i = n - 1; i >= 1; i--){
// 通分
f[i] = (f[i * 2] * product_tree[i * 2 + 1]) + (f[i * 2 + 1] * product_tree[i * 2]);
}
// g * f / g = f
return f[1].pre(n);
}
#line 2 "lib/polynomial/polynomial_interpolation.hpp"
/**
* @brief Polynomial Interpolation (多項式補間)
* @see https://37zigen.com/lagrange-interpolation/
*/
#line 2 "lib/polynomial/formal_power_series.hpp"
/**
* @brief Formal Power Series (形式的冪級数)
*/
#include <algorithm>
#include <cassert>
#include <vector>
#line 2 "lib/convolution/ntt.hpp"
/**
* @brief Number Theoretic Transform
*/
#line 2 "lib/math/modint.hpp"
#include <iostream>
#line 5 "lib/math/modint.hpp"
/**
* @brief ModInt
* @docs docs/math/modint.md
*/
template <long long Modulus>
struct ModInt{
long long val;
static constexpr int mod() { return Modulus; }
constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
normalize();
}
void normalize(){
val = (val % Modulus + Modulus) % Modulus;
}
inline ModInt &operator+=(const ModInt &rhs) noexcept {
if(val += rhs.val, val >= Modulus) val -= Modulus;
return *this;
}
inline ModInt &operator-=(const ModInt &rhs) noexcept {
if(val -= rhs.val, val < 0) val += Modulus;
return *this;
}
inline ModInt &operator*=(const ModInt &rhs) noexcept {
val = val * rhs.val % Modulus;
return *this;
}
inline ModInt &operator/=(const ModInt &rhs) noexcept {
val = val * inv(rhs.val).val % Modulus;
return *this;
}
inline ModInt &operator++() noexcept {
if(++val >= Modulus) val -= Modulus;
return *this;
}
inline ModInt operator++(int) noexcept {
ModInt t = val;
if(++val >= Modulus) val -= Modulus;
return t;
}
inline ModInt &operator--() noexcept {
if(--val < 0) val += Modulus;
return *this;
}
inline ModInt operator--(int) noexcept {
ModInt t = val;
if(--val < 0) val += Modulus;
return t;
}
inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
inline ModInt inv(void) const { return inv(val); }
ModInt pow(long long n) const {
assert(0 <= n);
ModInt x = *this, r = 1;
while(n){
if(n & 1) r *= x;
x *= x;
n >>= 1;
}
return r;
}
ModInt inv(const long long n) const {
long long a = n, b = Modulus, u = 1, v = 0;
while(b){
long long t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
u %= Modulus;
if(u < 0) u += Modulus;
return u;
}
friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
is >> x.val;
x.normalize();
return is;
}
friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 2 "lib/math/crt.hpp"
/**
* @brief Chinese Remainder Theorem (中国剰余定理)
* @docs docs/math/crt.md
*/
#include <numeric>
#line 10 "lib/math/crt.hpp"
namespace CRT{
inline long long mod(long long a, long long m){
return (a % m + m) % m;
}
long long extGCD(long long a, long long b, long long &x, long long &y){
if(b == 0){
x = 1;
y = 0;
return a;
}
long long d = extGCD(b, a % b, y, x);
y -= a / b * x;
return d;
}
std::pair<long long, long long> chineseRem(const std::vector<long long> &b, const std::vector<long long> &m) {
long long r = 0, M = 1;
for(int i = 0; i < (int) b.size(); i++){
long long p, q;
long long d = extGCD(M, m[i], p, q);
if((b[i] - r) % d != 0) return {0, -1};
long long tmp = (b[i] - r) / d * p % (m[i] / d);
r += M * tmp;
M *= m[i] / d;
}
r %= M;
if(r < 0) r += M;
return {r, M};
}
// not coprime
long long preGarner(std::vector<long long> &b, std::vector<long long> &m, const long long MOD){
long long res = 1;
int n = b.size();
for(int i = 0; i < n; i++){
for(int j = 0; j < i; j++){
long long g = std::gcd(m[i], m[j]);
if((b[i] - b[j]) % g != 0) return -1;
m[i] /= g, m[j] /= g;
// gcd の分だけ被ってるので振り分ける
long long gi = std::gcd(m[i], g), gj = g / gi;
do{
g = std::gcd(gi, gj);
gi *= g, gj /= g;
}while(g != 1);
m[i] *= gi, m[j] *= gj;
b[i] %= m[i], b[j] %= m[j];
}
}
for(auto x : m) (res *= x) %= MOD;
return res;
}
long long garner(const std::vector<long long> &b, const std::vector<long long> &m, const long long MOD){
std::vector<long long> tm = m;
tm.push_back(MOD);
auto inv = [&](long long a, long long m) -> long long {
long long x, y;
extGCD(a, m, x, y);
return mod(x, m);
};
int n = b.size();
std::vector<long long> coeffs(n + 1, 1), constants(n + 1, 0);
for(int i = 0; i < n; i++){
// solve "coeffs[i] * t[i] + constants[i] = b[i] (mod. m[i])
long long t = mod((b[i] - constants[i]) * inv(coeffs[i], tm[i]), tm[i]);
for(int j = i + 1; j < n + 1; j++){
(constants[j] += t * coeffs[j]) %= tm[j];
(coeffs[j] *= tm[i]) %= tm[j];
}
}
return constants[n];
}
// ax + b ≡ 0 (mod m)
long long modEquation(long long a, long long b, long long m, bool is_positive = false){
a %= m; b %= m;
b = (m - b) % m;
long long g = std::gcd(a, m);
if(b % g != 0) return -1;
a /= g; b /= g; m /= g;
if(is_positive && b == 0){
return m;
}
long long x, y;
extGCD(a, m, x, y);
return (b * x % m + m) % m;
}
}
#line 9 "lib/convolution/ntt.hpp"
#line 11 "lib/convolution/ntt.hpp"
namespace NTT{
// @param n `0 <= n`
// @return minimum non-negative `x` s.t. `n <= 2**x`
int ceil_pow2(int n) {
int x = 0;
while((1U << x) < (unsigned int) (n)) x++;
return x;
}
// @param n `1 <= n`
// @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
int bsf(unsigned int n) {
return __builtin_ctz(n);
}
int primitive_root(int m) {
if(m == 2) return 1;
if(m == 167772161) return 3;
if(m == 469762049) return 3;
if(m == 754974721) return 11;
if(m == 998244353) return 3;
return 1;
}
template <typename T>
void butterfly(std::vector<T> &a){
int g = primitive_root(T::mod());
int n = int(a.size());
int h = ceil_pow2(n);
static bool first = true;
static T sum_e[30]; // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
if(first){
first = false;
T es[30], ies[30]; // es[i]^(2^(2+i)) == 1
int cnt2 = bsf(T::mod() - 1);
T e = T(g).pow((T::mod() - 1) >> cnt2), ie = e.inv();
for(int i = cnt2; i >= 2; i--){
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
T now = 1;
for(int i = 0; i <= cnt2 - 2; i++){
sum_e[i] = es[i] * now;
now *= ies[i];
}
}
for(int ph = 1; ph <= h; ph++){
int w = 1 << (ph - 1), p = 1 << (h - ph);
T now = 1;
for(int s = 0; s < w; s++){
int offset = s << (h - ph + 1);
for(int i = 0; i < p; i++){
auto l = a[i + offset];
auto r = a[i + offset + p] * now;
a[i + offset] = l + r;
a[i + offset + p] = l - r;
}
now *= sum_e[bsf(~(unsigned int) (s))];
}
}
}
template <typename T>
void butterfly_inv(std::vector<T> &a) {
int g = primitive_root(T::mod());
int n = int(a.size());
int h = ceil_pow2(n);
static bool first = true;
static T sum_ie[30]; // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
if(first){
first = false;
T es[30], ies[30]; // es[i]^(2^(2+i)) == 1
int cnt2 = bsf(T::mod() - 1);
T e = T(g).pow((T::mod() - 1) >> cnt2), ie = e.inv();
for(int i = cnt2; i >= 2; i--){
// e^(2^i) == 1
es[i - 2] = e;
ies[i - 2] = ie;
e *= e;
ie *= ie;
}
T now = 1;
for(int i = 0; i <= cnt2 - 2; i++){
sum_ie[i] = ies[i] * now;
now *= es[i];
}
}
for(int ph = h; ph >= 1; ph--){
int w = 1 << (ph - 1), p = 1 << (h - ph);
T inow = 1;
for(int s = 0; s < w; s++){
int offset = s << (h - ph + 1);
for(int i = 0; i < p; i++){
auto l = a[i + offset];
auto r = a[i + offset + p];
a[i + offset] = l + r;
a[i + offset + p] = (unsigned long long) (T::mod() + l.val - r.val) * inow.val;
}
inow *= sum_ie[bsf(~(unsigned int) (s))];
}
}
}
template <typename T>
std::vector<T> convolution(std::vector<T> a, std::vector<T> b){
int n = int(a.size()), m = int(b.size());
if(!n || !m) return {};
if(std::min(n, m) <= 60) {
if(n < m) {
std::swap(n, m);
std::swap(a, b);
}
std::vector<T> ans(n + m - 1);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
ans[i + j] += a[i] * b[j];
}
}
return ans;
}
int z = 1 << ceil_pow2(n + m - 1);
a.resize(z);
butterfly(a);
b.resize(z);
butterfly(b);
for(int i = 0; i < z; i++){
a[i] *= b[i];
}
butterfly_inv(a);
a.resize(n + m - 1);
T iz = T(z).inv();
for(int i = 0; i < n + m - 1; i++) a[i] *= iz;
return a;
}
template <typename T>
std::vector<T> convolution_mod(const std::vector<T> &a, const std::vector<T> &b, const long long MOD){
if(MOD == 998244353){
return convolution(a, b);
}
constexpr long long M0 = 167772161;
constexpr long long M1 = 469762049;
constexpr long long M2 = 754974721;
using mint0 = ModInt<M0>;
using mint1 = ModInt<M1>;
using mint2 = ModInt<M2>;
int n = a.size(), m = b.size();
std::vector<mint0> a0(n), b0(m);
std::vector<mint1> a1(n), b1(m);
std::vector<mint2> a2(n), b2(m);
for(int i = 0; i < n; i++){
a0[i] = a[i].val;
a1[i] = a[i].val;
a2[i] = a[i].val;
}
for(int i = 0; i < m; i++){
b0[i] = b[i].val;
b1[i] = b[i].val;
b2[i] = b[i].val;
}
auto c0 = convolution(a0, b0);
auto c1 = convolution(a1, b1);
auto c2 = convolution(a2, b2);
std::vector<T> ret(n + m - 1);
for(int i = 0; i < n + m - 1; i++){
ret[i] = CRT::garner({c0[i].val, c1[i].val, c2[i].val}, {M0, M1, M2}, MOD);
}
return ret;
}
};
#line 11 "lib/polynomial/formal_power_series.hpp"
template <typename T>
struct FormalPowerSeries : std::vector<T> {
using std::vector<T>::vector;
using FPS = FormalPowerSeries;
// deg 次として初期化
FPS pre(int deg) const {
FPS res(std::begin(*this), std::begin(*this) + std::min((int) this->size(), deg));
if((int) res.size() < deg) res.resize(deg, T(0));
return res;
}
// deg 次として反転
FPS rev(int deg = -1) const {
FPS res(*this);
if(deg != -1) res.resize(deg, T(0));
std::reverse(std::begin(res), std::end(res));
return res;
}
int notZeroCount() const {
int res = 0;
for(auto x : *this){
if(x != T(0)) res++;
}
return res;
}
int maxDeg() const {
for(int i = (int) this->size() - 1; i >= 0; i--){
if((*this)[i] != T(0)) return i;
}
return -1;
}
void shrink() {
while(this->size() && this->back() == T(0)) this->pop_back();
}
std::vector<std::pair<int, T>> sparseFormat() const {
std::vector<std::pair<int, T>> res;
for(int i = 0; i < (int) this->size(); i++){
if((*this)[i] != T(0)) res.emplace_back(i, (*this)[i]);
}
return res;
}
FPS operator+(const T &rhs) const { return FPS(*this) += rhs; }
FPS operator+(const FPS &rhs) const { return FPS(*this) += rhs; }
FPS operator-(const T &rhs) const { return FPS(*this) -= rhs; }
FPS operator-(const FPS &rhs) const { return FPS(*this) -= rhs; }
FPS operator*(const T &rhs) const { return FPS(*this) *= rhs; }
FPS operator*(const FPS &rhs) const { return FPS(*this) *= rhs; }
FPS operator/(const T &rhs) const { return FPS(*this) /= rhs; }
FPS operator/(const FPS &rhs) const { return FPS(*this) /= rhs; }
FPS operator%(const FPS &rhs) const { return FPS(*this) %= rhs; }
FPS operator-() const {
FPS res(this->size());
for(int i = 0; i < (int) this->size(); i++) res[i] = -(*this)[i];
return res;
}
FPS &operator+=(const T &rhs){
if(this->empty()) this->resize(1);
(*this)[0] += rhs;
return *this;
}
FPS &operator-=(const T &rhs){
if(this->empty()) this->resize(1);
(*this)[0] -= rhs;
return *this;
}
FPS &operator*=(const T &rhs){
for(auto &x : *this) x *= rhs;
return *this;
}
FPS &operator/=(const T &rhs){
for(auto &x : *this) x /= rhs;
return *this;
}
FPS &operator+=(const FPS &rhs) noexcept {
if(this->size() < rhs.size()) this->resize(rhs.size());
for(int i = 0; i < (int) rhs.size(); i++) (*this)[i] += rhs[i];
return *this;
}
FPS &operator-=(const FPS &rhs) noexcept {
if(this->size() < rhs.size()) this->resize(rhs.size());
for(int i = 0; i < (int) rhs.size(); i++) (*this)[i] -= rhs[i];
return *this;
}
FPS &operator*=(const FPS &rhs) noexcept {
long long len1 = this->notZeroCount(), len2 = rhs.notZeroCount();
// Sparse な場合
if(len1 * len2 <= 60LL * (long long) std::max(this->size(), rhs.size())){
std::vector<std::pair<int, T>> rhs_sparse = rhs.sparseFormat();
return *this = this->multiply_naive(rhs_sparse);
}
auto res = NTT::convolution_mod(*this, rhs, T::mod());
return *this = {std::begin(res), std::end(res)};
}
// f/g = f * (g.inv())
FPS &operator/=(const FPS &rhs) noexcept {
if(this->size() < rhs.size()) return *this = FPS();
const int n = this->size() - rhs.size() + 1;
return *this = (rev().pre(n) * rhs.rev().inv(n)).pre(n).rev(n);
}
FPS &operator%=(const FPS &rhs) noexcept {
return *this -= (*this / rhs) * rhs;
}
FPS operator>>(int deg) const {
if((int) this->size() <= deg) return {};
FPS res(*this);
res.erase(std::begin(res), std::begin(res) + deg);
return res;
}
FPS operator<<(int deg) const {
FPS res(*this);
res.insert(std::begin(res), deg, T(0));
return res;
}
// 微分
FPS diff() const {
const int n = this->size();
FPS res(std::max(0, n - 1));
for(int i = 1; i < n; i++) res[i - 1] = (*this)[i] * T(i);
return res;
}
// 積分
FPS integral() const {
const int n = this->size();
FPS res(n + 1);
res[0] = T(0);
for(int i = 0; i < n; i++) res[i + 1] = (*this)[i] / T(i + 1);
return res;
}
// {lhs / rhs, lhs % rhs}
std::pair<FPS, FPS> division(const FPS &rhs) const {
FPS q = *this / rhs;
FPS r = *this - q * rhs;
q.shrink(), r.shrink();
return {q, r};
}
FPS multiply_naive(const std::vector<std::pair<int, T>> &rhs, int deg = -1){
if(deg == -1){
if(rhs.empty()) deg = this->size();
else deg = this->size() + (rhs.back().first + 1) - 1;
}
FPS res(deg, T(0));
for(auto &[i, x] : this->sparseFormat()){
for(auto &[j, y] : rhs){
if(i + j >= deg) break;
res[i + j] += x * y;
}
}
return *this = {std::begin(res), std::end(res)};
}
FPS divide_naive(const std::vector<std::pair<int, T>> &rhs){
assert(!rhs.empty());
if((int) this->size() < (rhs.back().first + 1)) return FPS();
auto [i0, x0] = rhs[0];
assert(i0 == 0 && x0 != T(0));
T x0_inv = T(1) / x0;
for(int i = 0; i < (int) this->size(); i++){
for(int i2 = 1; i2 < (int) rhs.size(); i2++){
auto &[j, y] = rhs[i2];
if(i < j) break;
(*this)[i] -= (*this)[i - j] * y;
}
(*this)[i] *= x0_inv;
}
return *this;
}
// fg = 1 (mod x^n) となる g
FPS inv(int deg = -1) const {
assert((*this)[0] != T(0));
if(deg == -1) deg = this->size();
// g_p mod x^k から g mod x^2k を求める
// (g - g_p)^2 = g^2 - 2 g g_p + (g_p)^2 = 0 (mod x^2k)
// fg^2 - 2fg g_p + f (g_p)^2
// = g - 2(g_p) + f (g_p)^2 = 0 (mod x^2k)
// g = 2(g_p) - f (g_p)^2 (mod x^2k)
FPS res({T(1) / (*this)[0]});
for(int i = 1; i < deg; i <<= 1) {
res = (res + res - res * res * pre(i << 1)).pre(i << 1);
}
return res.pre(deg);
}
// g = log f となる g
FPS log(int deg = -1) const {
assert((*this)[0] == T(1));
if(deg == -1) deg = this->size();
// log f = integral((f' / f) dx)
return (this->diff() * this->inv(deg)).pre(deg - 1).integral().pre(deg);
}
// g = exp(f) となる g
FPS exp(int deg = -1) const {
assert((*this)[0] == T(0));
if(deg == -1) deg = this->size();
// g_p mod x^k から g mod x^2k をニュートン法で求める
// log g = f (mod x^n) であるから、
// g = g_p - (log g_p - f)/(log' g_p)
// = g_p(1 - log g_p + f) (mod x^2k)
FPS res({T(1)});
for(int i = 1; i < deg; i <<= 1) {
res = (res * (-res.log(i << 1) + pre(i << 1) + T(1))).pre(i << 1);
}
return res.pre(deg);
}
// g = f^k となる g
FPS pow(long long k, int deg = -1) const {
if(deg == -1) deg = this->size();
if(k == 0){
FPS res(deg, T(0));
res[0] = T(1);
return res;
}
// f^k = exp(log f)^k = exp(k log f)
// log を計算するのに定数項が 1 である必要があるので調整する
// 最も低次の項を a x^i として、(f / (a x^i))^k を計算してから (a x^i)^k を掛ける
for(int i = 0; i < (int) this->size(); i++){
if(k * i > deg) return FPS(deg, T(0));
if((*this)[i] != T(0)){
T inv_i = T(1) / (*this)[i];
FPS res = ((((*this) * inv_i) >> i).log(deg) * k).exp(deg) * ((*this)[i].pow(k));
res = (res << (k * i)).pre(deg);
return res;
}
}
return *this;
}
long long sqrtT(const T a) const {
const long long p = T::mod();
if(a == T(0) || a == T(1)) return a.val;
if(a.pow((p - 1) / 2) != T(1)) return -1LL;
T b = 1;
while(b.pow((p - 1) / 2) == 1) b++;
// p - 1 = m 2^e
long long m = p - 1;
int e = 0;
while(m % 2 == 0) m >>= 1, e++;
// x = a^((m + 1) / 2) (mod p)
T x = a.pow((m - 1) / 2);
// y = a^{-1} x^2 (mod p)
T y = (a * x) * x;
x *= a;
T z = b.pow(m);
while(y != 1){
int j = 0;
T t = y;
while(t != 1){
t *= t;
j++;
}
z = z.pow(1LL << (e - j - 1));
x *= z;
z *= z;
y *= z;
e = j;
}
return x.val;
}
// g^2 = f となる g
FPS sqrt(int deg = -1) const {
if(this->empty()) return {};
if(deg == -1) deg = this->size();
// inv を計算するのに定数項が非零である必要があるので調整する
if((*this)[0] == T(0)){
for(int i = 1; i < (int) this->size(); i++){
if((*this)[i] == T(0)) continue;
if(i & 1) return {};
FPS res = (*this >> i).sqrt();
if(res.empty()) return {};
res = res.pre(deg - i / 2) << (i / 2);
return res;
}
FPS res(deg, T(0));
return res;
}
// g_p mod x^k から g mod x^2k をニュートン法で求める
// g^2 = f (mod x^n) であるから、
// g = g_p - ((g_p)^2 - f)/((g_p^2)')
// = g_p - ((g_p)^2 - f)/(2 g_p)
// = 1/2 * (g_p + f/g_p (mod x^2k)
long long sqrt0 = sqrtT((*this)[0]);
if(sqrt0 == -1) return {};
FPS res({T(sqrt0)});
T inv2 = T(1) / T(2);
for(int i = 1; i < deg; i <<= 1) {
res = (res + pre(i << 1) * res.inv(i << 1)) * inv2;
}
return res.pre(deg);
}
};
#line 2 "lib/polynomial/multipoint_evaluation.hpp"
/**
* @brief Multipoint Evaluation (多点評価)
* @see https://37zigen.com/multipoint-evaluation/
*/
#line 10 "lib/polynomial/multipoint_evaluation.hpp"
template <typename T>
std::vector<T> multipointEvalutation(const FormalPowerSeries<T> &f, const std::vector<T> &p){
using FPS = FormalPowerSeries<T>;
int m = 1;
while(m < (int) p.size()) m *= 2;
std::vector<FPS> product_tree(m * 2, FPS({T(1)}));
for(int i = 0; i < (int) p.size(); i++){
// f(a) = f(x) (mod (x - a))
product_tree[m + i] = {-p[i], T(1)};
}
for(int i = m - 1; i >= 1; i--){
product_tree[i] = product_tree[i * 2] * product_tree[i * 2 + 1];
}
std::vector<FPS> reminder_tree(m * 2, FPS({T(0)}));
reminder_tree[1] = f % product_tree[1];
for(int i = 2; i < m * 2; i++){
// (f mod (g_0 g_1)) mod g_0 = f mod g_0
reminder_tree[i] = reminder_tree[i / 2] % product_tree[i];
reminder_tree[i].shrink();
}
std::vector<T> res(p.size());
for(int i = 0; i < (int) p.size(); i++){
res[i] = reminder_tree[m + i][0];
}
return res;
}
#line 11 "lib/polynomial/polynomial_interpolation.hpp"
template <typename T>
FormalPowerSeries<T> polynomialInterpolation(const std::vector<T> &x, const std::vector<T> &y){
using FPS = FormalPowerSeries<T>;
assert(x.size() == y.size());
int n = 1;
while(n < (int) x.size()) n *= 2;
std::vector<FPS> product_tree(n * 2, FPS({T(1)}));
for(int i = 0; i < (int) x.size(); i++){
product_tree[n + i] = {-x[i], T(1)};
}
for(int i = n - 1; i >= 1; i--){
product_tree[i] = product_tree[i * 2] * product_tree[i * 2 + 1];
}
// g = procuct_tree[1]
FPS g_d = product_tree[1].diff();
std::vector<T> p = multipointEvalutation(g_d, x);
std::vector<FPS> f(n * 2, FPS({T(0)}));
for(int i = 0; i < (int) x.size(); i++){
f[n + i] = FPS({y[i] / p[i]});
}
for(int i = n - 1; i >= 1; i--){
// 通分
f[i] = (f[i * 2] * product_tree[i * 2 + 1]) + (f[i * 2 + 1] * product_tree[i * 2]);
}
// g * f / g = f
return f[1].pre(n);
}