kyopro_library

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

View the Project on GitHub dyktr06/kyopro_library

:heavy_check_mark: test/library_checker/convolution/convolution.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/convolution_mod"
#include <iostream>
#include <vector>

#include "../../../lib/convolution/ntt.hpp"
#include "../../../lib/math/modint.hpp"

using namespace std;

using mint = ModInt<998244353>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m; cin >> n >> m;
    vector<mint> a(n), b(m);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < m; i++) cin >> b[i];
    auto c = NTT::convolution(a, b);
    for(int i = 0; i < n + m - 1; i++) cout << c[i] << (i + 1 == n + m - 1 ? '\n' : ' ');
}
#line 1 "test/library_checker/convolution/convolution.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/convolution_mod"
#include <iostream>
#include <vector>

#line 2 "lib/convolution/ntt.hpp"

/**
 * @brief Number Theoretic Transform
 */

#line 2 "lib/math/modint.hpp"

#line 4 "lib/math/modint.hpp"
#include <cassert>

/**
 * @brief ModInt
 * @docs docs/math/modint.md
 */

template <long long Modulus>
struct ModInt{
    long long val;
    static constexpr int mod() { return Modulus; }
    constexpr ModInt(const long long _val = 0) noexcept : val(_val) {
        normalize();
    }
    void normalize(){
        val = (val % Modulus + Modulus) % Modulus;
    }
    inline ModInt &operator+=(const ModInt &rhs) noexcept {
        if(val += rhs.val, val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt &operator-=(const ModInt &rhs) noexcept {
        if(val -= rhs.val, val < 0) val += Modulus;
        return *this;
    }
    inline ModInt &operator*=(const ModInt &rhs) noexcept {
        val = val * rhs.val % Modulus;
        return *this;
    }
    inline ModInt &operator/=(const ModInt &rhs) noexcept {
        val = val * inv(rhs.val).val % Modulus;
        return *this;
    }
    inline ModInt &operator++() noexcept {
        if(++val >= Modulus) val -= Modulus;
        return *this;
    }
    inline ModInt operator++(int) noexcept {
        ModInt t = val;
        if(++val >= Modulus) val -= Modulus;
        return t;
    }
    inline ModInt &operator--() noexcept {
        if(--val < 0) val += Modulus;
        return *this;
    }
    inline ModInt operator--(int) noexcept {
        ModInt t = val;
        if(--val < 0) val += Modulus;
        return t;
    }
    inline ModInt operator-() const noexcept { return (Modulus - val) % Modulus; }
    inline ModInt inv(void) const { return inv(val); }
    ModInt pow(long long n) const {
        assert(0 <= n);
        ModInt x = *this, r = 1;
        while(n){
            if(n & 1) r *= x;
            x *= x;
            n >>= 1;
        }
        return r;
    }
    ModInt inv(const long long n) const {
        long long a = n, b = Modulus, u = 1, v = 0;
        while(b){
            long long t = a / b;
            a -= t * b; std::swap(a, b);
            u -= t * v; std::swap(u, v);
        }
        u %= Modulus;
        if(u < 0) u += Modulus;
        return u;
    }
    friend inline ModInt operator+(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) += rhs; }
    friend inline ModInt operator-(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) -= rhs; }
    friend inline ModInt operator*(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) *= rhs; }
    friend inline ModInt operator/(const ModInt &lhs, const ModInt &rhs) noexcept { return ModInt(lhs) /= rhs; }
    friend inline bool operator==(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val == rhs.val; }
    friend inline bool operator!=(const ModInt &lhs, const ModInt &rhs) noexcept { return lhs.val != rhs.val; }
    friend inline std::istream &operator>>(std::istream &is, ModInt &x) noexcept {
        is >> x.val;
        x.normalize();
        return is;
    }
    friend inline std::ostream &operator<<(std::ostream &os, const ModInt &x) noexcept { return os << x.val; }
};
#line 2 "lib/math/crt.hpp"

/**
 * @brief Chinese Remainder Theorem (中国剰余定理)
 * @docs docs/math/crt.md
 */

#include <numeric>
#line 10 "lib/math/crt.hpp"

namespace CRT{
    inline long long mod(long long a, long long m){
        return (a % m + m) % m;
    }

    long long extGCD(long long a, long long b, long long &x, long long &y){
        if(b == 0){
            x = 1;
            y = 0;
            return a;
        }
        long long d = extGCD(b, a % b, y, x);
        y -= a / b * x;
        return d;
    }

