cplib-cpp

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

View the Project on GitHub hitonanode/cplib-cpp

:heavy_check_mark: linear_algebra_matrix/test/hafnian.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/hafnian_of_matrix"
#include "../hafnian.hpp"
#include "../../modint.hpp"
#include <vector>
using namespace std;

using mint = ModInt<998244353>;

int main() {
    int N;
    cin >> N;
    vector<vector<mint>> A(N, vector<mint>(N));
    for (auto &vec : A) {
        for (auto &x : vec) cin >> x;
    }
    cout << hafnian(A) << '\n';
}
#line 1 "linear_algebra_matrix/test/hafnian.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/hafnian_of_matrix"
#line 2 "set_power_series/subset_convolution.hpp"
#include <algorithm>
#include <cassert>
#include <vector>

// CUT begin
// Subset sum (fast zeta transform)
// Complexity: O(N 2^N) for array of size 2^N
template <typename T> void subset_sum(std::vector<T> &f) {
    const int sz = f.size(), n = __builtin_ctz(sz);
    assert(__builtin_popcount(sz) == 1);
    for (int d = 0; d < n; d++) {
        for (int S = 0; S < 1 << n; S++)
            if (S & (1 << d)) f[S] += f[S ^ (1 << d)];
    }
}
// Inverse of subset sum (fast moebius transform)
// Complexity: O(N 2^N) for array of size 2^N
template <typename T> void subset_sum_inv(std::vector<T> &g) {
    const int sz = g.size(), n = __builtin_ctz(sz);
    assert(__builtin_popcount(sz) == 1);
    for (int d = 0; d < n; d++) {
        for (int S = 0; S < 1 << n; S++)
            if (S & (1 << d)) g[S] -= g[S ^ (1 << d)];
    }
}

// Superset sum / its inverse (fast zeta/moebius transform)
// Complexity: O(N 2^N) for array of size 2^N
template <typename T> void superset_sum(std::vector<T> &f) {
    const int sz = f.size(), n = __builtin_ctz(sz);
    assert(__builtin_popcount(sz) == 1);
    for (int d = 0; d < n; d++) {
        for (int S = 0; S < 1 << n; S++)
            if (!(S & (1 << d))) f[S] += f[S | (1 << d)];
    }
}
template <typename T> void superset_sum_inv(std::vector<T> &g) {
    const int sz = g.size(), n = __builtin_ctz(sz);
    assert(__builtin_popcount(sz) == 1);
    for (int d = 0; d < n; d++) {
        for (int S = 0; S < 1 << n; S++)
            if (!(S & (1 << d))) g[S] -= g[S | (1 << d)];
    }
}

template <typename T> std::vector<std::vector<T>> build_zeta_(int D, const std::vector<T> &f) {
    int n = f.size();
    std::vector<std::vector<T>> ret(D, std::vector<T>(n));
    for (int i = 0; i < n; i++) ret[__builtin_popcount(i)][i] += f[i];
    for (auto &vec : ret) subset_sum(vec);
    return ret;
}

template <typename T>
std::vector<T> get_moebius_of_prod_(const std::vector<std::vector<T>> &mat1,
                                    const std::vector<std::vector<T>> &mat2) {
    int D = mat1.size(), n = mat1[0].size();
    std::vector<std::vector<int>> pc2i(D);
    for (int i = 0; i < n; i++) pc2i[__builtin_popcount(i)].push_back(i);
    std::vector<T> tmp, ret(mat1[0].size());
    for (int d = 0; d < D; d++) {
        tmp.assign(mat1[d].size(), 0);
        for (int e = 0; e <= d; e++) {
            for (int i = 0; i < int(tmp.size()); i++) tmp[i] += mat1[e][i] * mat2[d - e][i];
        }
        subset_sum_inv(tmp);
        for (auto i : pc2i[d]) ret[i] = tmp[i];
    }
    return ret;
};

