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/lcm_convolution.test.cpp

Depends on

Code

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

#include "../../../lib/convolution/lcm_convolution.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; cin >> n;
    vector<mint> a(n + 1), b(n + 1);
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        cin >> b[i];
    }
    auto c = lcm_convolution(a, b);
    for(int i = 1; i <= n; ++i){
        cout << c[i] << " \n"[i == n];
    }
}
#line 1 "test/library_checker/convolution/lcm_convolution.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/lcm_convolution"
#include <iostream>
#include <vector>

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

/**
 * @brief LCM Convolution
 */

#line 8 "lib/convolution/lcm_convolution.hpp"
#include <cassert>

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

/**
 * @brief Divisor Zeta/Mobius Transform
 */

#line 8 "lib/convolution/divisor_zeta_mobius_transform.hpp"

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

/**
 * @brief Prime Sieve (エラトステネスの篩)
 * @docs docs/math/prime-sieve.md
 */

#line 9 "lib/math/prime_sieve.hpp"

template <typename T>
struct PrimeSieve{
    int n, half;
    std::vector<bool> sieve;
    std::vector<T> prime_list;
    // sieve[i] ... 2 * i + 1

    PrimeSieve(T _n) : n(_n){
        init();
    }

    void init(){
        if(n < 2){
            return;
        }
        half = (n + 1) / 2;
        sieve.assign(half, true);
        sieve[0] = false;
        prime_list.emplace_back(2);
        for(long long i = 1; 2 * i + 1 <= n; ++i){
            if(!sieve[i]) continue;
            T p = 2 * i + 1;
            prime_list.emplace_back(p);
            for(long long j = 2 * i * (i + 1); j < half; j += p){
                sieve[j] = false;
            }
        }
    }

    bool isPrime(T x){
        if(x == 2) return true;
        if(x % 2 == 0) return false;
        return sieve[x / 2];
    }

    T getPrimeCount(){
        return prime_list.size();
    }

    T getKthPrime(int k){
        return prime_list[k];
    }
};
#line 10 "lib/convolution/divisor_zeta_mobius_transform.hpp"

template <typename T>
void divisor_zeta_transform(std::vector<T> &a){
    int n = a.size() - 1;
    PrimeSieve<int> sieve(n);
    for(int d = 2; d <= n; d++){
        if(sieve.isPrime(d)){
            for(int i = 1; i * d <= n; i++){
                a[i * d] += a[i];
            }
        }
    }
}

template <typename T>
void divisor_reversed_zeta_transform(std::vector<T> &a){
    int n = a.size() - 1;
    PrimeSieve<int> sieve(n);
    for(int d = 2; d <= n; d++){
        if(sieve.isPrime(d)){
            for(int i = n / d; i >= 1; i--){
                a[i] += a[i * d];
            }
        }
    }
}

template <typename T>
void divisor_mobius_transform(std::vector<T> &a){
    int n = a.size() - 1;
    PrimeSieve<int> sieve(n);
    for(int d = 2; d <= n; d++){
        if(sieve.isPrime(d)){
            for(int i = n / d; i >= 1; i--){
                a[i * d] -= a[i];
            }
        }
    }
}

template <typename T>
void divisor_reversed_mobius_transform(std::vector<T> &a){
    int n = a.size() - 1;
    PrimeSieve<int> sieve(n);
    for(int d = 2; d <= n; d++){
        if(sieve.isPrime(d)){
            for(int i = 1; i * d <= n; i++){
                a[i] -= a[i * d];
            }
        }
    }
}
#line 11 "lib/convolution/lcm_convolution.hpp"

template <typename T>
std::vector<T> lcm_convolution(std::vector<T> f, std::vector<T> g){
    const int n = (int) f.size();
    assert(f.size() == g.size());
    assert(1 <= n);
    divisor_zeta_transform(f);
    divisor_zeta_transform(g);
    for(int i = 1; i < n; ++i){
        f[i] *= g[i];
    }
    divisor_mobius_transform(f);
    return f;
}
#line 2 "lib/math/modint.hpp"

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

/**
 * @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 7 "test/library_checker/convolution/lcm_convolution.test.cpp"

using namespace std;

using mint = ModInt<998244353>;

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

    int n; cin >> n;
    vector<mint> a(n + 1), b(n + 1);
    for(int i = 1; i <= n; ++i){
        cin >> a[i];
    }
    for(int i = 1; i <= n; ++i){
        cin >> b[i];
    }
    auto c = lcm_convolution(a, b);
    for(int i = 1; i <= n; ++i){
        cout << c[i] << " \n"[i == n];
    }
}
Back to top page