library

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

View the Project on GitHub masayoshi64/library

:heavy_check_mark: verify/yosupo-convolution_mod_1000000007.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/convolution_mod_1000000007"
#include "library/template/template.cpp"
// library
#include "library/convolution/FFT.cpp"
using mint = modint<1000000007>;
FFT<mint> fft;
int main()
{
    int n, m;
    cin >> n >> m;
    vector<mint> a(n), b(m);
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> b[i];
    auto c = fft.multiply(a, b);
    rep(i, n + m - 1) { cout << c[i] << ' '; }
    cout << endl;
}
#line 1 "verify/yosupo-convolution_mod_1000000007.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/convolution_mod_1000000007"
#line 2 "library/template/template.cpp"
/* #region header */
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
// types
using ll = long long;
using ull = unsigned long long;
using ld = long double;
typedef pair<ll, ll> Pl;
typedef pair<int, int> Pi;
typedef vector<ll> vl;
typedef vector<int> vi;
typedef vector<char> vc;
template <typename T>
using mat = vector<vector<T>>;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<vector<char>> vvc;
// abreviations
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define rep_(i, a_, b_, a, b, ...) for (ll i = (a), max_i = (b); i < max_i; i++)
#define rep(i, ...) rep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define rrep_(i, a_, b_, a, b, ...) \
    for (ll i = (b - 1), min_i = (a); i >= min_i; i--)
