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

Depends on

Code

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

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

using namespace std;

const long long MOD = 998244353;
using mint = ModInt<MOD>;

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

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

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

#include <vector>
#include <array>
#line 6 "lib/convolution/subset_convolution.hpp"

template <typename T, int MAX_RANK>
std::vector<std::array<T, MAX_RANK>> ranked_zeta_transform(const std::vector<T> &f){
    int N = f.size();
    std::vector<std::array<T, MAX_RANK>> fr(N);
    for(int i = 0; i < N; i++) fr[i][__builtin_popcount(i)] = f[i];
    for(int k = 1; k < N; k <<= 1){
        for(int i = k; i < N; i = (i + 1) | k){
            for(int p = 0; p < MAX_RANK; p++){
                fr[i][p] += fr[i ^ k][p];
            }
        }
    }
    return fr;
}

template <typename T, int MAX_RANK>
std::vector<T> ranked_mobius_transform(std::vector<std::array<T, MAX_RANK>> fr) {
    int N = fr.size();
    for(int k = 1; k < N; k <<= 1){
        for(int i = k; i < N; i = (i + 1) | k){
            for(int p = 0; p < MAX_RANK; p++){
                fr[i][p] -= fr[i ^ k][p];
            }
        }
    }
    std::vector<T> f(N);
    for(int i = 0; i < N; i++) f[i] = fr[i][__builtin_popcount(i)];
    return f;
}

template <typename T, int MAX_RANK>
std::vector<T> subset_convolution(const std::vector<T> &f, const std::vector<T> &g){
    const int N = f.size(), n = __builtin_ctz(f.size());
    assert(f.size() == g.size());
    assert((N & (N - 1)) == 0);
    assert(MAX_RANK >= n);
    auto fr = ranked_zeta_transform<T, MAX_RANK + 1>(f);
    auto gr = ranked_zeta_transform<T, MAX_RANK + 1>(g);
    for(int i = 0; i < N; i++){
        for(int p = MAX_RANK; p >= 0; p--){
            for(int q = MAX_RANK - p; q > 0; q--){
                fr[i][p + q] += fr[i][p] * gr[i][q];
            }
            fr[i][p] *= gr[i][0];
        }
    }
    return ranked_mobius_transform<T, MAX_RANK + 1>(std::move(fr));
}
#line 6 "test/library_checker/set_power_series/subset_convolution.test.cpp"

using namespace std;

const long long MOD = 998244353;
using mint = ModInt<MOD>;

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

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