hly1204's library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hly1204/library

:heavy_check_mark: relaxed_conv.hpp

Depends on

Verified with

Code

#pragma once

#include "fft.hpp"
#include <algorithm>
#include <cassert>
#include <vector>

template<typename Tp> class RelaxedConvolution {
    std::vector<Tp> L_, R_, P_ = {Tp(0), Tp(0), Tp(0), Tp(0)};
    std::vector<std::vector<Tp>> dftL_, dftR_;
    int N_ = 0;

public:
    const std::vector<Tp> &lhs() const { return L_; }
    const std::vector<Tp> &rhs() const { return R_; }
    const std::vector<Tp> &prod() const { return P_; }
    int count() const { return N_; }
    // returns [x^n] L*R
    // call operator[](n) when count=n to get [x^n] L*R - L(0) * [x^n] R - [x^n] L * R(0)
    Tp operator[](int n) const { return P_[n]; }
    Tp peek() const { return P_[N_]; }

    // set [x^N] L = l and [x^N] R = r
    // returns [x^N] L*R
    Tp push(const Tp &l, const Tp &r) {
        L_.emplace_back(l);
        R_.emplace_back(r);
        if (__builtin_expect(N_ > 1, 1)) {
            P_.emplace_back();
            P_.emplace_back();
            P_[N_] += l * R_[0] + L_[0] * r;
            P_[N_ + 1] += L_[1] * R_[N_] + L_[N_] * R_[1];
            for (int sft = 1, os = fft_len(N_ + 1) / 2, t = N_ + 1 - os;
                 (t & 1) == 0 && 1 << sft < os; ++sft, t /= 2) {
                std::vector<Tp> P0(2 << sft), P1(2 << sft);
                std::copy_n(L_.begin() + (N_ + 1 - (1 << sft)), 1 << sft, P0.begin());
                std::copy_n(R_.begin() + (N_ + 1 - (1 << sft)), 1 << sft, P1.begin());
                fft(P0);
                fft(P1);
                for (int i = 0; i < 2 << sft; ++i)
                    P0[i] = P0[i] * dftR_[sft - 1][i] + P1[i] * dftL_[sft - 1][i];
                inv_fft(P0);
                for (int i = 0; i < (2 << sft) - 1; ++i) P_[N_ + 1 + i] += P0[i];
            }
            if (((N_ + 1) & N_) == 0) {
                auto &&LL = dftL_.emplace_back(N_ + 1);
                auto &&RR = dftR_.emplace_back(N_ + 1);
                std::copy_n(L_.begin() + (N_ + 1) / 2, (N_ + 1) / 2, LL.begin());
                std::copy_n(R_.begin() + (N_ + 1) / 2, (N_ + 1) / 2, RR.begin());
                fft(LL);
                fft(RR);
                auto PP = LL;
                for (int i = 0; i < N_ + 1; ++i) PP[i] *= RR[i];
                inv_fft(PP);
                for (int i = 0; i < N_; ++i) P_[N_ + 1 + i] += PP[i];
            }
        } else {
            if (N_ == 0) P_[0] = l * r;
            if (N_ == 1) P_[1] = l * R_[0] + L_[0] * r, P_[2] = L_[1] * R_[1];
        }
        return P_[N_++];
    }
};

template<typename Tp> class RelaxedFPSInv {
    // f * 1/f = 1
    // [x^N] f * 1/f = R.peek() + f(0) * [x^N] (1/f) + ([x^N] f) * f(0)^-1
    //               = delta(N=0)
public:
    RelaxedConvolution<Tp> R;
    Tp Inv0;

    Tp push(const Tp &c) {
        if (__builtin_expect(R.count() > 0, 1)) {
            const Tp res = -(R.peek() + c * Inv0) * Inv0;
            R.push(c, res);
            return res;
        }
        assert(c != 0);
        R.push(c, Inv0 = c.inv());
        return Inv0;
    }
};

template<typename Tp> class RelaxedFPSDiv {
    // f/g * g = f
    // [x^N] f/g * g = R.peek() + (f/g)(0) * [x^N] g + ([x^N] f/g) * g(0)
    //               = [x^N] f
public:
    RelaxedConvolution<Tp> R;
    Tp Inv0;

    Tp push(const Tp &p, const Tp &q) {
        if (__builtin_expect(R.count() > 0, 1)) {
            const Tp res = (p - R.peek() - R.lhs().front() * q) * Inv0;
            R.push(res, q);
            return res;
        }
        assert(q != 0);
        const Tp res = p * (Inv0 = q.inv());
        R.push(res, q);
        return res;
    }
};

