sotanishy's competitive programming library

sotanishy's code snippets for competitive programming

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

:heavy_check_mark: Hafnian
(math/linalg/hafnian.hpp)

Description

偶数次対称行列のハフニアンを求める. $2n \times 2n$ 対称行列 $A$ のハフニアンは次式で定義される. \(\operatorname{haf}(A) = \sum_{\rho \in P_{2n}^2} \prod_{\{i,j\} \in \rho} A_{i,j}\) ここで, $P_{2n}^n$ は ${1,2,\dots,2n}$ の大きさ $2$ の部分集合への分割の全体である.

これは, $A$ を隣接行列として持つ無向グラフの完全マッチングの個数である.

Operations

Reference

Depends on

Verified with

Code

#pragma once
#include <cassert>
#include <vector>

#include "../set/set_power_series.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 2 "math/linalg/hafnian.hpp"
#include <cassert>
#include <vector>

#line 2 "math/set/set_power_series.hpp"
#include <algorithm>
#include <array>
#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();
}
Back to top page