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: Convolution
(library/convolution/convolution.hpp)

transform_convolution

Depends on

Required by

Verified with

Code

#ifndef SUISEN_CONVOLUTION
#define SUISEN_CONVOLUTION

#include <cassert>
#include <vector>

#include "library/util/default_operator.hpp"

namespace suisen {
    namespace convolution {
        template <typename T, auto transform, auto inv_transform, auto mul = default_operator::mul<T>>
        std::vector<T> transform_convolution(std::vector<T> a, std::vector<T> b) {
            const std::size_t n = a.size(), m = b.size();
            assert(n == m);
            transform(a), transform(b);
            for (std::size_t i = 0; i < n; ++i) a[i] = mul(a[i], b[i]);
            inv_transform(a);
            return a;
        }
        template <typename T, auto transform, auto inv_transform, auto mul = default_operator::mul<T>>
        std::vector<T> transform_convolution(std::vector<std::vector<T>> a) {
            const std::size_t num = a.size();
            assert(num);
            const std::size_t n = a[0].size();
            for (auto &v : a) {
                assert(n == int(v.size()));
                transform(v);
            }
            auto &res = a[0];
            for (int i = 1; i < num; ++i) {
                for (int j = 0; j < n; ++j) res[j] = mul(res[j], a[i][j]);
            }
            inv_transform(res);
            return res;
        }
    }
} // namespace suisen


#endif // SUISEN_CONVOLUTION
#line 1 "library/convolution/convolution.hpp"



#include <cassert>
#include <vector>

#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 8 "library/convolution/convolution.hpp"

namespace suisen {
    namespace convolution {
        template <typename T, auto transform, auto inv_transform, auto mul = default_operator::mul<T>>
        std::vector<T> transform_convolution(std::vector<T> a, std::vector<T> b) {
            const std::size_t n = a.size(), m = b.size();
            assert(n == m);
            transform(a), transform(b);
            for (std::size_t i = 0; i < n; ++i) a[i] = mul(a[i], b[i]);
            inv_transform(a);
            return a;
        }
        template <typename T, auto transform, auto inv_transform, auto mul = default_operator::mul<T>>
        std::vector<T> transform_convolution(std::vector<std::vector<T>> a) {
            const std::size_t num = a.size();
            assert(num);
            const std::size_t n = a[0].size();
            for (auto &v : a) {
                assert(n == int(v.size()));
                transform(v);
            }
            auto &res = a[0];
            for (int i = 1; i < num; ++i) {
                for (int j = 0; j < n; ++j) res[j] = mul(res[j], a[i][j]);
            }
            inv_transform(res);
            return res;
        }
    }
} // namespace suisen
Back to top page