template<typename Tp> class RelaxedFPSLog {
public:
    RelaxedFPSDiv<Tp> R;
    Tp LastCoeff = Tp(1);
    bool First   = true;

    Tp push(const Tp &c) {
        if (__builtin_expect(!First, 1)) {
            const int ind = R.R.count() + 1;
            const Tp res  = R.push(c * ind, LastCoeff) / ind;
            LastCoeff     = c;
            return res;
        }
        assert(c == 1);
        First = false;
        return Tp(0);
    }
};

template<typename Tp> class RelaxedFPSExp {
    // D(exp(f)) = exp(f) * D(f)
public:
    RelaxedConvolution<Tp> R;
    Tp LastCoeff = Tp(1);
    bool First   = true;

    Tp push(const Tp &c) {
        if (__builtin_expect(!First, 1)) {
            const int ind = R.count() + 1;
            const Tp res  = R.push(LastCoeff, c * ind) / ind;
            LastCoeff     = res;
            return res;
        }
        assert(c == 0);
        First = false;
        return Tp(1);
    }
};
#line 2 "relaxed_conv.hpp"

#line 2 "fft.hpp"

#include <algorithm>
#include <cassert>
#include <iterator>
#include <memory>
#include <vector>

template<typename Tp> class FftInfo {
    static Tp least_quadratic_nonresidue() {
        for (int i = 2;; ++i)
            if (Tp(i).pow((Tp::mod() - 1) / 2) == -1) return Tp(i);
    }

    const int ordlog2_;
    const Tp zeta_;
    const Tp invzeta_;
    const Tp imag_;
    const Tp invimag_;

    mutable std::vector<Tp> root_;
    mutable std::vector<Tp> invroot_;

    FftInfo()
        : ordlog2_(__builtin_ctzll(Tp::mod() - 1)),
          zeta_(least_quadratic_nonresidue().pow((Tp::mod() - 1) >> ordlog2_)),
          invzeta_(zeta_.inv()), imag_(zeta_.pow(1LL << (ordlog2_ - 2))), invimag_(-imag_),
          root_{Tp(1), imag_}, invroot_{Tp(1), invimag_} {}

public:
    static const FftInfo &get() {
        static FftInfo info;
        return info;
    }

    Tp imag() const { return imag_; }
    Tp inv_imag() const { return invimag_; }
    Tp zeta() const { return zeta_; }
    Tp inv_zeta() const { return invzeta_; }
    const std::vector<Tp> &root(int n) const {
        // [0, n)
        assert((n & (n - 1)) == 0);
        if (const int s = root_.size(); s < n) {
            root_.resize(n);
            for (int i = __builtin_ctz(s); (1 << i) < n; ++i) {
                const int j = 1 << i;
                root_[j]    = zeta_.pow(1LL << (ordlog2_ - i - 2));
                for (int k = j + 1; k < j * 2; ++k) root_[k] = root_[k - j] * root_[j];
            }
        }
        return root_;
    }
    const std::vector<Tp> &inv_root(int n) const {
        // [0, n)
        assert((n & (n - 1)) == 0);
        if (const int s = invroot_.size(); s < n) {
            invroot_.resize(n);
            for (int i = __builtin_ctz(s); (1 << i) < n; ++i) {
                const int j = 1 << i;
                invroot_[j] = invzeta_.pow(1LL << (ordlog2_ - i - 2));
                for (int k = j + 1; k < j * 2; ++k) invroot_[k] = invroot_[k - j] * invroot_[j];
            }
        }
        return invroot_;
    }
};

inline int fft_len(int n) {
    --n;
    n |= n >> 1, n |= n >> 2, n |= n >> 4, n |= n >> 8;
    return (n | n >> 16) + 1;
}