    std::pair<long long, long long> chineseRem(const std::vector<long long> &b, const std::vector<long long> &m) {
        long long r = 0, M = 1;
        for(int i = 0; i < (int) b.size(); i++){
            long long p, q;
            long long d = extGCD(M, m[i], p, q);
            if((b[i] - r) % d != 0) return {0, -1};
            long long tmp = (b[i] - r) / d * p % (m[i] / d);
            r += M * tmp;
            M *= m[i] / d;
        }
        r %= M;
        if(r < 0) r += M;
        return {r, M};
    }

    // not coprime
    long long preGarner(std::vector<long long> &b, std::vector<long long> &m, const long long MOD){
        long long res = 1;
        int n = b.size();
        for(int i = 0; i < n; i++){
            for(int j = 0; j < i; j++){
                long long g = std::gcd(m[i], m[j]);
                if((b[i] - b[j]) % g != 0) return -1;
                m[i] /= g, m[j] /= g;
                // gcd の分だけ被ってるので振り分ける
                long long gi = std::gcd(m[i], g), gj = g / gi;
                do{
                    g = std::gcd(gi, gj);
                    gi *= g, gj /= g;
                }while(g != 1);
                m[i] *= gi, m[j] *= gj;
                b[i] %= m[i], b[j] %= m[j];
            }
        }
        for(auto x : m) (res *= x) %= MOD;
        return res;
    }

    long long garner(const std::vector<long long> &b, const std::vector<long long> &m, const long long MOD){
        std::vector<long long> tm = m;
        tm.push_back(MOD);
        auto inv = [&](long long a, long long m) -> long long {
            long long x, y;
            extGCD(a, m, x, y);
            return mod(x, m);
        };
        int n = b.size();
        std::vector<long long> coeffs(n + 1, 1), constants(n + 1, 0);
        for(int i = 0; i < n; i++){
            // solve "coeffs[i] * t[i] + constants[i] = b[i] (mod. m[i])
            long long t = mod((b[i] - constants[i]) * inv(coeffs[i], tm[i]), tm[i]);
            for(int j = i + 1; j < n + 1; j++){
                (constants[j] += t * coeffs[j]) %= tm[j];
                (coeffs[j] *= tm[i]) %= tm[j];
            }
        }
        return constants[n];
    }

    // ax + b ≡ 0 (mod m)
    long long modEquation(long long a, long long b, long long m, bool is_positive = false){
        a %= m; b %= m;
        b = (m - b) % m;
        long long g = std::gcd(a, m);
        if(b % g != 0) return -1;
        a /= g; b /= g; m /= g;
        if(is_positive && b == 0){
            return m;
        }
        long long x, y;
        extGCD(a, m, x, y);
        return (b * x % m + m) % m;
    }
}
#line 9 "lib/convolution/ntt.hpp"

#line 11 "lib/convolution/ntt.hpp"

namespace NTT{

    // @param n `0 <= n`
    // @return minimum non-negative `x` s.t. `n <= 2**x`
    int ceil_pow2(int n) {
        int x = 0;
        while((1U << x) < (unsigned int) (n)) x++;
        return x;
    }

    // @param n `1 <= n`
    // @return minimum non-negative `x` s.t. `(n & (1 << x)) != 0`
    int bsf(unsigned int n) {
        return __builtin_ctz(n);
    }

    int primitive_root(int m) {
        if(m == 2) return 1;
        if(m == 167772161) return 3;
        if(m == 469762049) return 3;
        if(m == 754974721) return 11;
        if(m == 998244353) return 3;
        return 1;
    }

    template <typename T>
    void butterfly(std::vector<T> &a){
        int g = primitive_root(T::mod());
        int n = int(a.size());
        int h = ceil_pow2(n);

        static bool first = true;
        static T sum_e[30];  // sum_e[i] = ies[0] * ... * ies[i - 1] * es[i]
        if(first){
            first = false;
            T es[30], ies[30];  // es[i]^(2^(2+i)) == 1
            int cnt2 = bsf(T::mod() - 1);
            T e = T(g).pow((T::mod() - 1) >> cnt2), ie = e.inv();
            for(int i = cnt2; i >= 2; i--){
                // e^(2^i) == 1
                es[i - 2] = e;
                ies[i - 2] = ie;
                e *= e;
                ie *= ie;
            }
            T now = 1;
            for(int i = 0; i <= cnt2 - 2; i++){
                sum_e[i] = es[i] * now;
                now *= ies[i];
            }
        }
        for(int ph = 1; ph <= h; ph++){
            int w = 1 << (ph - 1), p = 1 << (h - ph);
            T now = 1;
            for(int s = 0; s < w; s++){
                int offset = s << (h - ph + 1);
                for(int i = 0; i < p; i++){
                    auto l = a[i + offset];
                    auto r = a[i + offset + p] * now;
                    a[i + offset] = l + r;
                    a[i + offset + p] = l - r;
                }
                now *= sum_e[bsf(~(unsigned int) (s))];
            }
        }
    }

