Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ebi-fly13/Library

:heavy_check_mark: $f(x)$ の逆関数
(fps/compositional_inverse_of_fps.hpp)

説明

形式的べき級数 $f$ について、その逆関数を求める。ニュートン法を用いると、形式的べき級数の合成がボトルネックとなり $O(N^2)$ となる。

\[g_{2n} = g_{n} - \frac{f(g_{n}) - x}{f^{\prime}(g_{n})} \mod x^{2n}\]

Depends on

Verified with

Code

#pragma once

#include <cassert>

#include "../fps/composition_of_fps.hpp"
#include "../fps/fps.hpp"
#include "../modint/base.hpp"

namespace ebi {

template <Modint mint,
          std::vector<mint> (*convolution)(const std::vector<mint> &,
                                           const std::vector<mint> &)>
FormalPowerSeries<mint, convolution> compositional_inverse_of_fps(
    FormalPowerSeries<mint, convolution> f, int d = -1) {
    using FPS = FormalPowerSeries<mint, convolution>;
    if (d < 0) d = f.deg();
    assert((int)f.size() >= 2 && f[0] == 0 && f[1] != 0);
    FPS df = f.differential();
    FPS g = {0, f[1].inv()};
    for (int n = 2; n < d; n <<= 1) {
        g.resize(2 * n);
        if (f.deg() < 2 * n) f.resize(2 * n);
        if (df.deg() < 2 * n) df.resize(2 * n);
        FPS fg = composition_of_fps(f.pre(2 * n), g);
        FPS fdg = composition_of_fps(df.pre(2 * n), g);
        g -= ((fg - FPS{0, 1}) * fdg.inv(2 * n)).pre(2 * n);
    }
    g.resize(d);
    return g;
}

}  // namespace ebi
#line 2 "fps/compositional_inverse_of_fps.hpp"

#include <cassert>

#line 2 "fps/composition_of_fps.hpp"

#line 4 "fps/composition_of_fps.hpp"
#include <vector>

#line 2 "fps/fps.hpp"

#include <algorithm>
#line 5 "fps/fps.hpp"
#include <optional>
#line 7 "fps/fps.hpp"

#line 2 "modint/base.hpp"

#include <concepts>
#include <iostream>
#include <utility>

namespace ebi {

template <class T>
concept Modint = requires(T a, T b) {
    a + b;
    a - b;
    a * b;
    a / b;
    a.inv();
    a.val();
    a.pow(std::declval<long long>());
    T::mod();
};

template <Modint mint> std::istream &operator>>(std::istream &os, mint &a) {
    long long x;
    os >> x;
    a = x;
    return os;
}

template <Modint mint>
std::ostream &operator<<(std::ostream &os, const mint &a) {
    return os << a.val();
}

}  // namespace ebi
#line 9 "fps/fps.hpp"

namespace ebi {

template <Modint mint,
          std::vector<mint> (*convolution)(const std::vector<mint> &,
                                           const std::vector<mint> &)>
struct FormalPowerSeries : std::vector<mint> {
  private:
    using std::vector<mint>::vector;
    using std::vector<mint>::vector::operator=;
    using FPS = FormalPowerSeries;

  public:
    FormalPowerSeries(const std::vector<mint> &a) {
        *this = a;
    }

    FPS operator+(const FPS &rhs) const noexcept {
        return FPS(*this) += rhs;
    }
    FPS operator-(const FPS &rhs) const noexcept {
        return FPS(*this) -= rhs;
    }
    FPS operator*(const FPS &rhs) const noexcept {
        return FPS(*this) *= rhs;
    }
    FPS operator/(const FPS &rhs) const noexcept {
        return FPS(*this) /= rhs;
    }
    FPS operator%(const FPS &rhs) const noexcept {
        return FPS(*this) %= rhs;
    }