namespace detail {

template<typename Iterator> inline void
butterfly_n(Iterator a, int n,
            const std::vector<typename std::iterator_traits<Iterator>::value_type> &root) {
    assert(n > 0);
    assert((n & (n - 1)) == 0);
    const int bn = __builtin_ctz(n);
    if (bn & 1) {
        for (int i = 0; i < n / 2; ++i) {
            const auto a0 = a[i], a1 = a[i + n / 2];
            a[i] = a0 + a1, a[i + n / 2] = a0 - a1;
        }
    }
    for (int i = n >> (bn & 1); i >= 4; i /= 4) {
        const int i4 = i / 4;
        for (int k = 0; k < i4; ++k) {
            const auto a0 = a[k + i4 * 0], a1 = a[k + i4 * 1];
            const auto a2 = a[k + i4 * 2], a3 = a[k + i4 * 3];
            const auto a02p = a0 + a2, a02m = a0 - a2;
            const auto a13p = a1 + a3, a13m = (a1 - a3) * root[1];
            a[k + i4 * 0] = a02p + a13p, a[k + i4 * 1] = a02p - a13p;
            a[k + i4 * 2] = a02m + a13m, a[k + i4 * 3] = a02m - a13m;
        }
        for (int j = i, m = 2; j < n; j += i, m += 2) {
            const auto r = root[m], r2 = r * r, r3 = r2 * r;
            for (int k = j; k < j + i4; ++k) {
                const auto a0 = a[k + i4 * 0], a1 = a[k + i4 * 1] * r;
                const auto a2 = a[k + i4 * 2] * r2, a3 = a[k + i4 * 3] * r3;
                const auto a02p = a0 + a2, a02m = a0 - a2;
                const auto a13p = a1 + a3, a13m = (a1 - a3) * root[1];
                a[k + i4 * 0] = a02p + a13p, a[k + i4 * 1] = a02p - a13p;
                a[k + i4 * 2] = a02m + a13m, a[k + i4 * 3] = a02m - a13m;
            }
        }
    }
}

template<typename Iterator> inline void
inv_butterfly_n(Iterator a, int n,
                const std::vector<typename std::iterator_traits<Iterator>::value_type> &root) {
    assert(n > 0);
    assert((n & (n - 1)) == 0);
    const int bn = __builtin_ctz(n);
    for (int i = 4; i <= (n >> (bn & 1)); i *= 4) {
        const int i4 = i / 4;
        for (int k = 0; k < i4; ++k) {
            const auto a0 = a[k + i4 * 0], a1 = a[k + i4 * 1];
            const auto a2 = a[k + i4 * 2], a3 = a[k + i4 * 3];
            const auto a01p = a0 + a1, a01m = a0 - a1;
            const auto a23p = a2 + a3, a23m = (a2 - a3) * root[1];
            a[k + i4 * 0] = a01p + a23p, a[k + i4 * 1] = a01m + a23m;
            a[k + i4 * 2] = a01p - a23p, a[k + i4 * 3] = a01m - a23m;
        }
        for (int j = i, m = 2; j < n; j += i, m += 2) {
            const auto r = root[m], r2 = r * r, r3 = r2 * r;
            for (int k = j; k < j + i4; ++k) {
                const auto a0 = a[k + i4 * 0], a1 = a[k + i4 * 1];
                const auto a2 = a[k + i4 * 2], a3 = a[k + i4 * 3];
                const auto a01p = a0 + a1, a01m = a0 - a1;
                const auto a23p = a2 + a3, a23m = (a2 - a3) * root[1];
                a[k + i4 * 0] = a01p + a23p, a[k + i4 * 1] = (a01m + a23m) * r;
                a[k + i4 * 2] = (a01p - a23p) * r2, a[k + i4 * 3] = (a01m - a23m) * r3;
            }
        }
    }
    if (bn & 1) {
        for (int i = 0; i < n / 2; ++i) {
            const auto a0 = a[i], a1 = a[i + n / 2];
            a[i] = a0 + a1, a[i + n / 2] = a0 - a1;
        }
    }
}

} // namespace detail

// FFT_n: A(x) |-> bit-reversed order of [A(1), A(zeta_n), ..., A(zeta_n^(n-1))]
template<typename Iterator> inline void fft_n(Iterator a, int n) {
    using Tp = typename std::iterator_traits<Iterator>::value_type;
    detail::butterfly_n(a, n, FftInfo<Tp>::get().root(n / 2));
}

template<typename Tp> inline void fft(std::vector<Tp> &a) { fft_n(a.begin(), a.size()); }

// IFFT_n: bit-reversed order of [A(1), A(zeta_n), ..., A(zeta_n^(n-1))] |-> A(x)
template<typename Iterator> inline void inv_fft_n(Iterator a, int n) {
    using Tp = typename std::iterator_traits<Iterator>::value_type;
    detail::inv_butterfly_n(a, n, FftInfo<Tp>::get().inv_root(n / 2));
    const Tp iv = Tp::mod() - (Tp::mod() - 1) / n;
    for (int i = 0; i < n; ++i) a[i] *= iv;
}

template<typename Tp> inline void inv_fft(std::vector<Tp> &a) { inv_fft_n(a.begin(), a.size()); }

