Fu_L's Library

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

View the Project on GitHub Fu-L/cp-library

:heavy_check_mark: verify/unit_test/convolution/convolution_ll.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#include "../../../src/template/template.hpp"
#include "../../../src/random/permuted_congruential_generator.hpp"
#include "../../../src/convolution/convolution_ll.hpp"
void test() {
    int n = rng(0, 1000), m = rng(0, 1000);
    vector<ll> a(n), b(m);
    rep(i, 0, n) a[i] = rng(-1000000, 1000000);
    rep(i, 0, m) b[i] = rng(-1000000, 1000000);
    vector<ll> c = convolution_ll(a, b);
    if(!n or !m) {
        assert(c.empty());
        return;
    }
    vector<ll> expected(n + m - 1);
    rep(i, 0, n + m - 1) {
        rep(j, 0, i + 1) {
            if(0 <= j and j < n and 0 <= i - j and i - j < m) expected[i] += a[j] * b[i - j];
        }
    }
    rep(i, 0, n + m - 1) {
        assert(c[i] == expected[i]);
    }
}
int main(void) {
    constexpr int test_num = 100;
    rep(_, 0, test_num) {
        test();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
#line 1 "verify/unit_test/convolution/convolution_ll.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/aplusb"
#line 2 "src/template/template.hpp"
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<long long, long long>;
#define rep(i, a, b) for(long long i = (a); i < (b); ++i)
#define rrep(i, a, b) for(long long i = (a); i >= (b); --i)
constexpr long long inf = 4e18;
struct SetupIO {
    SetupIO() {
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout << fixed << setprecision(30);
    }
} setup_io;
#line 3 "src/random/permuted_congruential_generator.hpp"
struct PermutedCongruentialGenerator {
    template <typename T>
        requires(is_integral_v<T> and sizeof(T) == 4) or (is_floating_point_v<T> and sizeof(T) == 8)
    inline T operator()(const T l, const T r) {
        assert(l <= r);
        if constexpr(is_integral_v<T>) {
            const unsigned int range = static_cast<unsigned int>(r - l + 1);
            return l + (next32() % range);
        } else {
            static constexpr unsigned long long denom = 1ull << 53;
            const unsigned long long x = next64() >> 11;
            const double u = static_cast<double>(x) / denom;
            return l + (r - l) * u;
        }
    }

   private:
    static constexpr unsigned long long MULT = 6364136223846793005;
    static inline unsigned long long state = chrono::steady_clock::now().time_since_epoch().count();
    inline unsigned int next32() {
        unsigned long long x = state;
        state *= MULT;
        const unsigned int count = x >> 61;
        x ^= x >> 22;
        return static_cast<unsigned int>(x >> (22 + count));
    }
    inline unsigned long long next64() {
        return (static_cast<unsigned long long>(next32()) << 32) | next32();
    }
} rng;
#line 3 "src/template/static_modint.hpp"
template <uint32_t m>
struct StaticModint {
    using mint = StaticModint;
    static constexpr uint32_t mod() {
        return m;
    }
    static constexpr mint raw(const uint32_t v) {
        mint a;
        a._v = v;
        return a;
    }
    constexpr StaticModint()
        : _v(0) {}
    template <class T>
    constexpr StaticModint(const T& v) {
        static_assert(is_integral_v<T>);
        if constexpr(is_signed_v<T>) {
            int64_t x = int64_t(v % int64_t(m));
            if(x < 0) x += m;
            _v = uint32_t(x);
        } else _v = uint32_t(v % m);
    }
    constexpr uint32_t val() const {
        return _v;
    }
    constexpr mint& operator++() {
        return *this += 1;
    }
    constexpr mint& operator--() {
        return *this -= 1;
    }
    constexpr mint operator++(int) {
        mint res = *this;
        ++*this;
        return res;
    }
    constexpr mint operator--(int) {
        mint res = *this;
        --*this;
        return res;
    }
    constexpr mint& operator+=(mint rhs) {
        if(_v >= m - rhs._v) _v -= m;
        _v += rhs._v;
        return *this;
    }
    constexpr mint& operator-=(mint rhs) {
        if(_v < rhs._v) _v += m;
        _v -= rhs._v;
        return *this;
    }
    constexpr mint& operator*=(mint rhs) {
        return *this = *this * rhs;
    }
    constexpr mint& operator/=(mint rhs) {
        return *this *= rhs.inv();
    }
    constexpr mint operator+() const {
        return *this;
    }
    constexpr mint operator-() const {
        return mint{} - *this;
    }
    constexpr mint pow(long long n) const {
        assert(0 <= n);
        if(n == 0) return 1;
        mint x = *this, r = 1;
        while(1) {
            if(n & 1) r *= x;
            n >>= 1;
            if(n == 0) return r;
            x *= x;
        }
    }
    constexpr mint inv() const {
        if constexpr(prime) {
            assert(_v);
            return pow(m - 2);
        } else {
            const auto eg = inv_gcd(_v, m);
            assert(eg.first == 1);
            return eg.second;
        }
    }
    friend constexpr mint operator+(mint lhs, mint rhs) {
        return lhs += rhs;
    }
    friend constexpr mint operator-(mint lhs, mint rhs) {
        return lhs -= rhs;
    }
    friend constexpr mint operator*(mint lhs, mint rhs) {
        return uint64_t(lhs._v) * rhs._v;
    }
    friend constexpr mint operator/(mint lhs, mint rhs) {
        return lhs /= rhs;
    }
    friend constexpr bool operator==(mint lhs, mint rhs) {
        return lhs._v == rhs._v;
    }
    friend constexpr bool operator!=(mint lhs, mint rhs) {
        return lhs._v != rhs._v;
    }
    friend istream& operator>>(istream& in, mint& x) {
        long long a;
        in >> a;
        x = a;
        return in;
    }
    friend ostream& operator<<(ostream& out, const mint& x) {
        return out << x.val();
    }

   private:
    uint32_t _v = 0;
    static constexpr bool prime = []() -> bool {
        if(m == 1) return 0;
        if(m == 2 or m == 7 or m == 61) return 1;
        if(m % 2 == 0) return 0;
        uint32_t d = m - 1;
        while(d % 2 == 0) d /= 2;
        for(uint32_t a : {2, 7, 61}) {
            uint32_t t = d;
            mint y = mint(a).pow(t);
            while(t != m - 1 && y != 1 && y != m - 1) {
                y *= y;
                t <<= 1;
            }
            if(y != m - 1 && t % 2 == 0) return 0;
        }
        return 1;
    }();
    static constexpr pair<int32_t, int32_t> inv_gcd(const int32_t a, const int32_t b) {
        if(a == 0) return {b, 0};
        int32_t s = b, t = a, m0 = 0, m1 = 1;
        while(t) {
            const int32_t u = s / t;
            s -= t * u;
            m0 -= m1 * u;
            swap(s, t);
            swap(m0, m1);
        }
        if(m0 < 0) m0 += b / s;
        return {s, m0};
    }
};
using modint998244353 = StaticModint<998244353>;
using modint1000000007 = StaticModint<1000000007>;
#line 3 "src/math/pow_mod.hpp"
constexpr long long pow_mod(long long x, long long n, const long long mod) {
    assert(n >= 0 and mod >= 1);
    x %= mod;
    if(x < 0) x += mod;
    long long res = 1;
    while(n > 0) {
        if(n & 1) res = res * x % mod;
        x = x * x % mod;
        n >>= 1;
    }
    return res;
}
#line 4 "src/math/primitive_root.hpp"
constexpr int primitive_root(const 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;
    int divs[20] = {};
    divs[0] = 2;
    int cnt = 1;
    int x = (m - 1) / 2;
    while(x % 2 == 0) x /= 2;
    for(int i = 3; (long long)(i)*i <= x; i += 2) {
        if(x % i == 0) {
            divs[cnt++] = i;
            while(x % i == 0) {
                x /= i;
            }
        }
    }
    if(x > 1) {
        divs[cnt++] = x;
    }
    for(int g = 2;; ++g) {
        bool ok = true;
        for(int i = 0; i < cnt; ++i) {
            if(pow_mod(g, (m - 1) / divs[i], m) == 1) {
                ok = false;
                break;
            }
        }
        if(ok) return g;
    }
}
#line 4 "src/convolution/convolution.hpp"
constexpr int countr_zero(const unsigned int n) {
    int res = 0;
    while(!(n & (1 << res))) ++res;
    return res;
}
template <typename mint, int g = primitive_root(mint::mod())>
struct FFT_Info {
    static constexpr int rank2 = countr_zero(mint::mod() - 1);
    array<mint, rank2 + 1> root;
    array<mint, rank2 + 1> iroot;
    array<mint, max(0, rank2 - 2 + 1)> rate2;
    array<mint, max(0, rank2 - 2 + 1)> irate2;
    array<mint, max(0, rank2 - 3 + 1)> rate3;
    array<mint, max(0, rank2 - 3 + 1)> irate3;
    FFT_Info() {
        root[rank2] = mint(g).pow((mint::mod() - 1) >> rank2);
        iroot[rank2] = root[rank2].inv();
        for(int i = rank2 - 1; i >= 0; --i) {
            root[i] = root[i + 1] * root[i + 1];
            iroot[i] = iroot[i + 1] * iroot[i + 1];
        }
        {
            mint prod = 1, iprod = 1;
            for(int i = 0; i <= rank2 - 2; ++i) {
                rate2[i] = root[i + 2] * prod;
                irate2[i] = iroot[i + 2] * iprod;
                prod *= iroot[i + 2];
                iprod *= root[i + 2];
            }
        }
        {
            mint prod = 1, iprod = 1;
            for(int i = 0; i <= rank2 - 3; ++i) {
                rate3[i] = root[i + 3] * prod;
                irate3[i] = iroot[i + 3] * iprod;
                prod *= iroot[i + 3];
                iprod *= root[i + 3];
            }
        }
    }
};
template <typename mint>
void butterfly(vector<mint>& a) {
    const int n = (int)a.size();
    const int h = __builtin_ctz((unsigned int)n);
    static const FFT_Info<mint> info;
    int len = 0;
    while(len < h) {
        if(h - len == 1) {
            const int p = 1 << (h - len - 1);
            mint rot = 1;
            for(int s = 0; s < (1 << len); ++s) {
                const int offset = s << (h - len);
                for(int i = 0; i < p; ++i) {
                    const auto l = a[i + offset];
                    const auto r = a[i + offset + p] * rot;
                    a[i + offset] = l + r;
                    a[i + offset + p] = l - r;
                }
                if(s + 1 != (1 << len)) rot *= info.rate2[__builtin_ctz(~(unsigned int)(s))];
            }
            ++len;
        } else {
            const int p = 1 << (h - len - 2);
            mint rot = 1, imag = info.root[2];
            for(int s = 0; s < (1 << len); ++s) {
                const mint rot2 = rot * rot;
                const mint rot3 = rot2 * rot;
                const int offset = s << (h - len);
                for(int i = 0; i < p; ++i) {
                    const auto mod2 = 1ULL * mint::mod() * mint::mod();
                    const auto a0 = 1ULL * a[i + offset].val();
                    const auto a1 = 1ULL * a[i + offset + p].val() * rot.val();
                    const auto a2 = 1ULL * a[i + offset + 2 * p].val() * rot2.val();
                    const auto a3 = 1ULL * a[i + offset + 3 * p].val() * rot3.val();
                    const auto a1na3imag = 1ULL * mint(a1 + mod2 - a3).val() * imag.val();
                    const auto na2 = mod2 - a2;
                    a[i + offset] = a0 + a2 + a1 + a3;
                    a[i + offset + 1 * p] = a0 + a2 + (2 * mod2 - (a1 + a3));
                    a[i + offset + 2 * p] = a0 + na2 + a1na3imag;
                    a[i + offset + 3 * p] = a0 + na2 + (mod2 - a1na3imag);
                }
                if(s + 1 != (1 << len)) rot *= info.rate3[__builtin_ctz(~(unsigned int)(s))];
            }
            len += 2;
        }
    }
}
template <typename mint>
void butterfly_inv(vector<mint>& a) {
    const int n = (int)a.size();
    const int h = __builtin_ctz((unsigned int)n);
    static const FFT_Info<mint> info;
    int len = h;
    while(len) {
        if(len == 1) {
            const int p = 1 << (h - len);
            mint irot = 1;
            for(int s = 0; s < (1 << (len - 1)); ++s) {
                const int offset = s << (h - len + 1);
                for(int i = 0; i < p; ++i) {
                    const auto l = a[i + offset];
                    const auto r = a[i + offset + p];
                    a[i + offset] = l + r;
                    a[i + offset + p] = (unsigned long long)(mint::mod() + l.val() - r.val()) * irot.val();
                }
                if(s + 1 != (1 << (len - 1))) irot *= info.irate2[__builtin_ctz(~(unsigned int)(s))];
            }
            --len;
        } else {
            const int p = 1 << (h - len);
            mint irot = 1, iimag = info.iroot[2];
            for(int s = 0; s < (1 << (len - 2)); ++s) {
                const mint irot2 = irot * irot;
                const mint irot3 = irot2 * irot;
                const int offset = s << (h - len + 2);
                for(int i = 0; i < p; ++i) {
                    const auto a0 = 1ULL * a[i + offset + 0 * p].val();
                    const auto a1 = 1ULL * a[i + offset + 1 * p].val();
                    const auto a2 = 1ULL * a[i + offset + 2 * p].val();
                    const auto a3 = 1ULL * a[i + offset + 3 * p].val();
                    const auto a2na3iimag = 1ULL * mint((mint::mod() + a2 - a3) * iimag.val()).val();
                    a[i + offset] = a0 + a1 + a2 + a3;
                    a[i + offset + 1 * p] = (a0 + (mint::mod() - a1) + a2na3iimag) * irot.val();
                    a[i + offset + 2 * p] = (a0 + a1 + (mint::mod() - a2) + (mint::mod() - a3)) * irot2.val();
                    a[i + offset + 3 * p] = (a0 + (mint::mod() - a1) + (mint::mod() - a2na3iimag)) * irot3.val();
                }
                if(s + 1 != (1 << (len - 2))) irot *= info.irate3[__builtin_ctz(~(unsigned int)(s))];
            }
            len -= 2;
        }
    }
}
template <typename mint>
vector<mint> convolution_naive(const vector<mint>& a, const vector<mint>& b) {
    const int n = (int)a.size(), m = (int)b.size();
    vector<mint> res(n + m - 1);
    if(n < m) {
        for(int j = 0; j < m; ++j) {
            for(int i = 0; i < n; ++i) {
                res[i + j] += a[i] * b[j];
            }
        }
    } else {
        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 mint>
vector<mint> convolution(vector<mint> a, vector<mint> b) {
    const int n = (int)a.size(), m = (int)b.size();
    if(n == 0 or m == 0) return {};
    int z = 1;
    while(z < n + m - 1) z *= 2;
    assert((mint::mod() - 1) % z == 0);
    if(min(n, m) <= 60) return convolution_naive(a, b);
    a.resize(z);
    b.resize(z);
    butterfly(a);
    butterfly(b);
    for(int i = 0; i < z; ++i) a[i] *= b[i];
    butterfly_inv(a);
    a.resize(n + m - 1);
    const mint iz = mint(z).inv();
    for(int i = 0; i < n + m - 1; ++i) a[i] *= iz;
    return a;
}
#line 5 "src/convolution/convolution_ll.hpp"
vector<long long> convolution_ll(const vector<long long>& a, const vector<long long>& b) {
    const int n = (int)a.size(), m = (int)b.size();
    if(!n or !m) return {};
    static constexpr unsigned long long MOD1 = 754974721;
    static constexpr unsigned long long MOD2 = 167772161;
    static constexpr unsigned long long MOD3 = 469762049;
    static constexpr unsigned long long M2M3 = MOD2 * MOD3;
    static constexpr unsigned long long M1M3 = MOD1 * MOD3;
    static constexpr unsigned long long M1M2 = MOD1 * MOD2;
    static constexpr unsigned long long M1M2M3 = MOD1 * MOD2 * MOD3;
    static constexpr unsigned long long i1 = 190329765;
    static constexpr unsigned long long i2 = 58587104;
    static constexpr unsigned long long i3 = 187290749;
    static constexpr int MAX_AB_BIT = 24;
    assert(n + m - 1 <= (1 << MAX_AB_BIT));
    using mint1 = StaticModint<MOD1>;
    using mint2 = StaticModint<MOD2>;
    using mint3 = StaticModint<MOD3>;
    vector<mint1> a1(n), b1(m);
    vector<mint2> a2(n), b2(m);
    vector<mint3> a3(n), b3(m);
    for(int i = 0; i < n; ++i) a1[i] = a[i];
    for(int i = 0; i < n; ++i) a2[i] = a[i];
    for(int i = 0; i < n; ++i) a3[i] = a[i];
    for(int i = 0; i < m; ++i) b1[i] = b[i];
    for(int i = 0; i < m; ++i) b2[i] = b[i];
    for(int i = 0; i < m; ++i) b3[i] = b[i];
    vector<mint1> c1 = convolution<mint1>(a1, b1);
    vector<mint2> c2 = convolution<mint2>(a2, b2);
    vector<mint3> c3 = convolution<mint3>(a3, b3);
    vector<long long> c(n + m - 1);
    for(int i = 0; i < n + m - 1; ++i) {
        unsigned long long x = 0;
        x += (c1[i].val() * i1) % MOD1 * M2M3;
        x += (c2[i].val() * i2) % MOD2 * M1M3;
        x += (c3[i].val() * i3) % MOD3 * M1M2;
        long long diff = c1[i].val() - ((long long)x % (long long)MOD1 + (long long)MOD1) % (long long)MOD1;
        if(diff < 0) diff += MOD1;
        static constexpr unsigned long long offset[5] = {0, 0, M1M2M3, 2 * M1M2M3, 3 * M1M2M3};
        x -= offset[diff % 5];
        c[i] = x;
    }
    return c;
}
#line 5 "verify/unit_test/convolution/convolution_ll.test.cpp"
void test() {
    int n = rng(0, 1000), m = rng(0, 1000);
    vector<ll> a(n), b(m);
    rep(i, 0, n) a[i] = rng(-1000000, 1000000);
    rep(i, 0, m) b[i] = rng(-1000000, 1000000);
    vector<ll> c = convolution_ll(a, b);
    if(!n or !m) {
        assert(c.empty());
        return;
    }
    vector<ll> expected(n + m - 1);
    rep(i, 0, n + m - 1) {
        rep(j, 0, i + 1) {
            if(0 <= j and j < n and 0 <= i - j and i - j < m) expected[i] += a[j] * b[i - j];
        }
    }
    rep(i, 0, n + m - 1) {
        assert(c[i] == expected[i]);
    }
}
int main(void) {
    constexpr int test_num = 100;
    rep(_, 0, test_num) {
        test();
    }
    int a, b;
    cin >> a >> b;
    cout << a + b << '\n';
}
Back to top page