// Subset convolution
// h[S] = \sum_T f[T] * g[S - T]
// Complexity: O(N^2 2^N) for arrays of size 2^N
template <typename T> std::vector<T> subset_convolution(std::vector<T> f, std::vector<T> g) {
    const int sz = f.size(), m = __builtin_ctz(sz) + 1;
    assert(__builtin_popcount(sz) == 1 and f.size() == g.size());
    auto ff = build_zeta_(m, f), fg = build_zeta_(m, g);
    return get_moebius_of_prod_(ff, fg);
}

// https://hos-lyric.hatenablog.com/entry/2021/01/14/201231
template <class T, class Function> void subset_func(std::vector<T> &f, const Function &func) {
    const int sz = f.size(), m = __builtin_ctz(sz) + 1;
    assert(__builtin_popcount(sz) == 1);

    auto ff = build_zeta_(m, f);

    std::vector<T> p(m);
    for (int i = 0; i < sz; i++) {
        for (int d = 0; d < m; d++) p[d] = ff[d][i];
        func(p);
        for (int d = 0; d < m; d++) ff[d][i] = p[d];
    }

    for (auto &vec : ff) subset_sum_inv(vec);
    for (int i = 0; i < sz; i++) f[i] = ff[__builtin_popcount(i)][i];
}

// log(f(x)) for f(x), f(0) == 1
// Requires inv()
template <class T> void poly_log(std::vector<T> &f) {
    assert(f.at(0) == T(1));
    static std::vector<T> invs{0};
    const int m = f.size();
    std::vector<T> finv(m);
    for (int d = 0; d < m; d++) {
        finv[d] = (d == 0);
        if (int(invs.size()) <= d) invs.push_back(T(d).inv());
        for (int e = 0; e < d; e++) finv[d] -= finv[e] * f[d - e];
    }
    std::vector<T> ret(m);
    for (int d = 1; d < m; d++) {
        for (int e = 0; d + e < m; e++) ret[d + e] += f[d] * d * finv[e] * invs[d + e];
    }
    f = ret;
}

// log(f(S)) for set function f(S), f(0) == 1
// Requires inv()
// Complexity: O(n^2 2^n)
// https://atcoder.jp/contests/abc213/tasks/abc213_g
template <class T> void subset_log(std::vector<T> &f) { subset_func(f, poly_log<T>); }

// exp(f(S)) for set function f(S), f(0) == 0
// Complexity: O(n^2 2^n)
// https://codeforces.com/blog/entry/92183
template <class T> void subset_exp(std::vector<T> &f) {
    const int sz = f.size(), m = __builtin_ctz(sz);
    assert(sz == 1 << m);
    assert(f.at(0) == 0);
    std::vector<T> ret{T(1)};
    ret.reserve(sz);
    for (int d = 0; d < m; d++) {
        auto c = subset_convolution({f.begin() + (1 << d), f.begin() + (1 << (d + 1))}, ret);
        ret.insert(ret.end(), c.begin(), c.end());
    }
    f = ret;
}

// sqrt(f(x)), f(x) == 1
// Requires inv of 2
// Compelxity: O(n^2)
template <class T> void poly_sqrt(std::vector<T> &f) {
    assert(f.at(0) == T(1));
    const int m = f.size();
    static const auto inv2 = T(2).inv();
    for (int d = 1; d < m; d++) {
        if (~(d & 1)) f[d] -= f[d / 2] * f[d / 2];
        f[d] *= inv2;
        for (int e = 1; e < d - e; e++) f[d] -= f[e] * f[d - e];
    }
}

// sqrt(f(S)) for set function f(S), f(0) == 1
// Requires inv()
// https://atcoder.jp/contests/xmascon20/tasks/xmascon20_h
template <class T> void subset_sqrt(std::vector<T> &f) { subset_func(f, poly_sqrt<T>); }