// IFFT_n^T: A(x) |-> 1/n FFT_n((x^n A(x^(-1))) mod (x^n - 1))
template<typename Iterator> inline void transposed_inv_fft_n(Iterator a, int n) {
    using Tp    = typename std::iterator_traits<Iterator>::value_type;
    const Tp iv = Tp::mod() - (Tp::mod() - 1) / n;
    for (int i = 0; i < n; ++i) a[i] *= iv;
    detail::butterfly_n(a, n, FftInfo<Tp>::get().inv_root(n / 2));
}

template<typename Tp> inline void transposed_inv_fft(std::vector<Tp> &a) {
    transposed_inv_fft_n(a.begin(), a.size());
}

// FFT_n^T : FFT_n((x^n A(x^(-1))) mod (x^n - 1)) |-> n A(x)
template<typename Iterator> inline void transposed_fft_n(Iterator a, int n) {
    using Tp = typename std::iterator_traits<Iterator>::value_type;
    detail::inv_butterfly_n(a, n, FftInfo<Tp>::get().root(n / 2));
}

template<typename Tp> inline void transposed_fft(std::vector<Tp> &a) {
    transposed_fft_n(a.begin(), a.size());
}

template<typename Tp> inline std::vector<Tp> convolution_fft(std::vector<Tp> a, std::vector<Tp> b) {
    if (a.empty() || b.empty()) return {};
    const int n   = a.size();
    const int m   = b.size();
    const int len = fft_len(n + m - 1);
    a.resize(len);
    b.resize(len);
    fft(a);
    fft(b);
    for (int i = 0; i < len; ++i) a[i] *= b[i];
    inv_fft(a);
    a.resize(n + m - 1);
    return a;
}

template<typename Tp> inline std::vector<Tp> square_fft(std::vector<Tp> a) {
    if (a.empty()) return {};
    const int n   = a.size();
    const int len = fft_len(n * 2 - 1);
    a.resize(len);
    fft(a);
    for (int i = 0; i < len; ++i) a[i] *= a[i];
    inv_fft(a);
    a.resize(n * 2 - 1);
    return a;
}

template<typename Tp>
inline std::vector<Tp> convolution_naive(const std::vector<Tp> &a, const std::vector<Tp> &b) {
    if (a.empty() || b.empty()) return {};
    const int n = a.size();
    const int m = b.size();
    std::vector<Tp> res(n + m - 1);
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < m; ++j) res[i + j] += a[i] * b[j];
    return res;
}

template<typename Tp>
inline std::vector<Tp> convolution(const std::vector<Tp> &a, const std::vector<Tp> &b) {
    if (std::min(a.size(), b.size()) < 60) return convolution_naive(a, b);
    if (std::addressof(a) == std::addressof(b)) return square_fft(a);
    return convolution_fft(a, b);
}

template<typename Tp> inline std::vector<Tp>
convolution_naive_trunc(const std::vector<Tp> &a, const std::vector<Tp> &b, int k) {
    if (a.empty() || b.empty()) return std::vector<Tp>(k);
    const int n = a.size();
    const int m = b.size();
    std::vector<Tp> res(k);
    for (int i = 0; i < std::min(n, k); ++i)
        for (int j = 0; j < std::min(m, k) && i + j < k; ++j) res[i + j] += a[i] * b[j];
    return res;
}

template<typename Tp> inline std::vector<Tp> convolution_trunc(const std::vector<Tp> &a,
                                                               const std::vector<Tp> &b, int k) {
    if (k < 60) return convolution_naive_trunc(a, b, k);
    auto ab = convolution(std::vector(a.begin(), std::min(a.end(), a.begin() + k)),
                          std::vector(b.begin(), std::min(b.end(), b.begin() + k)));
    ab.resize(k);
    return ab;
}
#line 7 "relaxed_conv.hpp"

