cplib-cpp

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

View the Project on GitHub hitonanode/cplib-cpp

:heavy_check_mark: formal_power_series/test/sparse_fps_exp.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/exp_of_formal_power_series_sparse"
#include "../sparse_fps.hpp"
#include "../../modint.hpp"
#include <iostream>
#include <vector>
using namespace std;
using mint = ModInt<998244353>;

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<mint> f(N);
    while (K--) {
        int i, a;
        cin >> i >> a;
        f.at(i) = a;
    }

    const auto ret = sparse_fps::exp(f, N - 1);

    for (auto e : ret) cout << e << ' ';
    cout << '\n';
}
#line 1 "formal_power_series/test/sparse_fps_exp.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/exp_of_formal_power_series_sparse"
#line 2 "formal_power_series/sparse_fps.hpp"
#include <algorithm>
#include <cassert>
#include <concepts>
#include <optional>
#include <utility>
#include <vector>

namespace sparse_fps {
// https://github.com/yosupo06/library-checker-problems/issues/767#issuecomment-1166414020

// Calculate f(x)^k up to x^max_deg
template <typename Vec>
    requires std::derived_from<Vec, std::vector<typename Vec::value_type>>
Vec pow(const Vec &f, int max_deg, long long k) {
    using T = typename Vec::value_type;
    assert(k >= 0);

    Vec ret(max_deg + 1);

    if (k == 0) {
        ret[0] = T{1};
        return ret;
    }

    std::vector<std::pair<int, T>> terms;
    int d0 = 0;
    while (d0 < int(f.size()) and d0 <= max_deg and f[d0] == T()) ++d0;
    if (d0 == int(f.size()) or d0 > max_deg) return ret;

    if (d0 and max_deg / d0 < k) return ret;

    for (int d = d0 + 1; d < std::min<int>(max_deg + 1, f.size()); ++d) {
        if (f[d] != T{}) terms.emplace_back(d - d0, f[d]);
    }

    const int bias = d0 * k;
    ret[bias] = f[d0].pow(k);
    const T fd0inv = 1 / f[d0];
    for (int d = 0; bias + d + 1 < int(ret.size()); ++d) {
        // Compare [x^d](k f'g - fg')
        T tmp{0};
        for (auto [i, fi] : terms) {
            int j = d - i;
            if (0 <= j) tmp -= fi * ret[bias + j + 1] * (j + 1);
            j = d - (i - 1);
            if (0 <= j) tmp += fi * i * ret[bias + j] * T(k);
        }
        ret[bias + d + 1] = tmp * fd0inv / (d + 1);
    }

    return ret;
}

template <typename Vec>
    requires std::derived_from<Vec, std::vector<typename Vec::value_type>>
Vec inv(const Vec &f, int max_deg) {
    using T = typename Vec::value_type;
    assert(!f.empty() and f[0] != T{0});

    Vec ret(max_deg + 1);

    std::vector<std::pair<int, T>> terms;
    for (int d = 1; d < (int)f.size() and d <= max_deg; ++d) {
        if (f[d] != T{}) terms.emplace_back(d, f[d]);
    }

    const T f0inv = f[0].inv();
    ret[0] = f0inv;

    for (int d = 1; d <= max_deg; ++d) {
        T tmp{0};
        for (auto [i, fi] : terms) {
            if (i > d) break;
            tmp -= fi * ret[d - i];
        }
        ret[d] = tmp * f0inv;
    }

    return ret;
}

template <typename Vec>
    requires std::derived_from<Vec, std::vector<typename Vec::value_type>>
Vec log(const Vec &f, int max_deg) {
    using T = typename Vec::value_type;
    assert(!f.empty() and f[0] != T{0});

    const auto inv = sparse_fps::inv(f, max_deg);

    std::vector<std::pair<int, T>> df_terms;
    for (int d = 1; d < (int)f.size() and d <= max_deg; ++d) {
        if (f[d] != T{}) df_terms.emplace_back(d - 1, f[d] * T{d});
    }

    Vec ret(max_deg + 1);

    for (int d = 0; d < max_deg; ++d) {
        for (auto [i, fi] : df_terms) {
            const int j = d + i + 1;
            if (j > max_deg) break;
            ret[j] += inv[d] * fi * T{j}.inv();
        }
    }

    return ret;
}

template <typename Vec>
    requires std::derived_from<Vec, std::vector<typename Vec::value_type>>
Vec exp(const Vec &f, int max_deg) {
    using T = typename Vec::value_type;
    assert(f.empty() or f[0] == T{0});

    std::vector<std::pair<int, T>> df_terms;
    for (int d = 1; d < (int)f.size() and d <= max_deg; ++d) {
        if (f[d] != T{}) df_terms.emplace_back(d - 1, f[d] * T{d});
    }

    Vec ret(max_deg + 1);
    ret[0] = T{1};
    // F' = F * f'
    for (int d = 1; d <= max_deg; ++d) {
        T tmp{0};
        for (auto [i, dfi] : df_terms) {
            if (i > d - 1) break;
            tmp += dfi * ret[d - 1 - i];
        }
        ret[d] = tmp * T{d}.inv();
    }

    return ret;
}

template <typename Vec>
    requires std::derived_from<Vec, std::vector<typename Vec::value_type>>
std::optional<Vec> sqrt(const Vec &f, int max_deg) {
    using T = typename Vec::value_type;

    Vec ret(max_deg + 1);

    int d0 = 0;
    while (d0 < int(f.size()) and d0 <= max_deg and f[d0] == T{}) ++d0;
    if (d0 == int(f.size()) or d0 > max_deg) return ret;
    if (d0 & 1) return std::nullopt;

    const T sqrtf0 = f[d0].sqrt();
    if (sqrtf0 == T{}) return std::nullopt;

    std::vector<std::pair<int, T>> terms;
    const T fd0inv = 1 / f[d0];
    for (int d = d0 + 1; d < std::min<int>(max_deg + 1, f.size()); ++d) {
        if (f[d] != T{}) terms.emplace_back(d - d0, f[d] * fd0inv);
    }

    const int bias = d0 / 2;
    const T inv2 = T{2}.inv();
    ret[bias] = sqrtf0;
    for (int d = 0; bias + d + 1 < int(ret.size()); ++d) {
        T tmp{0};
        for (auto [i, fi] : terms) {
            if (i > d + 1) break;
            int j = d - i;
            if (0 <= j) tmp -= fi * ret[bias + j + 1] * (j + 1);
            j = d - (i - 1);
            if (0 <= j) tmp += fi * i * ret[bias + j] * inv2;
        }
        ret[bias + d + 1] = tmp / (d + 1);
    }

    return ret;
}

} // namespace sparse_fps
#line 3 "modint.hpp"
#include <iostream>
#include <set>
#line 6 "modint.hpp"