#define rrep(i, ...) rrep_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
#define srep(i, a, b, c) for (ll i = (a), max_i = (b); i < max_i; i += c)
#define SZ(x) ((int)(x).size())
#define pb(x) push_back(x)
#define eb(x) emplace_back(x)
#define mp make_pair
//入出力
#define print(x) cout << x << endl
template <class T>
ostream &operator<<(ostream &os, const vector<T> &v)
{
    for (auto &e : v)
        cout << e << " ";
    cout << endl;
    return os;
}
void scan(int &a) { cin >> a; }
void scan(long long &a) { cin >> a; }
void scan(char &a) { cin >> a; }
void scan(double &a) { cin >> a; }
void scan(string &a) { cin >> a; }
template <class T>
void scan(vector<T> &a)
{
    for (auto &i : a)
        scan(i);
}
#define vsum(x) accumulate(all(x), 0LL)
#define vmax(a) *max_element(all(a))
#define vmin(a) *min_element(all(a))
#define lb(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define ub(c, x) distance((c).begin(), upper_bound(all(c), (x)))
// functions
// gcd(0, x) fails.
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }
template <class T>
bool chmax(T &a, const T &b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b)
{
    if (b < a)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <typename T>
T mypow(T x, ll n)
{
    T ret = 1;
    while (n > 0)
    {
        if (n & 1)
            (ret *= x);
        (x *= x);
        n >>= 1;
    }
    return ret;
}
ll modpow(ll x, ll n, const ll mod)
{
    ll ret = 1;
    while (n > 0)
    {
        if (n & 1)
            (ret *= x);
        (x *= x);
        n >>= 1;
        x %= mod;
        ret %= mod;
    }
    return ret;
}
ll safemod(ll x, ll mod) { return (x % mod + mod) % mod; }
int popcnt(ull x) { return __builtin_popcountll(x); }
template <typename T>
vector<int> IOTA(vector<T> a)
{
    int n = a.size();
    vector<int> id(n);
    iota(all(id), 0);
    sort(all(id), [&](int i, int j)
         { return a[i] < a[j]; });
    return id;
}
long long xor64(long long range) {
    static uint64_t x = 88172645463325252ULL;
    x ^= x << 13;
    x ^= x >> 7;
    return (x ^= x << 17) % range;
}
struct Timer
{
    clock_t start_time;
    void start() { start_time = clock(); }
    int lap()
    {
        // return x ms.
        return (clock() - start_time) * 1000 / CLOCKS_PER_SEC;
    }
};
template <typename T = int>
struct Edge
{
    int from, to;
    T cost;
    int idx;

    Edge() = default;

    Edge(int from, int to, T cost = 1, int idx = -1)
        : from(from), to(to), cost(cost), idx(idx) {}

    operator int() const { return to; }
};

template <typename T = int>
struct Graph
{
    vector<vector<Edge<T>>> g;
    int es;

    Graph() = default;

    explicit Graph(int n) : g(n), es(0) {}

    size_t size() const { return g.size(); }

    void add_directed_edge(int from, int to, T cost = 1)
    {
        g[from].emplace_back(from, to, cost, es++);
    }

    void add_edge(int from, int to, T cost = 1)
    {
        g[from].emplace_back(from, to, cost, es);
        g[to].emplace_back(to, from, cost, es++);
    }

    void read(int M, int padding = -1, bool weighted = false,
              bool directed = false)
    {
        for (int i = 0; i < M; i++)
        {
            int a, b;
            cin >> a >> b;
            a += padding;
            b += padding;
            T c = T(1);
            if (weighted)
                cin >> c;
            if (directed)
                add_directed_edge(a, b, c);
            else
                add_edge(a, b, c);
        }
    }
};

/* #endregion*/
// constant
#define inf 1000000000ll
#define INF 4000000004000000000LL
#define endl '\n'
const long double eps = 0.000000000000001;
const long double PI = 3.141592653589793;
#line 3 "verify/yosupo-convolution_mod_1000000007.test.cpp"
// library
#line 2 "library/mod/modint.cpp"
template <int Mod>
struct modint
{
    int x;

    modint() : x(0) {}

    modint(long long y) : x(y >= 0 ? y % Mod : (Mod - (-y) % Mod) % Mod) {}

    modint &operator+=(const modint &p)
    {
        if ((x += p.x) >= Mod)
            x -= Mod;
        return *this;
    }

    modint &operator-=(const modint &p)
    {
        if ((x += Mod - p.x) >= Mod)
            x -= Mod;
        return *this;
    }

    modint &operator*=(const modint &p)
    {
        x = (int)(1LL * x * p.x % Mod);
        return *this;
    }

    modint &operator/=(const modint &p)
    {
        *this *= p.inverse();
        return *this;
    }

    modint operator-() const { return modint(-x); }

    modint operator+(const modint &p) const { return modint(*this) += p; }

    modint operator-(const modint &p) const { return modint(*this) -= p; }

    modint operator*(const modint &p) const { return modint(*this) *= p; }

    modint operator/(const modint &p) const { return modint(*this) /= p; }

    bool operator==(const modint &p) const { return x == p.x; }

    bool operator!=(const modint &p) const { return x != p.x; }

    modint inverse() const
    {
        int a = x, b = Mod, u = 1, v = 0, t;
        while (b > 0)
        {
            t = a / b;
            swap(a -= t * b, b);
            swap(u -= t * v, v);
        }
        return modint(u);
    }

    modint pow(int64_t n) const
    {
        modint ret(1), mul(x);
        while (n > 0)
        {
            if (n & 1)
                ret *= mul;
            mul *= mul;
            n >>= 1;
        }
        return ret;
    }

    friend ostream &operator<<(ostream &os, const modint &p)
    {
        return os << p.x;
    }

    friend istream &operator>>(istream &is, modint &a)
    {
        long long t;
        is >> t;
        a = modint<Mod>(t);
        return (is);
    }

    static int get_mod() { return Mod; }

    constexpr int get() const { return x; }
};
#line 2 "library/convolution/NTT.cpp"
/**
 * @brief Number Theoretic Transform
 * @docs docs/NTT.md
 * @param modint
 */
template <typename Mint>
struct NTT
{
private:
    vector<Mint> root_pow, root_pow_inv;
    int max_base;
    Mint root; //原始根

    void ntt(vector<Mint> &a)
    {
        const int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(__builtin_ctz(n) <= max_base);
        for (int m = n / 2; m >= 1; m >>= 1)
        {
            Mint w = 1;
            for (int s = 0, k = 0; s < n; s += 2 * m)
            {
                for (int i = s, j = s + m; i < s + m; ++i, ++j)
                {
                    auto x = a[i], y = a[j] * w;
                    a[i] = x + y, a[j] = x - y;
                }
                w *= root_pow[__builtin_ctz(++k)];
            }
        }
    }

    void intt(vector<Mint> &a)
    {
        const int n = a.size();
        assert((n & (n - 1)) == 0);
        assert(__builtin_ctz(n) <= max_base);
        for (int m = 1; m < n; m *= 2)
        {
            Mint w = 1;
            for (int s = 0, k = 0; s < n; s += 2 * m)
            {
                for (int i = s, j = s + m; i < s + m; ++i, ++j)
                {
                    auto x = a[i], y = a[j];
                    a[i] = x + y, a[j] = (x - y) * w;
                }
                w *= root_pow_inv[__builtin_ctz(++k)];
            }
        }
    }

public:
    NTT()
    {
        const unsigned Mod = Mint::get_mod();
        auto tmp = Mod - 1;
        max_base = 0;
        while (tmp % 2 == 0)
            tmp >>= 1, max_base++;
        root = 2;
        while (root.pow((Mod - 1) >> 1) == 1)
            root += 1;
        root_pow.resize(max_base);
        root_pow_inv.resize(max_base);
        for (int i = 0; i < max_base; i++)
        {
            root_pow[i] = -root.pow((Mod - 1) >> (i + 2));
            root_pow_inv[i] = Mint(1) / root_pow[i];
        }
    }

    /**
     * @brief 畳み込み
     * @param vector<modint<mod>>
     */
    vector<Mint> multiply(vector<Mint> a, vector<Mint> b)
    {
        const int need = a.size() + b.size() - 1;
        int nbase = 1;
        while ((1 << nbase) < need)
            nbase++;
        int sz = 1 << nbase;
        a.resize(sz, 0);
        b.resize(sz, 0);
        ntt(a);
        ntt(b);
        Mint inv_sz = Mint(1) / sz;
        for (int i = 0; i < sz; i++)
            a[i] *= b[i] * inv_sz;
        intt(a);
        a.resize(need);
        return a;
    }
};
#line 4 "library/convolution/FFT.cpp"
/**
 * @brief Fast Fourier Transform
 * @see https://nyaannyaan.github.io/library/ntt/arbitrary-ntt.hpp
 * @docs docs/FFT.md
 */

template <typename Mint>
struct FFT
{
private:
    using i64 = int64_t;
    static const int32_t m0 = 167772161;
    static const int32_t m1 = 469762049;
    static const int32_t m2 = 754974721;
    using mint0 = modint<m0>;
    using mint1 = modint<m1>;
    using mint2 = modint<m2>;
    const int32_t r01 = 104391568;
    const int32_t r02 = 323560596;
    const int32_t r12 = 399692502;
    const int32_t r02r12 = i64(r02) * r12 % m2;
    const i64 w1 = m0;
    const i64 w2 = i64(m0) * m1;
    template <typename T, typename submint>

    vector<submint> mul(const vector<T> &a, const vector<T> &b)
    {
        static NTT<submint> ntt;
        vector<submint> s(a.size()), t(b.size());
        for (int i = 0; i < (int)a.size(); ++i)
            s[i] = i64(a[i] % submint::get_mod());
        for (int i = 0; i < (int)b.size(); ++i)
            t[i] = i64(b[i] % submint::get_mod());
        return ntt.multiply(s, t);
    }

public:
    FFT()
    {
    }

    /**
     * @brief 任意modによるmodintの畳み込み
     * @arg vector<modint<mod>>
     */
    vector<Mint> multiply(const vector<Mint> &x, const vector<Mint> &y)
    {
        if (x.size() == 0 && y.size() == 0)
            return {};
        if (min<int>(x.size(), y.size()) < 128)
        {
            vector<Mint> ret(x.size() + y.size() - 1);
            for (int i = 0; i < (int)x.size(); ++i)
                for (int j = 0; j < (int)y.size(); ++j)
                    ret[i + j] += x[i] * y[j];
            return ret;
        }
        vector<int> s(x.size()), t(y.size());
        for (int i = 0; i < (int)x.size(); ++i)
            s[i] = x[i].get();
        for (int i = 0; i < (int)y.size(); ++i)
            t[i] = y[i].get();
        auto d0 = mul<int, mint0>(s, t);
        auto d1 = mul<int, mint1>(s, t);
        auto d2 = mul<int, mint2>(s, t);
        int n = d0.size();
        vector<Mint> ret(n);
        const Mint W1 = w1;
        const Mint W2 = w2;
        for (int i = 0; i < n; i++)
        {
            int n1 = d1[i].get(), n2 = d2[i].get(), a = d0[i].get();
            int b = i64(n1 + m1 - a) * r01 % m1;
            int c = (i64(n2 + m2 - a) * r02r12 + i64(m2 - b) * r12) % m2;
            ret[i] = W1 * b + W2 * c + a;
        }
        return ret;
    }

    /**
     * @brief int, long long用の畳み込み
     * @arg vector<long long>を想定
     */
    template <typename T>
    vector<T> multiply_ll(const vector<T> &s, const vector<T> &t)
    {
        if (s.size() == 0 && t.size() == 0)
            return {};
        if (min<int>(s.size(), t.size()) < 128)
        {
            vector<T> ret(s.size() + t.size() - 1);
            for (int i = 0; i < (int)s.size(); ++i)
                for (int j = 0; j < (int)t.size(); ++j)
                    ret[i + j] += i64(s[i]) * t[j];
            return ret;
        }
        auto d0 = mul<T, mint0>(s, t);
        auto d1 = mul<T, mint1>(s, t);
        auto d2 = mul<T, mint2>(s, t);
        int n = d0.size();
        vector<T> ret(n);
        for (int i = 0; i < n; i++)
        {
            i64 n1 = d1[i].get(), n2 = d2[i].get();
            i64 a = d0[i].get();
            T b = (n1 + m1 - a) * r01 % m1;
            T c = ((n2 + m2 - a) * r02r12 + (m2 - b) * r12) % m2;
            ret[i] = a + b * w1 + c * w2;
        }
        return ret;
    }
};
#line 5 "verify/yosupo-convolution_mod_1000000007.test.cpp"
using mint = modint<1000000007>;
FFT<mint> fft;
int main()
{
    int n, m;
    cin >> n >> m;
    vector<mint> a(n), b(m);
    rep(i, n) cin >> a[i];
    rep(i, m) cin >> b[i];
    auto c = fft.multiply(a, b);
    rep(i, n + m - 1) { cout << c[i] << ' '; }
    cout << endl;
}
Back to top page