    template <typename T>
    void butterfly_inv(std::vector<T> &a) {
        int g = primitive_root(T::mod());
        int n = int(a.size());
        int h = ceil_pow2(n);

        static bool first = true;
        static T sum_ie[30];  // sum_ie[i] = es[0] * ... * es[i - 1] * ies[i]
        if(first){
            first = false;
            T es[30], ies[30];  // es[i]^(2^(2+i)) == 1
            int cnt2 = bsf(T::mod() - 1);
            T e = T(g).pow((T::mod() - 1) >> cnt2), ie = e.inv();
            for(int i = cnt2; i >= 2; i--){
                // e^(2^i) == 1
                es[i - 2] = e;
                ies[i - 2] = ie;
                e *= e;
                ie *= ie;
            }
            T now = 1;
            for(int i = 0; i <= cnt2 - 2; i++){
                sum_ie[i] = ies[i] * now;
                now *= es[i];
            }
        }

        for(int ph = h; ph >= 1; ph--){
            int w = 1 << (ph - 1), p = 1 << (h - ph);
            T inow = 1;
            for(int s = 0; s < w; s++){
                int offset = s << (h - ph + 1);
                for(int i = 0; i < p; i++){
                    auto l = a[i + offset];
                    auto r = a[i + offset + p];
                    a[i + offset] = l + r;
                    a[i + offset + p] = (unsigned long long) (T::mod() + l.val - r.val) * inow.val;
                }
                inow *= sum_ie[bsf(~(unsigned int) (s))];
            }
        }
    }

    template <typename T>
    std::vector<T> convolution(std::vector<T> a, std::vector<T> b){
        int n = int(a.size()), m = int(b.size());
        if(!n || !m) return {};
        if(std::min(n, m) <= 60) {
            if(n < m) {
                std::swap(n, m);
                std::swap(a, b);
            }
            std::vector<T> ans(n + m - 1);
            for(int i = 0; i < n; i++){
                for(int j = 0; j < m; j++){
                    ans[i + j] += a[i] * b[j];
                }
            }
            return ans;
        }
        int z = 1 << ceil_pow2(n + m - 1);
        a.resize(z);
        butterfly(a);
        b.resize(z);
        butterfly(b);
        for(int i = 0; i < z; i++){
            a[i] *= b[i];
        }
        butterfly_inv(a);
        a.resize(n + m - 1);
        T iz = T(z).inv();
        for(int i = 0; i < n + m - 1; i++) a[i] *= iz;
        return a;
    }

    template <typename T>
    std::vector<T> convolution_mod(const std::vector<T> &a, const std::vector<T> &b, const long long MOD){
        if(MOD == 998244353){
            return convolution(a, b);
        }
        constexpr long long M0 = 167772161;
        constexpr long long M1 = 469762049;
        constexpr long long M2 = 754974721;
        using mint0 = ModInt<M0>;
        using mint1 = ModInt<M1>;
        using mint2 = ModInt<M2>;
        int n = a.size(), m = b.size();
        std::vector<mint0> a0(n), b0(m);
        std::vector<mint1> a1(n), b1(m);
        std::vector<mint2> a2(n), b2(m);
        for(int i = 0; i < n; i++){
            a0[i] = a[i].val;
            a1[i] = a[i].val;
            a2[i] = a[i].val;
        }
        for(int i = 0; i < m; i++){
            b0[i] = b[i].val;
            b1[i] = b[i].val;
            b2[i] = b[i].val;
        }
        auto c0 = convolution(a0, b0);
        auto c1 = convolution(a1, b1);
        auto c2 = convolution(a2, b2);
        std::vector<T> ret(n + m - 1);
        for(int i = 0; i < n + m - 1; i++){
            ret[i] = CRT::garner({c0[i].val, c1[i].val, c2[i].val}, {M0, M1, M2}, MOD);
        }
        return ret;
    }
};
#line 7 "test/library_checker/convolution/convolution.test.cpp"

using namespace std;

using mint = ModInt<998244353>;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int n, m; cin >> n >> m;
    vector<mint> a(n), b(m);
    for(int i = 0; i < n; i++) cin >> a[i];
    for(int i = 0; i < m; i++) cin >> b[i];
    auto c = NTT::convolution(a, b);
    for(int i = 0; i < n + m - 1; i++) cout << c[i] << (i + 1 == n + m - 1 ? '\n' : ' ');
}
Back to top page