template <int md> struct ModInt {
    static_assert(md > 1);
    using lint = long long;
    constexpr static int mod() { return md; }
    static int get_primitive_root() {
        static int primitive_root = 0;
        if (!primitive_root) {
            primitive_root = [&]() {
                std::set<int> fac;
                int v = md - 1;
                for (lint i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < md; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModInt(g).pow((md - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root;
    }
    int val_;
    int val() const noexcept { return val_; }
    constexpr ModInt() : val_(0) {}
    constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
    constexpr ModInt(lint v) { _setval(v % md + md); }
    constexpr explicit operator bool() const { return val_ != 0; }
    constexpr ModInt operator+(const ModInt &x) const {
        return ModInt()._setval((lint)val_ + x.val_);
    }
    constexpr ModInt operator-(const ModInt &x) const {
        return ModInt()._setval((lint)val_ - x.val_ + md);
    }
    constexpr ModInt operator*(const ModInt &x) const {
        return ModInt()._setval((lint)val_ * x.val_ % md);
    }
    constexpr ModInt operator/(const ModInt &x) const {
        return ModInt()._setval((lint)val_ * x.inv().val() % md);
    }
    constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
    friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
    friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
    friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
    constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
    constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
    constexpr bool operator<(const ModInt &x) const {
        return val_ < x.val_;
    } // To use std::map<ModInt, T>
    friend std::istream &operator>>(std::istream &is, ModInt &x) {
        lint t;
        return is >> t, x = ModInt(t), is;
    }
    constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
        return os << x.val_;
    }

    constexpr ModInt pow(lint n) const {
        ModInt ans = 1, tmp = *this;
        while (n) {
            if (n & 1) ans *= tmp;
            tmp *= tmp, n >>= 1;
        }
        return ans;
    }

    static constexpr int cache_limit = std::min(md, 1 << 21);
    static std::vector<ModInt> facs, facinvs, invs;

    constexpr static void _precalculation(int N) {
        const int l0 = facs.size();
        if (N > md) N = md;
        if (N <= l0) return;
        facs.resize(N), facinvs.resize(N), invs.resize(N);
        for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
        facinvs[N - 1] = facs.back().pow(md - 2);
        for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
        for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
    }

    constexpr ModInt inv() const {
        if (this->val_ < cache_limit) {
            if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
            while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
            return invs[this->val_];
        } else {
            return this->pow(md - 2);
        }
    }

    constexpr static ModInt fac(int n) {
        assert(n >= 0);
        if (n >= md) return ModInt(0);
        while (n >= int(facs.size())) _precalculation(facs.size() * 2);
        return facs[n];
    }

    constexpr static ModInt facinv(int n) {
        assert(n >= 0);
        if (n >= md) return ModInt(0);
        while (n >= int(facs.size())) _precalculation(facs.size() * 2);
        return facinvs[n];
    }

    constexpr static ModInt doublefac(int n) {
        assert(n >= 0);
        if (n >= md) return ModInt(0);
        long long k = (n + 1) / 2;
        return (n & 1) ? ModInt::fac(k * 2) / (ModInt(2).pow(k) * ModInt::fac(k))
                       : ModInt::fac(k) * ModInt(2).pow(k);
    }

    constexpr static ModInt nCr(int n, int r) {
        assert(n >= 0);
        if (r < 0 or n < r) return ModInt(0);
        return ModInt::fac(n) * ModInt::facinv(r) * ModInt::facinv(n - r);
    }

    constexpr static ModInt nPr(int n, int r) {
        assert(n >= 0);
        if (r < 0 or n < r) return ModInt(0);
        return ModInt::fac(n) * ModInt::facinv(n - r);
    }

    static ModInt binom(long long n, long long r) {
        static long long bruteforce_times = 0;

        if (r < 0 or n < r) return ModInt(0);
        if (n <= bruteforce_times or n < (int)facs.size()) return ModInt::nCr(n, r);

        r = std::min(r, n - r);
        assert((int)r == r);

        ModInt ret = ModInt::facinv(r);
        for (int i = 0; i < r; ++i) ret *= n - i;
        bruteforce_times += r;

        return ret;
    }

    // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
    // Complexity: O(sum(ks))
    // Verify: https://yukicoder.me/problems/no/3178
    template <class Vec> static ModInt multinomial(const Vec &ks) {
        ModInt ret{1};
        int sum = 0;
        for (int k : ks) {
            assert(k >= 0);
            ret *= ModInt::facinv(k), sum += k;
        }
        return ret * ModInt::fac(sum);
    }
    template <class... Args> static ModInt multinomial(Args... args) {
        int sum = (0 + ... + args);
        ModInt result = (1 * ... * ModInt::facinv(args));
        return ModInt::fac(sum) * result;
    }

    // Catalan number, C_n = binom(2n, n) / (n + 1) = # of Dyck words of length 2n
    // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
    // https://oeis.org/A000108
    // Complexity: O(n)
    static ModInt catalan(int n) {
        if (n < 0) return ModInt(0);
        return ModInt::fac(n * 2) * ModInt::facinv(n + 1) * ModInt::facinv(n);
    }

    ModInt sqrt() const {
        if (val_ == 0) return 0;
        if (md == 2) return val_;
        if (pow((md - 1) / 2) != 1) return 0;
        ModInt b = 1;
        while (b.pow((md - 1) / 2) == 1) b += 1;
        int e = 0, m = md - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModInt z = b.pow(m);
        while (y != 1) {
            int j = 0;
            ModInt t = y;
            while (t != 1) j++, t *= t;
            z = z.pow(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModInt(std::min(x.val_, md - x.val_));
    }
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};

using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 6 "formal_power_series/test/sparse_fps_exp.test.cpp"
using namespace std;
using mint = ModInt<998244353>;

int main() {
    cin.tie(nullptr), ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<mint> f(N);
    while (K--) {
        int i, a;
        cin >> i >> a;
        f.at(i) = a;
    }

    const auto ret = sparse_fps::exp(f, N - 1);

    for (auto e : ret) cout << e << ' ';
    cout << '\n';
}
Back to top page