// exp(f(S)) for set function f(S), f(0) == 0
template <class T> void poly_exp(std::vector<T> &P) {
    const int m = P.size();
    assert(m and P[0] == 0);
    std::vector<T> Q(m), logQ(m), Qinv(m);
    Q[0] = Qinv[0] = T(1);
    static std::vector<T> invs{0};

    auto set_invlog = [&](int d) {
        Qinv[d] = 0;
        for (int e = 0; e < d; e++) Qinv[d] -= Qinv[e] * Q[d - e];
        while (d >= int(invs.size())) {
            int sz = invs.size();
            invs.push_back(T(sz).inv());
        }
        logQ[d] = 0;
        for (int e = 1; e <= d; e++) logQ[d] += Q[e] * e * Qinv[d - e];
        logQ[d] *= invs[d];
    };
    for (int d = 1; d < m; d++) {
        Q[d] += P[d] - logQ[d];
        set_invlog(d);
        assert(logQ[d] == P[d]);
        if (d + 1 < m) set_invlog(d + 1);
    }
    P = Q;
}

// f(S)^k for set function f(S)
// Requires inv()
template <class T> void subset_pow(std::vector<T> &f, long long k) {
    auto poly_pow = [&](std::vector<T> &f) {
        const int m = f.size();
        if (k == 0) f[0] = 1, std::fill(f.begin() + 1, f.end(), T(0));
        if (k <= 1) return;
        int nzero = 0;
        while (nzero < int(f.size()) and f[nzero] == T(0)) nzero++;
        int rem = std::max<long long>((long long)f.size() - nzero * k, 0LL);
        if (rem == 0) {
            std::fill(f.begin(), f.end(), 0);
            return;
        }
        f.erase(f.begin(), f.begin() + nzero);
        f.resize(rem);
        const T f0 = f.at(0), f0inv = f0.inv(), f0pow = f0.pow(k);
        for (auto &x : f) x *= f0inv;
        poly_log(f);
        for (auto &x : f) x *= k;
        poly_exp(f);
        for (auto &x : f) x *= f0pow;
        f.resize(rem, 0);
        f.insert(f.begin(), m - int(f.size()), T(0));
    };
    subset_func(f, poly_pow);
}
#line 4 "linear_algebra_matrix/hafnian.hpp"

// Count perfect matchings of undirected graph (Hafnian of the matrix)
// Assumption: mat[i][j] == mat[j][i], mat[i][i] == 0
// Complexity: O(n^2 2^n)
// [1] A. Björklund, "Counting Perfect Matchings as Fast as Ryser,
//     Proc. of 23rd ACM-SIAM symposium on Discrete Algorithms, pp.914-921, 2012.
template <class T> T hafnian(const std::vector<std::vector<T>> &mat) {
    const int N = mat.size();
    if (N % 2) return 0;
    std::vector<std::vector<std::vector<T>>> B(N, std::vector<std::vector<T>>(N));
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) B[i][j] = std::vector<T>{mat[i][j]};
    }

    std::vector<int> pc(1 << (N / 2));
    std::vector<std::vector<int>> pc2i(N / 2 + 1);
    for (int i = 0; i < int(pc.size()); i++) {
        pc[i] = __builtin_popcount(i);
        pc2i[pc[i]].push_back(i);
    }

    std::vector<T> h{1};
    for (int i = 1; i < N / 2; i++) {
        int r1 = N - i * 2, r2 = r1 + 1;
        auto h_add = subset_convolution(h, B[r2][r1]);
        h.insert(h.end(), h_add.begin(), h_add.end());

        std::vector<std::vector<std::vector<T>>> B1(r1), B2(r1);
        for (int j = 0; j < r1; j++) {
            B1[j] = build_zeta_(i, B[r1][j]);
            B2[j] = build_zeta_(i, B[r2][j]);
        }

        for (int j = 0; j < r1; j++) {
            for (int k = 0; k < j; k++) {
                auto Sijk1 = get_moebius_of_prod_(B1[j], B2[k]);
                auto Sijk2 = get_moebius_of_prod_(B1[k], B2[j]);
                for (int s = 0; s < int(Sijk2.size()); s++) Sijk1[s] += Sijk2[s];
                B[j][k].insert(B[j][k].end(), Sijk1.begin(), Sijk1.end());
            }
        }
    }
    T ret = 0;
    for (int i = 0; i < int(h.size()); i++) ret += h[i] * B[1][0][h.size() - 1 - i];
    return ret;
}
#line 3 "modint.hpp"
#include <iostream>
#include <set>
#line 6 "modint.hpp"