    FPS operator+(const mint &rhs) const noexcept {
        return FPS(*this) += rhs;
    }
    FPS operator-(const mint &rhs) const noexcept {
        return FPS(*this) -= rhs;
    }
    FPS operator*(const mint &rhs) const noexcept {
        return FPS(*this) *= rhs;
    }
    FPS operator/(const mint &rhs) const noexcept {
        return FPS(*this) /= rhs;
    }

    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 {
        *this = convolution(*this, rhs);
        return *this;
    }

    FPS &operator/=(const FPS &rhs) noexcept {
        int n = deg() - 1;
        int m = rhs.deg() - 1;
        if (n < m) {
            *this = {};
            return *this;
        }
        *this = (*this).rev() * rhs.rev().inv(n - m + 1);
        (*this).resize(n - m + 1);
        std::reverse((*this).begin(), (*this).end());
        return *this;
    }

    FPS &operator%=(const FPS &rhs) noexcept {
        *this -= *this / rhs * rhs;
        shrink();
        return *this;
    }

    FPS &operator+=(const mint &rhs) noexcept {
        if (this->empty()) this->resize(1);
        (*this)[0] += rhs;
        return *this;
    }

    FPS &operator-=(const mint &rhs) noexcept {
        if (this->empty()) this->resize(1);
        (*this)[0] -= rhs;
        return *this;
    }

    FPS &operator*=(const mint &rhs) noexcept {
        for (int i = 0; i < deg(); ++i) {
            (*this)[i] *= rhs;
        }
        return *this;
    }
    FPS &operator/=(const mint &rhs) noexcept {
        mint inv_rhs = rhs.inv();
        for (int i = 0; i < deg(); ++i) {
            (*this)[i] *= inv_rhs;
        }
        return *this;
    }

    FPS operator>>(int d) const {
        if (deg() <= d) return {};
        FPS f = *this;
        f.erase(f.begin(), f.begin() + d);
        return f;
    }

    FPS operator<<(int d) const {
        FPS f = *this;
        f.insert(f.begin(), d, 0);
        return f;
    }

    FPS operator-() const {
        FPS g(this->size());
        for (int i = 0; i < (int)this->size(); i++) g[i] = -(*this)[i];
        return g;
    }

    FPS pre(int sz) const {
        return FPS(this->begin(), this->begin() + std::min(deg(), sz));
    }

    FPS rev() const {
        auto f = *this;
        std::reverse(f.begin(), f.end());
        return f;
    }

    FPS differential() const {
        int n = deg();
        FPS g(std::max(0, n - 1));
        for (int i = 0; i < n - 1; i++) {
            g[i] = (*this)[i + 1] * (i + 1);
        }
        return g;
    }

    FPS integral() const {
        int n = deg();
        FPS g(n + 1);
        g[0] = 0;
        if (n > 0) g[1] = 1;
        auto mod = mint::mod();
        for (int i = 2; i <= n; i++) g[i] = (-g[mod % i]) * (mod / i);
        for (int i = 0; i < n; i++) g[i + 1] *= (*this)[i];
        return g;
    }

    FPS inv(int d = -1) const {
        int n = 1;
        if (d < 0) d = deg();
        FPS g(n);
        g[0] = (*this)[0].inv();
        while (n < d) {
            n <<= 1;
            g = (g * 2 - g * g * this->pre(n)).pre(n);
        }
        g.resize(d);
        return g;
    }

    FPS log(int d = -1) const {
        assert((*this)[0].val() == 1);
        if (d < 0) d = deg();
        return ((*this).differential() * (*this).inv(d)).pre(d - 1).integral();
    }

    FPS exp(int d = -1) const {
        assert((*this)[0].val() == 0);
        int n = 1;
        if (d < 0) d = deg();
        FPS g(n);
        g[0] = 1;
        while (n < d) {
            n <<= 1;
            g = (g * (this->pre(n) - g.log(n) + 1)).pre(n);
        }
        g.resize(d);
        return g;
    }