template<typename Tp> class RelaxedConvolution {
    std::vector<Tp> L_, R_, P_ = {Tp(0), Tp(0), Tp(0), Tp(0)};
    std::vector<std::vector<Tp>> dftL_, dftR_;
    int N_ = 0;

public:
    const std::vector<Tp> &lhs() const { return L_; }
    const std::vector<Tp> &rhs() const { return R_; }
    const std::vector<Tp> &prod() const { return P_; }
    int count() const { return N_; }
    // returns [x^n] L*R
    // call operator[](n) when count=n to get [x^n] L*R - L(0) * [x^n] R - [x^n] L * R(0)
    Tp operator[](int n) const { return P_[n]; }
    Tp peek() const { return P_[N_]; }

    // set [x^N] L = l and [x^N] R = r
    // returns [x^N] L*R
    Tp push(const Tp &l, const Tp &r) {
        L_.emplace_back(l);
        R_.emplace_back(r);
        if (__builtin_expect(N_ > 1, 1)) {
            P_.emplace_back();
            P_.emplace_back();
            P_[N_] += l * R_[0] + L_[0] * r;
            P_[N_ + 1] += L_[1] * R_[N_] + L_[N_] * R_[1];
            for (int sft = 1, os = fft_len(N_ + 1) / 2, t = N_ + 1 - os;
                 (t & 1) == 0 && 1 << sft < os; ++sft, t /= 2) {
                std::vector<Tp> P0(2 << sft), P1(2 << sft);
                std::copy_n(L_.begin() + (N_ + 1 - (1 << sft)), 1 << sft, P0.begin());
                std::copy_n(R_.begin() + (N_ + 1 - (1 << sft)), 1 << sft, P1.begin());
                fft(P0);
                fft(P1);
                for (int i = 0; i < 2 << sft; ++i)
                    P0[i] = P0[i] * dftR_[sft - 1][i] + P1[i] * dftL_[sft - 1][i];
                inv_fft(P0);
                for (int i = 0; i < (2 << sft) - 1; ++i) P_[N_ + 1 + i] += P0[i];
            }
            if (((N_ + 1) & N_) == 0) {
                auto &&LL = dftL_.emplace_back(N_ + 1);
                auto &&RR = dftR_.emplace_back(N_ + 1);
                std::copy_n(L_.begin() + (N_ + 1) / 2, (N_ + 1) / 2, LL.begin());
                std::copy_n(R_.begin() + (N_ + 1) / 2, (N_ + 1) / 2, RR.begin());
                fft(LL);
                fft(RR);
                auto PP = LL;
                for (int i = 0; i < N_ + 1; ++i) PP[i] *= RR[i];
                inv_fft(PP);
                for (int i = 0; i < N_; ++i) P_[N_ + 1 + i] += PP[i];
            }
        } else {
            if (N_ == 0) P_[0] = l * r;
            if (N_ == 1) P_[1] = l * R_[0] + L_[0] * r, P_[2] = L_[1] * R_[1];
        }
        return P_[N_++];
    }
};

template<typename Tp> class RelaxedFPSInv {
    // f * 1/f = 1
    // [x^N] f * 1/f = R.peek() + f(0) * [x^N] (1/f) + ([x^N] f) * f(0)^-1
    //               = delta(N=0)
public:
    RelaxedConvolution<Tp> R;
    Tp Inv0;

    Tp push(const Tp &c) {
        if (__builtin_expect(R.count() > 0, 1)) {
            const Tp res = -(R.peek() + c * Inv0) * Inv0;
            R.push(c, res);
            return res;
        }
        assert(c != 0);
        R.push(c, Inv0 = c.inv());
        return Inv0;
    }
};

template<typename Tp> class RelaxedFPSDiv {
    // f/g * g = f
    // [x^N] f/g * g = R.peek() + (f/g)(0) * [x^N] g + ([x^N] f/g) * g(0)
    //               = [x^N] f
public:
    RelaxedConvolution<Tp> R;
    Tp Inv0;

    Tp push(const Tp &p, const Tp &q) {
        if (__builtin_expect(R.count() > 0, 1)) {
            const Tp res = (p - R.peek() - R.lhs().front() * q) * Inv0;
            R.push(res, q);
            return res;
        }
        assert(q != 0);
        const Tp res = p * (Inv0 = q.inv());
        R.push(res, q);
        return res;
    }
};

template<typename Tp> class RelaxedFPSLog {
public:
    RelaxedFPSDiv<Tp> R;
    Tp LastCoeff = Tp(1);
    bool First   = true;

    Tp push(const Tp &c) {
        if (__builtin_expect(!First, 1)) {
            const int ind = R.R.count() + 1;
            const Tp res  = R.push(c * ind, LastCoeff) / ind;
            LastCoeff     = c;
            return res;
        }
        assert(c == 1);
        First = false;
        return Tp(0);
    }
};

template<typename Tp> class RelaxedFPSExp {
    // D(exp(f)) = exp(f) * D(f)
public:
    RelaxedConvolution<Tp> R;
    Tp LastCoeff = Tp(1);
    bool First   = true;

    Tp push(const Tp &c) {
        if (__builtin_expect(!First, 1)) {
            const int ind = R.count() + 1;
            const Tp res  = R.push(LastCoeff, c * ind) / ind;
            LastCoeff     = res;
            return res;
        }
        assert(c == 0);
        First = false;
        return Tp(1);
    }
};
Back to top page