template <int md> struct ModInt {
    using lint = long long;
    constexpr static int mod() { return md; }
    static int get_primitive_root() {
        static int primitive_root = 0;
        if (!primitive_root) {
            primitive_root = [&]() {
                std::set<int> fac;
                int v = md - 1;
                for (lint i = 2; i * i <= v; i++)
                    while (v % i == 0) fac.insert(i), v /= i;
                if (v > 1) fac.insert(v);
                for (int g = 1; g < md; g++) {
                    bool ok = true;
                    for (auto i : fac)
                        if (ModInt(g).pow((md - 1) / i) == 1) {
                            ok = false;
                            break;
                        }
                    if (ok) return g;
                }
                return -1;
            }();
        }
        return primitive_root;
    }
    int val_;
    int val() const noexcept { return val_; }
    constexpr ModInt() : val_(0) {}
    constexpr ModInt &_setval(lint v) { return val_ = (v >= md ? v - md : v), *this; }
    constexpr ModInt(lint v) { _setval(v % md + md); }
    constexpr explicit operator bool() const { return val_ != 0; }
    constexpr ModInt operator+(const ModInt &x) const {
        return ModInt()._setval((lint)val_ + x.val_);
    }
    constexpr ModInt operator-(const ModInt &x) const {
        return ModInt()._setval((lint)val_ - x.val_ + md);
    }
    constexpr ModInt operator*(const ModInt &x) const {
        return ModInt()._setval((lint)val_ * x.val_ % md);
    }
    constexpr ModInt operator/(const ModInt &x) const {
        return ModInt()._setval((lint)val_ * x.inv().val() % md);
    }
    constexpr ModInt operator-() const { return ModInt()._setval(md - val_); }
    constexpr ModInt &operator+=(const ModInt &x) { return *this = *this + x; }
    constexpr ModInt &operator-=(const ModInt &x) { return *this = *this - x; }
    constexpr ModInt &operator*=(const ModInt &x) { return *this = *this * x; }
    constexpr ModInt &operator/=(const ModInt &x) { return *this = *this / x; }
    friend constexpr ModInt operator+(lint a, const ModInt &x) { return ModInt(a) + x; }
    friend constexpr ModInt operator-(lint a, const ModInt &x) { return ModInt(a) - x; }
    friend constexpr ModInt operator*(lint a, const ModInt &x) { return ModInt(a) * x; }
    friend constexpr ModInt operator/(lint a, const ModInt &x) { return ModInt(a) / x; }
    constexpr bool operator==(const ModInt &x) const { return val_ == x.val_; }
    constexpr bool operator!=(const ModInt &x) const { return val_ != x.val_; }
    constexpr bool operator<(const ModInt &x) const {
        return val_ < x.val_;
    } // To use std::map<ModInt, T>
    friend std::istream &operator>>(std::istream &is, ModInt &x) {
        lint t;
        return is >> t, x = ModInt(t), is;
    }
    constexpr friend std::ostream &operator<<(std::ostream &os, const ModInt &x) {
        return os << x.val_;
    }

    constexpr ModInt pow(lint n) const {
        ModInt ans = 1, tmp = *this;
        while (n) {
            if (n & 1) ans *= tmp;
            tmp *= tmp, n >>= 1;
        }
        return ans;
    }

    static constexpr int cache_limit = std::min(md, 1 << 21);
    static std::vector<ModInt> facs, facinvs, invs;

    constexpr static void _precalculation(int N) {
        const int l0 = facs.size();
        if (N > md) N = md;
        if (N <= l0) return;
        facs.resize(N), facinvs.resize(N), invs.resize(N);
        for (int i = l0; i < N; i++) facs[i] = facs[i - 1] * i;
        facinvs[N - 1] = facs.back().pow(md - 2);
        for (int i = N - 2; i >= l0; i--) facinvs[i] = facinvs[i + 1] * (i + 1);
        for (int i = N - 1; i >= l0; i--) invs[i] = facinvs[i] * facs[i - 1];
    }

    constexpr ModInt inv() const {
        if (this->val_ < cache_limit) {
            if (facs.empty()) facs = {1}, facinvs = {1}, invs = {0};
            while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
            return invs[this->val_];
        } else {
            return this->pow(md - 2);
        }
    }
    constexpr ModInt fac() const {
        while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
        return facs[this->val_];
    }
    constexpr ModInt facinv() const {
        while (this->val_ >= int(facs.size())) _precalculation(facs.size() * 2);
        return facinvs[this->val_];
    }
    constexpr ModInt doublefac() const {
        lint k = (this->val_ + 1) / 2;
        return (this->val_ & 1) ? ModInt(k * 2).fac() / (ModInt(2).pow(k) * ModInt(k).fac())
                                : ModInt(k).fac() * ModInt(2).pow(k);
    }

    constexpr ModInt nCr(int r) const {
        if (r < 0 or this->val_ < r) return ModInt(0);
        return this->fac() * (*this - r).facinv() * ModInt(r).facinv();
    }

    constexpr ModInt nPr(int r) const {
        if (r < 0 or this->val_ < r) return ModInt(0);
        return this->fac() * (*this - r).facinv();
    }

    static ModInt binom(int n, int r) {
        static long long bruteforce_times = 0;

        if (r < 0 or n < r) return ModInt(0);
        if (n <= bruteforce_times or n < (int)facs.size()) return ModInt(n).nCr(r);

        r = std::min(r, n - r);

        ModInt ret = ModInt(r).facinv();
        for (int i = 0; i < r; ++i) ret *= n - i;
        bruteforce_times += r;

        return ret;
    }

    // Multinomial coefficient, (k_1 + k_2 + ... + k_m)! / (k_1! k_2! ... k_m!)
    // Complexity: O(sum(ks))
    template <class Vec> static ModInt multinomial(const Vec &ks) {
        ModInt ret{1};
        int sum = 0;
        for (int k : ks) {
            assert(k >= 0);
            ret *= ModInt(k).facinv(), sum += k;
        }
        return ret * ModInt(sum).fac();
    }

    // Catalan number, C_n = binom(2n, n) / (n + 1)
    // C_0 = 1, C_1 = 1, C_2 = 2, C_3 = 5, C_4 = 14, ...
    // https://oeis.org/A000108
    // Complexity: O(n)
    static ModInt catalan(int n) {
        if (n < 0) return ModInt(0);
        return ModInt(n * 2).fac() * ModInt(n + 1).facinv() * ModInt(n).facinv();
    }

    ModInt sqrt() const {
        if (val_ == 0) return 0;
        if (md == 2) return val_;
        if (pow((md - 1) / 2) != 1) return 0;
        ModInt b = 1;
        while (b.pow((md - 1) / 2) == 1) b += 1;
        int e = 0, m = md - 1;
        while (m % 2 == 0) m >>= 1, e++;
        ModInt x = pow((m - 1) / 2), y = (*this) * x * x;
        x *= (*this);
        ModInt z = b.pow(m);
        while (y != 1) {
            int j = 0;
            ModInt t = y;
            while (t != 1) j++, t *= t;
            z = z.pow(1LL << (e - j - 1));
            x *= z, z *= z, y *= z;
            e = j;
        }
        return ModInt(std::min(x.val_, md - x.val_));
    }
};
template <int md> std::vector<ModInt<md>> ModInt<md>::facs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::facinvs = {1};
template <int md> std::vector<ModInt<md>> ModInt<md>::invs = {0};

using ModInt998244353 = ModInt<998244353>;
// using mint = ModInt<998244353>;
// using mint = ModInt<1000000007>;
#line 5 "linear_algebra_matrix/test/hafnian.test.cpp"
using namespace std;

using mint = ModInt<998244353>;

int main() {
    int N;
    cin >> N;
    vector<vector<mint>> A(N, vector<mint>(N));
    for (auto &vec : A) {
        for (auto &x : vec) cin >> x;
    }
    cout << hafnian(A) << '\n';
}
Back to top page