cp-library-cpp

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

View the Project on GitHub suisen-cp/cp-library-cpp

:heavy_check_mark: test/src/convolution/polynomial_eval_multipoint_eval/nim_counting.test.cpp

Depends on

Code

#define PROBLEM "https://atcoder.jp/contests/abc212/tasks/abc212_h"

#include <iostream>
#include <atcoder/convolution>
#include <atcoder/modint>

#include "library/polynomial/fps.hpp"
#include "library/transform/walsh_hadamard.hpp"
#include "library/convolution/polynomial_eval_multipoint_eval.hpp"
using namespace suisen;

using mint = atcoder::modint998244353;

constexpr int M = 1 << 16;

int main() {
    FPS<mint>::set_multiplication([](const auto& f, const auto& g) { return atcoder::convolution(f, g); });

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, k;
    std::cin >> n >> k;

    std::vector<mint> c(M, 0);
    for (int i = 0; i < k; ++i) {
        int v;
        std::cin >> v;
        ++c[v];
    }

    FPS<mint> f(n + 1, 1);
    f[0] = 0;

    using namespace walsh_hadamard_transform;

    auto res = polynomial_eval<mint, walsh_hadamard<mint>, walsh_hadamard_inv<mint>>(c, f);

    std::cout << std::accumulate(res.begin() + 1, res.end(), mint(0)).val() << std::endl;

    return 0;
}
#line 1 "test/src/convolution/polynomial_eval_multipoint_eval/nim_counting.test.cpp"
#define PROBLEM "https://atcoder.jp/contests/abc212/tasks/abc212_h"

#include <iostream>
#include <atcoder/convolution>
#include <atcoder/modint>

#line 1 "library/polynomial/fps.hpp"



#include <algorithm>
#include <cassert>
#line 7 "library/polynomial/fps.hpp"
#include <queue>

#line 1 "library/polynomial/fps_naive.hpp"



#line 5 "library/polynomial/fps_naive.hpp"
#include <cmath>
#include <limits>
#include <type_traits>
#include <vector>

#line 1 "library/type_traits/type_traits.hpp"



#line 7 "library/type_traits/type_traits.hpp"

namespace suisen {
    template <typename ...Constraints> using constraints_t = std::enable_if_t<std::conjunction_v<Constraints...>, std::nullptr_t>;

    template <typename T, typename = std::nullptr_t> struct bitnum { static constexpr int value = 0; };
    template <typename T> struct bitnum<T, constraints_t<std::is_integral<T>>> { static constexpr int value = std::numeric_limits<std::make_unsigned_t<T>>::digits; };
    template <typename T> static constexpr int bitnum_v = bitnum<T>::value;
    template <typename T, size_t n> struct is_nbit { static constexpr bool value = bitnum_v<T> == n; };
    template <typename T, size_t n> static constexpr bool is_nbit_v = is_nbit<T, n>::value;