    FPS pow(int64_t k, int d = -1) const {
        const int n = deg();
        if (d < 0) d = n;
        if (k == 0) {
            FPS f(d);
            if (d > 0) f[0] = 1;
            return f;
        }
        for (int i = 0; i < n; i++) {
            if ((*this)[i] != 0) {
                mint rev = (*this)[i].inv();
                FPS f = (((*this * rev) >> i).log(d) * k).exp(d);
                f *= (*this)[i].pow(k);
                f = (f << (i * k)).pre(d);
                if (f.deg() < d) f.resize(d);
                return f;
            }
            if (i + 1 >= (d + k - 1) / k) break;
        }
        return FPS(d);
    }

    int deg() const {
        return (*this).size();
    }

    void shrink() {
        while ((!this->empty()) && this->back() == 0) this->pop_back();
    }

    int count_terms() const {
        int c = 0;
        for (int i = 0; i < deg(); i++) {
            if ((*this)[i] != 0) c++;
        }
        return c;
    }

    std::optional<FPS> sqrt(int d = -1) const;

    static FPS exp_x(int n) {
        FPS f(n);
        mint fact = 1;
        for (int i = 1; i < n; i++) fact *= i;
        f[n - 1] = fact.inv();
        for (int i = n - 1; i >= 0; i--) f[i - 1] = f[i] * i;
        return f;
    }
};

}  // namespace ebi
#line 8 "fps/composition_of_fps.hpp"

namespace ebi {

template <Modint mint,
          std::vector<mint> (*convolution)(const std::vector<mint> &,
                                           const std::vector<mint> &)>
FormalPowerSeries<mint, convolution> composition_of_fps(
    const FormalPowerSeries<mint, convolution> &f,
    const FormalPowerSeries<mint, convolution> &g) {
    using FPS = FormalPowerSeries<mint, convolution>;
    // assert(f.deg() == g.deg());
    int n = f.deg();
    int k = 1;
    while (k * k < n) k++;
    std::vector<FPS> baby(k + 1);
    baby[0] = FPS{1};
    baby[1] = g;
    for (int i = 2; i < k + 1; i++) {
        baby[i] = (baby[i - 1] * g).pre(n);
    }
    std::vector<FPS> giant(k + 1);
    giant[0] = FPS{1};
    giant[1] = baby[k];
    for (int i = 2; i < k + 1; i++) {
        giant[i] = (giant[i - 1] * giant[1]).pre(n);
    }
    FPS h(n);
    for (int i = 0; i < k + 1; i++) {
        FPS a(n);
        for (int j = 0; j < k; j++) {
            if (k * i + j < n) {
                mint coef = f[k * i + j];
                a += baby[j] * coef;
            } else
                break;
        }
        h += (giant[i] * a).pre(n);
    }
    return h;
}

}  // namespace ebi
#line 8 "fps/compositional_inverse_of_fps.hpp"

namespace ebi {

template <Modint mint,
          std::vector<mint> (*convolution)(const std::vector<mint> &,
                                           const std::vector<mint> &)>
FormalPowerSeries<mint, convolution> compositional_inverse_of_fps(
    FormalPowerSeries<mint, convolution> f, int d = -1) {
    using FPS = FormalPowerSeries<mint, convolution>;
    if (d < 0) d = f.deg();
    assert((int)f.size() >= 2 && f[0] == 0 && f[1] != 0);
    FPS df = f.differential();
    FPS g = {0, f[1].inv()};
    for (int n = 2; n < d; n <<= 1) {
        g.resize(2 * n);
        if (f.deg() < 2 * n) f.resize(2 * n);
        if (df.deg() < 2 * n) df.resize(2 * n);
        FPS fg = composition_of_fps(f.pre(2 * n), g);
        FPS fdg = composition_of_fps(df.pre(2 * n), g);
        g -= ((fg - FPS{0, 1}) * fdg.inv(2 * n)).pre(2 * n);
    }
    g.resize(d);
    return g;
}

}  // namespace ebi
Back to top page