sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

View the Project on GitHub sotanishy/cp-library-cpp

:heavy_check_mark: test/yosupo/hafnian_of_matrix.test.cpp

Depends on

Code

#define PROBLEM "https://judge.yosupo.jp/problem/hafnian_of_matrix"

#include <bits/stdc++.h>

#include "../../math/linalg/hafnian.hpp"
#include "../../math/modint.hpp"
using namespace std;

using mint = Modint<998244353>;

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

    int N;
    cin >> N;
    vector a(N, vector<mint>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) cin >> a[i][j];
    }
    cout << hafnian<mint, 19>(a) << endl;
}
#line 1 "test/yosupo/hafnian_of_matrix.test.cpp"
#define PROBLEM "https://judge.yosupo.jp/problem/hafnian_of_matrix"

#include <bits/stdc++.h>

#line 4 "math/linalg/hafnian.hpp"

#line 6 "math/set/set_power_series.hpp"

#line 4 "math/set/subset_convolution.hpp"

#line 2 "math/set/zeta_moebius_transform.hpp"
#include <bit>
#line 5 "math/set/zeta_moebius_transform.hpp"

template <typename T>
void superset_fzt(std::vector<T>& a) {
    assert(std::has_single_bit(a.size()));
    const int n = a.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; ++j) {
            if (!(j & i)) a[j] += a[j | i];
        }
    }
}

template <typename T>
void superset_fmt(std::vector<T>& a) {
    assert(std::has_single_bit(a.size()));
    const int n = a.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; ++j) {
            if (!(j & i)) a[j] -= a[j | i];
        }
    }
}

template <typename T>
void subset_fzt(std::vector<T>& a) {
    assert(std::has_single_bit(a.size()));
    const int n = a.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; ++j) {
            if (!(j & i)) a[j | i] += a[j];
        }
    }
}

template <typename T>
void subset_fmt(std::vector<T>& a) {
    assert(std::has_single_bit(a.size()));
    const int n = a.size();
    for (int i = 1; i < n; i <<= 1) {
        for (int j = 0; j < n; ++j) {
            if (!(j & i)) a[j | i] -= a[j];
        }
    }
}
#line 6 "math/set/subset_convolution.hpp"

template <typename T, std::size_t N>
std::array<T, N>& operator+=(std::array<T, N>& lhs,
                             const std::array<T, N>& rhs) {
    for (int i = 0; i < (int)N; ++i) lhs[i] += rhs[i];
    return lhs;
}

template <typename T, std::size_t N>
std::array<T, N>& operator-=(std::array<T, N>& lhs,
                             const std::array<T, N>& rhs) {
    for (int i = 0; i < (int)N; ++i) lhs[i] -= rhs[i];
    return lhs;
}

template <typename T, int N>
std::vector<T> subset_convolution(const std::vector<T>& a,
                                  const std::vector<T>& b) {
    using Poly = std::array<T, N + 1>;
    const int n = std::bit_ceil(std::max(a.size(), b.size()));

    // convert to polynomials
    std::vector<Poly> pa(n), pb(n);
    for (int i = 0; i < (int)a.size(); ++i) {
        pa[i][std::popcount((unsigned int)i)] = a[i];
    }
    for (int i = 0; i < (int)b.size(); ++i) {
        pb[i][std::popcount((unsigned int)i)] = b[i];
    }

    // bitwise or convolution
    subset_fzt(pa);
    subset_fzt(pb);
    for (int i = 0; i < n; ++i) {
        Poly pc;
        for (int j = 0; j <= N; ++j) {
            for (int k = 0; k <= N - j; ++k) {
                pc[j + k] += pa[i][j] * pb[i][k];
            }
        }
        pa[i].swap(pc);
    }
    subset_fmt(pa);

    // convert back
    std::vector<T> ret(n);
    for (int i = 0; i < n; ++i) {
        ret[i] = pa[i][std::popcount((unsigned int)i)];
    }
    return ret;
}
#line 8 "math/set/set_power_series.hpp"

/**
 * @brief Set Power Series
 */

template <typename mint, int N>
class SetPowerSeries : public std::vector<mint> {
    using SPS = SetPowerSeries<mint, N>;
    using Poly = std::array<mint, N + 1>;

   public:
    using std::vector<mint>::vector;
    using std::vector<mint>::operator=;

    // -- binary operation with scalar ---

    SPS& operator+=(const mint& rhs) {
        if (this->empty()) this->resize(1);
        (*this)[0] += rhs;
        return *this;
    }

    SPS& operator-=(const mint& rhs) {
        if (this->empty()) this->resize(1);
        (*this)[0] -= rhs;
        return *this;
    }

    SPS& operator*=(const mint& rhs) {
        for (auto& x : *this) x *= rhs;
        return *this;
    }

    SPS& operator/=(const mint& rhs) { return *this *= rhs.inv(); }

    SPS operator+(const mint& rhs) const { return SPS(*this) += rhs; }
    SPS operator-(const mint& rhs) const { return SPS(*this) -= rhs; }
    SPS operator*(const mint& rhs) const { return SPS(*this) *= rhs; }
    SPS operator/(const mint& rhs) const { return SPS(*this) /= rhs; }

    // --- binary operation with SPS ---

    SPS& operator+=(const SPS& rhs) {
        if (this->size() < rhs.size()) this->resize(rhs.size());
        for (int i = 0; i < (int)rhs.size(); ++i) (*this)[i] += rhs[i];
        return *this;
    }