    template <typename T, typename = std::nullptr_t> struct safely_multipliable { using type = T; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 32>>> { using type = long long; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_signed<T>, is_nbit<T, 64>>> { using type = __int128_t; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 32>>> { using type = unsigned long long; };
    template <typename T> struct safely_multipliable<T, constraints_t<std::is_unsigned<T>, is_nbit<T, 64>>> { using type = __uint128_t; };
    template <typename T> using safely_multipliable_t = typename safely_multipliable<T>::type;

    template <typename T, typename = void> struct rec_value_type { using type = T; };
    template <typename T> struct rec_value_type<T, std::void_t<typename T::value_type>> {
        using type = typename rec_value_type<typename T::value_type>::type;
    };
    template <typename T> using rec_value_type_t = typename rec_value_type<T>::type;

    template <typename T> class is_iterable {
        template <typename T_> static auto test(T_ e) -> decltype(e.begin(), e.end(), std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_iterable_v = is_iterable<T>::value;
    template <typename T> class is_writable {
        template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::ostream&>() << e, std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_writable_v = is_writable<T>::value;
    template <typename T> class is_readable {
        template <typename T_> static auto test(T_ e) -> decltype(std::declval<std::istream&>() >> e, std::true_type{});
        static std::false_type test(...);
    public:
        static constexpr bool value = decltype(test(std::declval<T>()))::value;
    };
    template <typename T> static constexpr bool is_readable_v = is_readable<T>::value;
} // namespace suisen

#line 11 "library/polynomial/fps_naive.hpp"

#line 1 "library/math/modint_extension.hpp"



#line 5 "library/math/modint_extension.hpp"
#include <optional>

/**
 * refernce: https://37zigen.com/tonelli-shanks-algorithm/
 * calculates x s.t. x^2 = a mod p in O((log p)^2).
 */
template <typename mint>
std::optional<mint> safe_sqrt(mint a) {
    static int p = mint::mod();
    if (a == 0) return std::make_optional(0);
    if (p == 2) return std::make_optional(a);
    if (a.pow((p - 1) / 2) != 1) return std::nullopt;
    mint b = 1;
    while (b.pow((p - 1) / 2) == 1) ++b;
    static int tlz = __builtin_ctz(p - 1), q = (p - 1) >> tlz;
    mint x = a.pow((q + 1) / 2);
    b = b.pow(q);
    for (int shift = 2; x * x != a; ++shift) {
        mint e = a.inv() * x * x;
        if (e.pow(1 << (tlz - shift)) != 1) x *= b;
        b *= b;
    }
    return std::make_optional(x);
}

/**
 * calculates x s.t. x^2 = a mod p in O((log p)^2).
 * if not exists, raises runtime error.
 */
template <typename mint>
auto sqrt(mint a) -> decltype(mint::mod(), mint()) {
    return *safe_sqrt(a);
}
template <typename mint>
auto log(mint a) -> decltype(mint::mod(), mint()) {
    assert(a == 1);
    return 0;
}
template <typename mint>
auto exp(mint a) -> decltype(mint::mod(), mint()) {
    assert(a == 0);
    return 1;
}
template <typename mint, typename T>
auto pow(mint a, T b) -> decltype(mint::mod(), mint()) {
    return a.pow(b);
}
template <typename mint>
auto inv(mint a) -> decltype(mint::mod(), mint()) {
    return a.inv();
}


#line 1 "library/math/inv_mods.hpp"



#line 5 "library/math/inv_mods.hpp"

namespace suisen {
    template <typename mint>
    class inv_mods {
    public:
        inv_mods() = default;
        inv_mods(int n) { ensure(n); }
        const mint& operator[](int i) const {
            ensure(i);
            return invs[i];
        }
        static void ensure(int n) {
            int sz = invs.size();
            if (sz < 2) invs = { 0, 1 }, sz = 2;
            if (sz < n + 1) {
                invs.resize(n + 1);
                for (int i = sz; i <= n; ++i) invs[i] = mint(mod - mod / i) * invs[mod % i];
            }
        }
    private:
        static std::vector<mint> invs;
        static constexpr int mod = mint::mod();
    };
    template <typename mint>
    std::vector<mint> inv_mods<mint>::invs{};

    template <typename mint>
    std::vector<mint> get_invs(const std::vector<mint>& vs) {
        const int n = vs.size();

        mint p = 1;
        for (auto& e : vs) {
            p *= e;
            assert(e != 0);
        }
        mint ip = p.inv();

        std::vector<mint> rp(n + 1);
        rp[n] = 1;
        for (int i = n - 1; i >= 0; --i) {
            rp[i] = rp[i + 1] * vs[i];
        }
        std::vector<mint> res(n);
        for (int i = 0; i < n; ++i) {
            res[i] = ip * rp[i + 1];
            ip *= vs[i];
        }
        return res;
    }
}


#line 14 "library/polynomial/fps_naive.hpp"

namespace suisen {
    template <typename T>
    struct FPSNaive : std::vector<T> {
        static inline int MAX_SIZE = std::numeric_limits<int>::max() / 2;

        using value_type = T;
        using element_type = rec_value_type_t<T>;
        using std::vector<value_type>::vector;

        FPSNaive(const std::initializer_list<value_type> l) : std::vector<value_type>::vector(l) {}
        FPSNaive(const std::vector<value_type>& v) : std::vector<value_type>::vector(v) {}

        static void set_max_size(int n) {
            FPSNaive<T>::MAX_SIZE = n;
        }

        const value_type operator[](int n) const {
            return n <= deg() ? unsafe_get(n) : value_type{ 0 };
        }
        value_type& operator[](int n) {
            return ensure_deg(n), unsafe_get(n);
        }

        int size() const {
            return std::vector<value_type>::size();
        }
        int deg() const {
            return size() - 1;
        }
        int normalize() {
            while (size() and this->back() == value_type{ 0 }) this->pop_back();
            return deg();
        }
        FPSNaive& cut_inplace(int n) {
            if (size() > n) this->resize(std::max(0, n));
            return *this;
        }
        FPSNaive cut(int n) const {
            FPSNaive f = FPSNaive(*this).cut_inplace(n);
            return f;
        }

        FPSNaive operator+() const {
            return FPSNaive(*this);
        }
        FPSNaive operator-() const {
            FPSNaive f(*this);
            for (auto& e : f) e = -e;
            return f;
        }
        FPSNaive& operator++() { return ++(*this)[0], * this; }
        FPSNaive& operator--() { return --(*this)[0], * this; }
        FPSNaive& operator+=(const value_type x) { return (*this)[0] += x, *this; }
        FPSNaive& operator-=(const value_type x) { return (*this)[0] -= x, *this; }
        FPSNaive& operator+=(const FPSNaive& g) {
            ensure_deg(g.deg());
            for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) += g.unsafe_get(i);
            return *this;
        }
        FPSNaive& operator-=(const FPSNaive& g) {
            ensure_deg(g.deg());
            for (int i = 0; i <= g.deg(); ++i) unsafe_get(i) -= g.unsafe_get(i);
            return *this;
        }
        FPSNaive& operator*=(const FPSNaive& g) { return *this = *this * g; }
        FPSNaive& operator*=(const value_type x) {
            for (auto& e : *this) e *= x;
            return *this;
        }
        FPSNaive& operator/=(const FPSNaive& g) { return *this = *this / g; }
        FPSNaive& operator%=(const FPSNaive& g) { return *this = *this % g; }
        FPSNaive& operator<<=(const int shamt) {
            this->insert(this->begin(), shamt, value_type{ 0 });
            return *this;
        }
        FPSNaive& operator>>=(const int shamt) {
            if (shamt > size()) this->clear();
            else this->erase(this->begin(), this->begin() + shamt);
            return *this;
        }

        friend FPSNaive operator+(FPSNaive f, const FPSNaive& g) { f += g; return f; }
        friend FPSNaive operator+(FPSNaive f, const value_type& x) { f += x; return f; }
        friend FPSNaive operator-(FPSNaive f, const FPSNaive& g) { f -= g; return f; }
        friend FPSNaive operator-(FPSNaive f, const value_type& x) { f -= x; return f; }
        friend FPSNaive operator*(const FPSNaive& f, const FPSNaive& g) {
            if (f.empty() or g.empty()) return FPSNaive{};
            const int n = f.size(), m = g.size();
            FPSNaive h(std::min(MAX_SIZE, n + m - 1));
            for (int i = 0; i < n; ++i) for (int j = 0; j < m; ++j) {
                if (i + j >= MAX_SIZE) break;
                h.unsafe_get(i + j) += f.unsafe_get(i) * g.unsafe_get(j);
            }
            return h;
        }
        friend FPSNaive operator*(FPSNaive f, const value_type& x) { f *= x; return f; }
        friend FPSNaive operator/(FPSNaive f, const FPSNaive& g) { return std::move(f.div_mod(g).first); }
        friend FPSNaive operator%(FPSNaive f, const FPSNaive& g) { return std::move(f.div_mod(g).second); }
        friend FPSNaive operator*(const value_type x, FPSNaive f) { f *= x; return f; }
        friend FPSNaive operator<<(FPSNaive f, const int shamt) { f <<= shamt; return f; }
        friend FPSNaive operator>>(FPSNaive f, const int shamt) { f >>= shamt; return f; }

        std::pair<FPSNaive, FPSNaive> div_mod(FPSNaive g) const {
            FPSNaive f = *this;
            const int fd = f.normalize(), gd = g.normalize();
            assert(gd >= 0);
            if (fd < gd) return { FPSNaive{}, f };
            if (gd == 0) return { f *= g.unsafe_get(0).inv(), FPSNaive{} };
            const int k = f.deg() - gd;
            value_type head_inv = g.unsafe_get(gd).inv();
            FPSNaive q(k + 1);
            for (int i = k; i >= 0; --i) {
                value_type div = f.unsafe_get(i + gd) * head_inv;
                q.unsafe_get(i) = div;
                for (int j = 0; j <= gd; ++j) f.unsafe_get(i + j) -= div * g.unsafe_get(j);
            }
            f.cut_inplace(gd);
            f.normalize();
            return { q, f };
        }

        friend bool operator==(const FPSNaive& f, const FPSNaive& g) {
            const int n = f.size(), m = g.size();
            if (n < m) return g == f;
            for (int i = 0; i < m; ++i) if (f.unsafe_get(i) != g.unsafe_get(i)) return false;
            for (int i = m; i < n; ++i) if (f.unsafe_get(i) != 0) return false;
            return true;
        }
        friend bool operator!=(const FPSNaive& f, const FPSNaive& g) {
            return not (f == g);
        }

        FPSNaive mul(const FPSNaive& g, int n = -1) const {
            if (n < 0) n = size();
            if (this->empty() or g.empty()) return FPSNaive{};
            const int m = size(), k = g.size();
            FPSNaive h(std::min(n, m + k - 1));
            for (int i = 0; i < m; ++i) {
                for (int j = 0, jr = std::min(k, n - i); j < jr; ++j) {
                    h.unsafe_get(i + j) += unsafe_get(i) * g.unsafe_get(j);
                }
            }
            return h;
        }
        FPSNaive diff() const {
            if (this->empty()) return {};
            FPSNaive g(size() - 1);
            for (int i = 1; i <= deg(); ++i) g.unsafe_get(i - 1) = unsafe_get(i) * i;
            return g;
        }
        FPSNaive intg() const {
            const int n = size();
            FPSNaive g(n + 1);
            for (int i = 0; i < n; ++i) g.unsafe_get(i + 1) = unsafe_get(i) * invs[i + 1];
            if (g.deg() > MAX_SIZE) g.cut_inplace(MAX_SIZE);
            return g;
        }
        FPSNaive inv(int n = -1) const {
            if (n < 0) n = size();
            FPSNaive g(n);
            const value_type inv_f0 = ::inv(unsafe_get(0));
            g.unsafe_get(0) = inv_f0;
            for (int i = 1; i < n; ++i) {
                for (int j = 1; j <= i; ++j) g.unsafe_get(i) -= g.unsafe_get(i - j) * (*this)[j];
                g.unsafe_get(i) *= inv_f0;
            }
            return g;
        }
        FPSNaive exp(int n = -1) const {
            if (n < 0) n = size();
            assert(unsafe_get(0) == value_type{ 0 });
            FPSNaive g(n);
            g.unsafe_get(0) = value_type{ 1 };
            for (int i = 1; i < n; ++i) {
                for (int j = 1; j <= i; ++j) g.unsafe_get(i) += j * g.unsafe_get(i - j) * (*this)[j];
                g.unsafe_get(i) *= invs[i];
            }
            return g;
        }
        FPSNaive log(int n = -1) const {
            if (n < 0) n = size();
            assert(unsafe_get(0) == value_type{ 1 });
            FPSNaive g(n);
            g.unsafe_get(0) = value_type{ 0 };
            for (int i = 1; i < n; ++i) {
                g.unsafe_get(i) = i * (*this)[i];
                for (int j = 1; j < i; ++j) g.unsafe_get(i) -= (i - j) * g.unsafe_get(i - j) * (*this)[j];
                g.unsafe_get(i) *= invs[i];
            }
            return g;
        }
        FPSNaive pow(const long long k, int n = -1) const {
            if (n < 0) n = size();
            if (k == 0) {
                FPSNaive res(n);
                res[0] = 1;
                return res;
            }
            int z = 0;
            while (z < size() and unsafe_get(z) == value_type{ 0 }) ++z;
            if (z == size() or z > (n - 1) / k) return FPSNaive(n, 0);
            const int m = n - z * k;

            FPSNaive g(m);
            const value_type inv_f0 = ::inv(unsafe_get(z));
            g.unsafe_get(0) = unsafe_get(z).pow(k);
            for (int i = 1; i < m; ++i) {
                for (int j = 1; j <= i; ++j) g.unsafe_get(i) += (element_type{ k } *j - (i - j)) * g.unsafe_get(i - j) * (*this)[z + j];
                g.unsafe_get(i) *= inv_f0 * invs[i];
            }
            g <<= z * k;
            return g;
        }

        std::optional<FPSNaive> safe_sqrt(int n = -1) const {
            if (n < 0) n = size();
            int dl = 0;
            while (dl < size() and unsafe_get(dl) == value_type{ 0 }) ++dl;
            if (dl == size()) return FPSNaive(n, 0);
            if (dl & 1) return std::nullopt;

            const int m = n - dl / 2;

            FPSNaive g(m);
            auto opt_g0 = ::safe_sqrt((*this)[dl]);
            if (not opt_g0.has_value()) return std::nullopt;
            g.unsafe_get(0) = *opt_g0;
            value_type inv_2g0 = ::inv(2 * g.unsafe_get(0));
            for (int i = 1; i < m; ++i) {
                g.unsafe_get(i) = (*this)[dl + i];
                for (int j = 1; j < i; ++j) g.unsafe_get(i) -= g.unsafe_get(j) * g.unsafe_get(i - j);
                g.unsafe_get(i) *= inv_2g0;
            }
            g <<= dl / 2;
            return g;
        }
        FPSNaive sqrt(int n = -1) const {
            if (n < 0) n = size();
            return *safe_sqrt(n);
        }

        value_type eval(value_type x) const {
            value_type y = 0;
            for (int i = size() - 1; i >= 0; --i) y = y * x + unsafe_get(i);
            return y;
        }

    private:
        static inline inv_mods<element_type> invs;

        void ensure_deg(int d) {
            if (deg() < d) this->resize(d + 1, value_type{ 0 });
        }
        const value_type& unsafe_get(int i) const {
            return std::vector<value_type>::operator[](i);
        }
        value_type& unsafe_get(int i) {
            return std::vector<value_type>::operator[](i);
        }
    };
} // namespace suisen

template <typename mint>
suisen::FPSNaive<mint> sqrt(suisen::FPSNaive<mint> a) {
    return a.sqrt();
}
template <typename mint>
suisen::FPSNaive<mint> log(suisen::FPSNaive<mint> a) {
    return a.log();
}
template <typename mint>
suisen::FPSNaive<mint> exp(suisen::FPSNaive<mint> a) {
    return a.exp();
}
template <typename mint, typename T>
suisen::FPSNaive<mint> pow(suisen::FPSNaive<mint> a, T b) {
    return a.pow(b);
}
template <typename mint>
suisen::FPSNaive<mint> inv(suisen::FPSNaive<mint> a) {
    return a.inv();
}


#line 12 "library/polynomial/fps.hpp"

namespace suisen {
    template <typename mint>
    using convolution_t = std::vector<mint>(*)(const std::vector<mint>&, const std::vector<mint>&);

    template <typename mint>
    struct FPS : public std::vector<mint> {
        using base_type = std::vector<mint>;
        using value_type = typename base_type::value_type;
        using base_type::vector;

        FPS(const std::initializer_list<mint> l) : std::vector<mint>::vector(l) {}
        FPS(const std::vector<mint>& v) : std::vector<mint>::vector(v) {}
        FPS(std::vector<mint>&& v) : std::vector<mint>::vector(std::move(v)) {}

        static void set_multiplication(convolution_t<mint> multiplication) {
            FPS<mint>::mult = multiplication;
        }

        int size() const noexcept {
            return base_type::size();
        }
        int deg() const noexcept {
            return size() - 1;
        }
        void ensure(int n) {
            if (size() < n) this->resize(n);
        }

        value_type safe_get(int d) const {
            return d <= deg() ? (*this)[d] : 0;
        }
        value_type& safe_get(int d) {
            ensure(d + 1);
            return (*this)[d];
        }

        FPS& cut_trailing_zeros() {
            while (this->size() and this->back() == 0) this->pop_back();
            return *this;
        }
        FPS& cut(int n) {
            if (size() > n) this->resize(std::max(0, n));
            return *this;
        }
        FPS cut_copy(int n) const {
            FPS res(this->begin(), this->begin() + std::min(size(), n));
            res.ensure(n);
            return res;
        }
        FPS cut_copy(int l, int r) const {
            if (l >= size()) return FPS(r - l, 0);
            FPS res(this->begin() + l, this->begin() + std::min(size(), r));
            res.ensure(r - l);
            return res;
        }

        /* Unary Operations */

        FPS operator+() const { return *this; }
        FPS operator-() const {
            FPS res = *this;
            for (auto& e : res) e = -e;
            return res;
        }
        FPS& operator++() { return ++safe_get(0), * this; }
        FPS& operator--() { return --safe_get(0), * this; }
        FPS operator++(int) {
            FPS res = *this;
            ++(*this);
            return res;
        }
        FPS operator--(int) {
            FPS res = *this;
            --(*this);
            return res;
        }

        /* Binary Operations With Constant */

        FPS& operator+=(const value_type& x) { return safe_get(0) += x, *this; }
        FPS& operator-=(const value_type& x) { return safe_get(0) -= x, *this; }
        FPS& operator*=(const value_type& x) {
            for (auto& e : *this) e *= x;
            return *this;
        }
        FPS& operator/=(const value_type& x) { return *this *= x.inv(); }

        friend FPS operator+(FPS f, const value_type& x) { f += x; return f; }
        friend FPS operator+(const value_type& x, FPS f) { f += x; return f; }
        friend FPS operator-(FPS f, const value_type& x) { f -= x; return f; }
        friend FPS operator-(const value_type& x, FPS f) { f -= x; return -f; }
        friend FPS operator*(FPS f, const value_type& x) { f *= x; return f; }
        friend FPS operator*(const value_type& x, FPS f) { f *= x; return f; }
        friend FPS operator/(FPS f, const value_type& x) { f /= x; return f; }

        /* Binary Operations With Formal Power Series */

        FPS& operator+=(const FPS& g) {
            const int n = g.size();
            ensure(n);
            for (int i = 0; i < n; ++i) (*this)[i] += g[i];
            return *this;
        }
        FPS& operator-=(const FPS& g) {
            const int n = g.size();
            ensure(n);
            for (int i = 0; i < n; ++i) (*this)[i] -= g[i];
            return *this;
        }
        FPS& operator*=(const FPS& g) { return *this = *this * g; }
        FPS& operator/=(const FPS& g) { return *this = *this / g; }
        FPS& operator%=(const FPS& g) { return *this = *this % g; }

        friend FPS operator+(FPS f, const FPS& g) { f += g; return f; }
        friend FPS operator-(FPS f, const FPS& g) { f -= g; return f; }
        friend FPS operator*(const FPS& f, const FPS& g) { return mult(f, g); }
        friend FPS operator/(FPS f, FPS g) {
            if (f.size() < 60) return FPSNaive<mint>(f).div_mod(g).first;
            f.cut_trailing_zeros(), g.cut_trailing_zeros();
            const int fd = f.deg(), gd = g.deg();
            assert(gd >= 0);
            if (fd < gd) return {};
            if (gd == 0) {
                f /= g[0];
                return f;
            }
            std::reverse(f.begin(), f.end()), std::reverse(g.begin(), g.end());
            const int qd = fd - gd;
            FPS q = f * g.inv(qd + 1);
            q.cut(qd + 1);
            std::reverse(q.begin(), q.end());
            return q;
        }
        friend FPS operator%(const FPS& f, const FPS& g) { return f.div_mod(g).second; }
        std::pair<FPS, FPS> div_mod(const FPS& g) const {
            if (size() < 60) {
                auto [q, r] = FPSNaive<mint>(*this).div_mod(g);
                return { q, r };
            }
            FPS q = *this / g, r = *this - g * q;
            r.cut_trailing_zeros();
            return { q, r };
        }

        /* Shift Operations */

        FPS& operator<<=(const int shamt) {
            return this->insert(this->begin(), shamt, 0), * this;
        }
        FPS& operator>>=(const int shamt) {
            return this->erase(this->begin(), this->begin() + std::min(shamt, size())), * this;
        }
        friend FPS operator<<(FPS f, const int shamt) { f <<= shamt; return f; }
        friend FPS operator>>(FPS f, const int shamt) { f >>= shamt; return f; }

        /* Compare */

        friend bool operator==(const FPS& f, const FPS& g) {
            const int n = f.size(), m = g.size();
            if (n < m) return g == f;
            for (int i = 0; i < m; ++i) if (f[i] != g[i]) return false;
            for (int i = m; i < n; ++i) if (f[i] != 0) return false;
            return true;
        }
        friend bool operator!=(const FPS& f, const FPS& g) { return not (f == g); }

        /* Other Operations */

        FPS& diff_inplace() {
            const int n = size();
            for (int i = 1; i < n; ++i) (*this)[i - 1] = (*this)[i] * i;
            return (*this)[n - 1] = 0, *this;
        }
        FPS diff() const {
            FPS res = *this;
            res.diff_inplace();
            return res;
        }
        FPS& intg_inplace() {
            const int n = size();
            inv_mods<value_type> invs(n);
            this->resize(n + 1);
            for (int i = n; i > 0; --i) (*this)[i] = (*this)[i - 1] * invs[i];
            return (*this)[0] = 0, *this;
        }
        FPS intg() const {
            FPS res = *this;
            res.intg_inplace();
            return res;
        }
        
        FPS& inv_inplace(const int n = -1) { return *this = inv(n); } 
        FPS inv(int n = -1) const {
            if (n < 0) n = size();
            if (n < 60) return FPSNaive<mint>(*this).inv(n);
            if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return inv_sparse(std::move(*sp_f), n);
            FPS g{ (*this)[0].inv() };
            for (int k = 1; k < n; k *= 2) {
                FPS f_lo = cut_copy(k), f_hi = cut_copy(k, 2 * k);
                FPS h = (f_hi * g).cut(k) + ((f_lo * g) >>= k);
                FPS g_hi = g * h;
                g.resize(2 * k);
                for (int i = 0; i < k; ++i) g[k + i] = -g_hi[i];
            }
            g.resize(n);
            return g;
        }
        FPS& log_inplace(int n = -1) { return *this = log(n); }
        FPS log(int n = -1) const {
            assert(safe_get(0) == 1);
            if (n < 0) n = size();
            if (n < 60) return FPSNaive<mint>(cut_copy(n)).log(n);
            if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return log_sparse(std::move(*sp_f), n);
            FPS res = inv(n) * diff();
            res.resize(n - 1);
            return res.intg();
        }
        FPS& exp_inplace(int n = -1) { return *this = exp(n); }
        FPS exp(int n = -1) {
            assert(safe_get(0) == 0);
            if (n < 0) n = size();
            if (n < 60) return FPSNaive<mint>(cut_copy(n)).exp(n);
            if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return exp_sparse(std::move(*sp_f), n);
            FPS res{ 1 };
            for (int k = 1; k < n; k *= 2) res *= ++(cut_copy(k * 2) - res.log(k * 2)), res.cut(k * 2);
            res.resize(n);
            return res;
        }
        FPS& pow_inplace(long long k, int n = -1) { return *this = pow(k, n); }
        FPS pow(const long long k, int n = -1) const {
            if (n < 0) n = size();
            if (n < 60) return FPSNaive<mint>(cut_copy(n)).pow(k, n);
            if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return pow_sparse(std::move(*sp_f), k, n);
            if (k == 0) {
                FPS f{ 1 };
                f.resize(n);
                return f;
            }
            int tlz = 0;
            while (tlz < size() and (*this)[tlz] == 0) ++tlz;
            if (tlz == size() or tlz > (n - 1) / k) return FPS(n, 0);
            const int m = n - tlz * k;
            FPS f = *this >> tlz;
            value_type base = f[0];
            return ((((f /= base).log(m) *= k).exp(m) *= base.pow(k)) <<= (tlz * k));
        }
        std::optional<FPS> safe_sqrt(int n = -1) const {
            if (n < 0) n = size();
            if (n < 60) return FPSNaive<mint>(cut_copy(n)).safe_sqrt(n);
            if (auto sp_f = sparse_fps_format(15); sp_f.has_value()) return safe_sqrt_sparse(std::move(*sp_f), n);
            int tlz = 0;
            while (tlz < size() and (*this)[tlz] == 0) ++tlz;
            if (tlz == size()) return FPS(n, 0);
            if (tlz & 1) return std::nullopt;
            const int m = n - tlz / 2;

            FPS h(this->begin() + tlz, this->end());
            auto q0 = ::safe_sqrt(h[0]);
            if (not q0.has_value()) return std::nullopt;
            FPS f{ *q0 }, g{ q0->inv() };
            mint inv_2 = mint(2).inv();
            for (int k = 1; k < m; k *= 2) {
                FPS tmp = h.cut_copy(2 * k) * f.inv(2 * k);
                tmp.cut(2 * k);
                f += tmp, f *= inv_2;
            }
            f.resize(m);
            f <<= tlz / 2;
            return f;
        }
        FPS& sqrt_inplace(int n = -1) { return *this = sqrt(n); }
        FPS sqrt(int n = -1) const {
            return *safe_sqrt(n);
        }

        mint eval(mint x) const {
            mint y = 0;
            for (int i = size() - 1; i >= 0; --i) y = y * x + (*this)[i];
            return y;
        }

        static FPS prod(const std::vector<FPS>& fs) {
            auto comp = [](const FPS& f, const FPS& g) { return f.size() > g.size(); };
            std::priority_queue<FPS, std::vector<FPS>, decltype(comp)> pq{ comp };
            for (const auto& f : fs) pq.push(f);
            while (pq.size() > 1) {
                auto f = pq.top();
                pq.pop();
                auto g = pq.top();
                pq.pop();
                pq.push(f * g);
            }
            return pq.top();
        }

        std::optional<std::vector<std::pair<int, value_type>>> sparse_fps_format(int max_size) const {
            std::vector<std::pair<int, value_type>> res;
            for (int i = 0; i <= deg() and int(res.size()) <= max_size; ++i) if (value_type v = (*this)[i]; v != 0) res.emplace_back(i, v);
            if (int(res.size()) > max_size) return std::nullopt;
            return res;
        }

    protected:
        static convolution_t<mint> mult;

        static FPS div_fps_sparse(const FPS& f, const std::vector<std::pair<int, value_type>>& g, int n) {
            const int siz = g.size();
            assert(siz and g[0].first == 0);
            const value_type inv_g0 = g[0].second.inv();
            FPS h(n);
            for (int i = 0; i < n; ++i) {
                value_type v = f.safe_get(i);
                for (int idx = 1; idx < siz; ++idx) {
                    const auto& [j, gj] = g[idx];
                    if (j > i) break;
                    v -= gj * h[i - j];
                }
                h[i] = v * inv_g0;
            }
            return h;
        }
        static FPS inv_sparse(const std::vector<std::pair<int, value_type>>& g, const int n) {
            return div_fps_sparse(FPS{ 1 }, g, n);
        }
        static FPS exp_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) {
            const int siz = f.size();
            assert(not siz or f[0].first != 0);
            FPS g(n);
            g[0] = 1;
            inv_mods<value_type> invs(n);
            for (int i = 1; i < n; ++i) {
                value_type v = 0;
                for (const auto& [j, fj] : f) {
                    if (j > i) break;
                    v += j * fj * g[i - j];
                }
                v *= invs[i];
                g[i] = v;
            }
            return g;
        }
        static FPS log_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) {
            const int siz = f.size();
            assert(siz and f[0].first == 0 and f[0].second == 1);
            FPS g(n);
            for (int idx = 1; idx < siz; ++idx) {
                const auto& [j, fj] = f[idx];
                if (j >= n) break;
                g[j] = j * fj;
            }
            inv_mods<value_type> invs(n);
            for (int i = 1; i < n; ++i) {
                value_type v = g[i];
                for (int idx = 1; idx < siz; ++idx) {
                    const auto& [j, fj] = f[idx];
                    if (j > i) break;
                    v -= fj * g[i - j] * (i - j);
                }
                v *= invs[i];
                g[i] = v;
            }
            return g;
        }
        static FPS pow_sparse(const std::vector<std::pair<int, value_type>>& f, const long long k, const int n) {
            if (k == 0) {
                FPS res(n, 0);
                res[0] = 1;
                return res;
            }
            const int siz = f.size();
            if (not siz) return FPS(n, 0);
            const int p = f[0].first;
            if (p > (n - 1) / k) return FPS(n, 0);
            const value_type inv_f0 = f[0].second.inv();
            const int lz = p * k;
            FPS g(n);
            g[lz] = f[0].second.pow(k);
            inv_mods<value_type> invs(n);
            for (int i = 1; lz + i < n; ++i) {
                value_type v = 0;
                for (int idx = 1; idx < siz; ++idx) {
                    auto [j, fj] = f[idx];
                    j -= p;
                    if (j > i) break;
                    v += fj * g[lz + i - j] * (value_type(k) * j - (i - j));
                }
                v *= invs[i] * inv_f0;
                g[lz + i] = v;
            }
            return g;
        }
        static std::optional<FPS> safe_sqrt_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) {
            const int siz = f.size();
            if (not siz) return FPS(n, 0);
            const int p = f[0].first;
            if (p % 2 == 1) return std::nullopt;
            if (p / 2 >= n) return FPS(n, 0);
            const value_type inv_f0 = f[0].second.inv();
            const int lz = p / 2;
            FPS g(n);
            auto opt_g0 = ::safe_sqrt(f[0].second);
            if (not opt_g0.has_value()) return std::nullopt;
            g[lz] = *opt_g0;
            value_type k = mint(2).inv();
            inv_mods<value_type> invs(n);
            for (int i = 1; lz + i < n; ++i) {
                value_type v = 0;
                for (int idx = 1; idx < siz; ++idx) {
                    auto [j, fj] = f[idx];
                    j -= p;
                    if (j > i) break;
                    v += fj * g[lz + i - j] * (k * j - (i - j));
                }
                v *= invs[i] * inv_f0;
                g[lz + i] = v;
            }
            return g;
        }
        static FPS sqrt_sparse(const std::vector<std::pair<int, value_type>>& f, const int n) {
            return *safe_sqrt(f, n);
        }
    };

    template <typename mint>
    convolution_t<mint> FPS<mint>::mult = [](const auto&, const auto&) {
        std::cerr << "convolution function is not available." << std::endl;
        assert(false);
        return std::vector<mint>{};
    };

} // namespace suisen

template <typename mint>
suisen::FPS<mint> sqrt(suisen::FPS<mint> a) {
    return a.sqrt();
}
template <typename mint>
suisen::FPS<mint> log(suisen::FPS<mint> a) {
    return a.log();
}
template <typename mint>
suisen::FPS<mint> exp(suisen::FPS<mint> a) {
    return a.exp();
}
template <typename mint, typename T>
suisen::FPS<mint> pow(suisen::FPS<mint> a, T b) {
    return a.pow(b);
}
template <typename mint>
suisen::FPS<mint> inv(suisen::FPS<mint> a) {
    return a.inv();
}


#line 1 "library/transform/walsh_hadamard.hpp"



#line 1 "library/transform/kronecker_power.hpp"



#line 5 "library/transform/kronecker_power.hpp"
#include <utility>
#line 7 "library/transform/kronecker_power.hpp"

#line 1 "library/util/default_operator.hpp"



namespace suisen {
    namespace default_operator {
        template <typename T>
        auto zero() -> decltype(T { 0 }) { return T { 0 }; }
        template <typename T>
        auto one()  -> decltype(T { 1 }) { return T { 1 }; }
        template <typename T>
        auto add(const T &x, const T &y) -> decltype(x + y) { return x + y; }
        template <typename T>
        auto sub(const T &x, const T &y) -> decltype(x - y) { return x - y; }
        template <typename T>
        auto mul(const T &x, const T &y) -> decltype(x * y) { return x * y; }
        template <typename T>
        auto div(const T &x, const T &y) -> decltype(x / y) { return x / y; }
        template <typename T>
        auto mod(const T &x, const T &y) -> decltype(x % y) { return x % y; }
        template <typename T>
        auto neg(const T &x) -> decltype(-x) { return -x; }
        template <typename T>
        auto inv(const T &x) -> decltype(one<T>() / x)  { return one<T>() / x; }
    } // default_operator
    namespace default_operator_noref {
        template <typename T>
        auto zero() -> decltype(T { 0 }) { return T { 0 }; }
        template <typename T>
        auto one()  -> decltype(T { 1 }) { return T { 1 }; }
        template <typename T>
        auto add(T x, T y) -> decltype(x + y) { return x + y; }
        template <typename T>
        auto sub(T x, T y) -> decltype(x - y) { return x - y; }
        template <typename T>
        auto mul(T x, T y) -> decltype(x * y) { return x * y; }
        template <typename T>
        auto div(T x, T y) -> decltype(x / y) { return x / y; }
        template <typename T>
        auto mod(T x, T y) -> decltype(x % y) { return x % y; }
        template <typename T>
        auto neg(T x) -> decltype(-x) { return -x; }
        template <typename T>
        auto inv(T x) -> decltype(one<T>() / x)  { return one<T>() / x; }
    } // default_operator
} // namespace suisen


#line 9 "library/transform/kronecker_power.hpp"

namespace suisen {
    namespace kronecker_power_transform {
        namespace internal {
            template <typename UnitTransform, typename ReferenceGetter, std::size_t... Seq>
            void unit_transform(UnitTransform transform, ReferenceGetter ref_getter, std::index_sequence<Seq...>) {
                transform(ref_getter(Seq)...);
            }
        }

        template <typename T, std::size_t D, auto unit_transform>
        void kronecker_power_transform(std::vector<T> &x) {
            const std::size_t n = x.size();
            for (std::size_t block = 1; block < n; block *= D) {
                for (std::size_t l = 0; l < n; l += D * block) {
                    for (std::size_t offset = l; offset < l + block; ++offset) {
                        const auto ref_getter = [&](std::size_t i) -> T& { return x[offset + i * block]; };
                        internal::unit_transform(unit_transform, ref_getter, std::make_index_sequence<D>());
                    }
                }
            }
        }

        template <typename T, typename UnitTransform>
        void kronecker_power_transform(std::vector<T> &x, const std::size_t D, UnitTransform unit_transform) {
            const std::size_t n = x.size();
            std::vector<T> work(D);
            for (std::size_t block = 1; block < n; block *= D) {
                for (std::size_t l = 0; l < n; l += D * block) {
                    for (std::size_t offset = l; offset < l + block; ++offset) {
                        for (std::size_t i = 0; i < D; ++i) work[i] = x[offset + i * block];
                        unit_transform(work);
                        for (std::size_t i = 0; i < D; ++i) x[offset + i * block] = work[i];
                    }
                }
            }
        }

        template <typename T, auto e = default_operator::zero<T>, auto add = default_operator::add<T>, auto mul = default_operator::mul<T>>
        auto kronecker_power_transform(std::vector<T> &x, const std::vector<std::vector<T>> &A) -> decltype(e(), add(std::declval<T>(), std::declval<T>()), mul(std::declval<T>(), std::declval<T>()), void()) {
            const std::size_t D = A.size();
            assert(D == A[0].size());
            auto unit_transform = [&](std::vector<T> &x) {
                std::vector<T> y(D, e());
                for (std::size_t i = 0; i < D; ++i) for (std::size_t j = 0; j < D; ++j) {
                    y[i] = add(y[i], mul(A[i][j], x[j]));
                }
                x.swap(y);
            };
            kronecker_power_transform<T>(x, D, unit_transform);
        }
    }
} // namespace suisen



#line 5 "library/transform/walsh_hadamard.hpp"

namespace suisen::walsh_hadamard_transform {
    namespace internal {
        template <typename T, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>>
        void unit_transform(T& x0, T& x1) {
            T y0 = x0, y1 = x1;
            x0 = add(y0, y1);   // 1,  1
            x1 = sub(y0, y1);   // 1, -1
        }
    } // namespace internal

    using kronecker_power_transform::kronecker_power_transform;

    template <typename T, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>>
    void walsh_hadamard(std::vector<T>& a) {
        kronecker_power_transform<T, 2, internal::unit_transform<T, add, sub>>(a);
    }
    template <typename T, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>, auto div = default_operator::div<T>, std::enable_if_t<std::is_integral_v<T>, std::nullptr_t> = nullptr>
    void walsh_hadamard_inv(std::vector<T>& a) {
        walsh_hadamard<T, add, sub>(a);
        const T n{ a.size() };
        for (auto& val : a) val = div(val, n);
    }
    template <typename T, auto add = default_operator::add<T>, auto sub = default_operator::sub<T>, auto mul = default_operator::mul<T>, auto inv = default_operator::inv<T>, std::enable_if_t<std::negation_v<std::is_integral<T>>, std::nullptr_t> = nullptr>
    void walsh_hadamard_inv(std::vector<T>& a) {
        walsh_hadamard<T, add, sub>(a);
        const T n{ a.size() };
        const T inv_n = inv(n);
        for (auto& val : a) val = mul(val, inv_n);
    }
} // namespace suisen::walsh_hadamard_transform



#line 1 "library/convolution/polynomial_eval_multipoint_eval.hpp"



#line 1 "library/polynomial/multi_point_eval.hpp"



#line 5 "library/polynomial/multi_point_eval.hpp"

namespace suisen {
    template <typename FPSType, typename T>
    std::vector<typename FPSType::value_type> multi_point_eval(const FPSType& f, const std::vector<T>& xs) {
        int n = xs.size();
        if (n == 0) return {};
        std::vector<FPSType> seg(2 * n);
        for (int i = 0; i < n; ++i) seg[n + i] = FPSType{ -xs[i], 1 };
        for (int i = n - 1; i > 0; --i) seg[i] = seg[i * 2] * seg[i * 2 + 1];
        seg[1] = f % seg[1];
        for (int i = 2; i < 2 * n; ++i) seg[i] = seg[i / 2] % seg[i];
        std::vector<typename FPSType::value_type> ys(n);
        for (int i = 0; i < n; ++i) ys[i] = seg[n + i].size() ? seg[n + i][0] : 0;
        return ys;
    }
} // namespace suisen


#line 6 "library/convolution/polynomial_eval_multipoint_eval.hpp"

namespace suisen {
    template <typename mint, auto transform, auto transform_inv>
    std::vector<mint> polynomial_eval(std::vector<mint> &&a, const FPS<mint> &f) {
        transform(a);
        a = multi_point_eval(f, a);
        transform_inv(a);
        return a;
    }

    template <typename mint, auto transform, auto transform_inv>
    std::vector<mint> polynomial_eval(const std::vector<mint> &a, const FPS<mint> &f) {
        return polynomial_eval<mint, transform, transform_inv>(std::vector<mint>(a), f);
    }
} // namespace suisen


#line 10 "test/src/convolution/polynomial_eval_multipoint_eval/nim_counting.test.cpp"
using namespace suisen;

using mint = atcoder::modint998244353;

constexpr int M = 1 << 16;

int main() {
    FPS<mint>::set_multiplication([](const auto& f, const auto& g) { return atcoder::convolution(f, g); });

    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);

    int n, k;
    std::cin >> n >> k;

    std::vector<mint> c(M, 0);
    for (int i = 0; i < k; ++i) {
        int v;
        std::cin >> v;
        ++c[v];
    }

    FPS<mint> f(n + 1, 1);
    f[0] = 0;

    using namespace walsh_hadamard_transform;

    auto res = polynomial_eval<mint, walsh_hadamard<mint>, walsh_hadamard_inv<mint>>(c, f);

    std::cout << std::accumulate(res.begin() + 1, res.end(), mint(0)).val() << std::endl;

    return 0;
}
Back to top page