    SPS& operator-=(const SPS& rhs) {
        if (this->size() < rhs.size()) this->resize(rhs.size());
        for (int i = 0; i < (int)rhs.size(); ++i) (*this)[i] -= rhs[i];
        return *this;
    }

    SPS& operator*=(const SPS& rhs) {
        *this = subset_convolution<mint, N>(*this, rhs);
        return *this;
    }

    SPS operator+(const SPS& rhs) const { return SPS(*this) += rhs; }
    SPS operator-(const SPS& rhs) const { return SPS(*this) -= rhs; }
    SPS operator*(const SPS& rhs) const { return SPS(*this) *= rhs; }

    // --- compositions ---

    SPS exp() const {
        assert((*this)[0] == mint(0));
        const int n = std::bit_width(std::bit_ceil(this->size())) - 1;
        SPS res(1 << n);
        res[0] = 1;
        for (int i = 0; i < n; ++i) {
            SPS a(this->begin() + (1 << i), this->begin() + (1 << (i + 1)));
            SPS b(res.begin(), res.begin() + (1 << i));
            a *= b;
            std::copy(a.begin(), a.end(), res.begin() + (1 << i));
        }
        return res;
    }
};
#line 6 "math/linalg/hafnian.hpp"

template <typename T, int N>
T hafnian(std::vector<std::vector<T>> mat) {
    const int n = mat.size();
    assert(n % 2 == 0);
    const int n2 = n / 2;

    // cyc[S]: number of alternating cycles using all edges in S
    SetPowerSeries<T, N> cyc(1 << n2);

    for (int i = 0; i < n2; ++i) {
        int ui = 2 * i, vi = 2 * i + 1;
        // ui-vi=ui
        cyc[1 << i] += mat[ui][vi];

        // dp[S][v]: number of alternating paths between ui and v
        // using all edges in S
        std::vector dp(1 << i, std::vector<T>(2 * i));
        for (int j = 0; j < i; ++j) {
            int uj = 2 * j, vj = 2 * j + 1;
            dp[1 << j][uj] += mat[ui][vj];  // ui-vj=uj
            dp[1 << j][vj] += mat[ui][uj];  // ui-uj=vj
        }

        for (int S = 0; S < (1 << i); ++S) {
            for (int j = 0; j < i; ++j) {
                int uj = 2 * j, vj = 2 * j + 1;
                cyc[S | (1 << i)] +=
                    dp[S][uj] * mat[vi][uj];  // ui-...=uj-vi=ui
                cyc[S | (1 << i)] +=
                    dp[S][vj] * mat[vi][vj];  // ui-...=vj-vi=ui

                for (int k = 0; k < i; ++k) {
                    if (!(S >> k & 1)) {
                        int uk = 2 * k, vk = 2 * k + 1;
                        int nS = S | (1 << k);

                        dp[nS][uk] +=
                            dp[S][uj] * mat[uj][vk];  // ui-...=uj-vk=uk
                        dp[nS][uk] +=
                            dp[S][vj] * mat[vj][vk];  // ui-...=vj-vk=uk
                        dp[nS][vk] +=
                            dp[S][uj] * mat[uj][uk];  // ui-...=uj-uk=vk
                        dp[nS][vk] +=
                            dp[S][vj] * mat[vj][uk];  // ui-...=vj-uk=vk
                    }
                }
            }
        }
    }
    return cyc.exp().back();
}
#line 4 "math/modint.hpp"

/**
 * @brief Mod int
 */
template <int m>
class Modint {
    using mint = Modint;
    static_assert(m > 0, "Modulus must be positive");

   public:
    static constexpr int mod() { return m; }

    constexpr Modint(long long y = 0) : x(y >= 0 ? y % m : (y % m + m) % m) {}

    constexpr int val() const { return x; }

    constexpr mint& operator+=(const mint& r) {
        if ((x += r.x) >= m) x -= m;
        return *this;
    }
    constexpr mint& operator-=(const mint& r) {
        if ((x += m - r.x) >= m) x -= m;
        return *this;
    }
    constexpr mint& operator*=(const mint& r) {
        x = static_cast<int>(1LL * x * r.x % m);
        return *this;
    }
    constexpr mint& operator/=(const mint& r) { return *this *= r.inv(); }

    constexpr bool operator==(const mint& r) const { return x == r.x; }

    constexpr mint operator+() const { return *this; }
    constexpr mint operator-() const { return mint(-x); }

    constexpr friend mint operator+(const mint& l, const mint& r) {
        return mint(l) += r;
    }
    constexpr friend mint operator-(const mint& l, const mint& r) {
        return mint(l) -= r;
    }
    constexpr friend mint operator*(const mint& l, const mint& r) {
        return mint(l) *= r;
    }
    constexpr friend mint operator/(const mint& l, const mint& r) {
        return mint(l) /= r;
    }

    constexpr mint inv() const {
        int a = x, b = m, u = 1, v = 0;
        while (b > 0) {
            int t = a / b;
            std::swap(a -= t * b, b);
            std::swap(u -= t * v, v);
        }
        return mint(u);
    }

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

    friend std::ostream& operator<<(std::ostream& os, const mint& r) {
        return os << r.x;
    }

    friend std::istream& operator>>(std::istream& is, mint& r) {
        long long t;
        is >> t;
        r = mint(t);
        return is;
    }

   private:
    int x;
};
#line 7 "test/yosupo/hafnian_of_matrix.test.cpp"
using namespace std;

using mint = Modint<998244353>;

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

    int N;
    cin >> N;
    vector a(N, vector<mint>(N));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) cin >> a[i][j];
    }
    cout << hafnian<mint, 19>(a) << endl;
}
Back to top page