hly1204's library

This documentation is automatically generated by competitive-verifier/competitive-verifier

View the Project on GitHub hly1204/library

:heavy_check_mark: test/matrix/yukicoder3231.0.test.cpp

Depends on

Code

// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/3231

#include "frobenius.hpp"
#include "matrix.hpp"
#include "modint.hpp"
#include <iostream>
#include <vector>

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    using mint = ModInt<1000000007>;
    Matrix<mint> A(2, std::vector<mint>(2)), B(2, std::vector<mint>(2));
    std::cin >> A >> B;
    Frobenius<mint> F_A(A), F_B(B);
    if (F_A.frobenius_form() == F_B.frobenius_form()) {
        std::cout << "Yes\n";
    } else {
        std::cout << "No\n";
    }
    return 0;
}
#line 1 "test/matrix/yukicoder3231.0.test.cpp"
// competitive-verifier: PROBLEM https://yukicoder.me/problems/no/3231

#line 2 "frobenius.hpp"

#line 2 "basis.hpp"

#line 2 "matrix.hpp"

#include <algorithm>
#include <cassert>
#include <iostream>
#include <optional>
#include <utility>
#include <vector>

template<typename Tp> class Matrix : public std::vector<std::vector<Tp>> {
public:
    using std::vector<std::vector<Tp>>::vector;

    friend std::istream &operator>>(std::istream &a, Matrix &b) {
        const int w = b.width();
        const int h = b.height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) a >> b[i][j];
        return a;
    }

    friend std::ostream &operator<<(std::ostream &a, const Matrix &b) {
        const int w = b.width();
        const int h = b.height();
        for (int i = 0; i < h; ++i) {
            if (i) a << '\n';
            for (int j = 0; j < w; ++j) {
                if (j) a << ' ';
                a << b[i][j];
            }
        }
        return a;
    }

    int width() const { return this->empty() ? 0 : (*this)[0].size(); }
    int height() const { return this->size(); }
    bool is_square() const { return width() == height(); }

    static Matrix identity(int n) {
        Matrix A(n, std::vector<Tp>(n));
        for (int i = 0; i < n; ++i) A[i][i] = 1;
        return A;
    }

    Matrix transpose() const {
        const int w = width();
        const int h = height();
        Matrix<Tp> TA(w, std::vector<Tp>(h));
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) TA[j][i] = (*this)[i][j];
        return TA;
    }

    std::vector<Tp> apply(const std::vector<Tp> &b) const {
        const int w = width();
        const int h = height();
        assert((int)b.size() == w);
        std::vector<Tp> Ab(h);
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) Ab[i] += (*this)[i][j] * b[j];
        return Ab;
    }

    Matrix operator-() const {
        Matrix A    = *this;
        const int w = A.width();
        const int h = A.height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) A[i][j] = -A[i][j];
        return A;
    }

    Matrix &operator+=(const Matrix<Tp> &B) {
        const int w = width();
        const int h = height();
        assert(w == B.width());
        assert(h == B.height());
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] += B[i][j];
        return *this;
    }

    Matrix &operator-=(const Matrix<Tp> &B) {
        const int w = width();
        const int h = height();
        assert(w == B.width());
        assert(h == B.height());
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] -= B[i][j];
        return *this;
    }

    Matrix operator+(const Matrix<Tp> &B) { return Matrix(*this) += B; }
    Matrix operator-(const Matrix<Tp> &B) { return Matrix(*this) -= B; }
    Matrix operator*(const Tp &b) { return Matrix(*this) *= b; }

    Matrix &operator*=(const Tp &b) {
        const int w = width();
        const int h = height();
        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j) (*this)[i][j] *= b;
        return *this;
    }

    Matrix operator*(const Matrix<Tp> &B) const {
        const int wA = width();
        const int hA = height();
        assert(B.height() == wA);
        const int wB = B.width();
        Matrix<Tp> AB(hA, std::vector<Tp>(wB));
        for (int i = 0; i < hA; ++i)
            for (int k = 0; k < wA; ++k)
                for (int j = 0; j < wB; ++j) AB[i][j] += (*this)[i][k] * B[k][j];
        return AB;
    }

    Matrix &operator*=(const Matrix<Tp> &B) { return *this = (*this) * B; }

    // returns B s.t. B = X*A in reduced row echelon form.
    Matrix gauss(std::vector<int> *pivot_seq = nullptr, Matrix *X = nullptr) const {
        Matrix B    = *this;
        const int m = B.height();
        const int n = B.width();
        if (X) *X = identity(m);
        for (int i = 0, r = -1 /* r = rank-1 */; i < n; ++i) {
            int pivot = r + 1;
            for (; pivot < m; ++pivot)
                if (B[pivot][i] != 0) break;
            if (pivot == m) continue;
            if (pivot != ++r) {
                B[pivot].swap(B[r]);
                if (X) (*X)[pivot].swap((*X)[r]);
            }
            if (pivot_seq) pivot_seq->push_back(i);
            if (B[r][i] != 1) {
                const auto iv = B[r][i].inv();
                for (int j = i; j < n; ++j) B[r][j] *= iv;
                if (X)
                    for (int j = 0; j < m; ++j) (*X)[r][j] *= iv;
            }
            for (int j = 0; j < m; ++j)
                if (j != r) {
                    const auto p = B[j][i];
                    if (p == 0) continue;
                    for (int k = i; k < n; ++k) B[j][k] -= p * B[r][k];
                    if (X)
                        for (int k = 0; k < m; ++k) (*X)[j][k] -= p * (*X)[r][k];
                }
        }
        return B;
    }

    Tp tr() const {
        assert(is_square());
        Tp s;
        for (int i = 0; i < height(); ++i) s += (*this)[i][i];
        return s;
    }

    Tp det() const {
        assert(is_square());
        Matrix A    = *this;
        const int n = A.height();
        Tp d        = 1;
        for (int i = 0; i < n; ++i) {
            int pivot = i;
            for (; pivot < n; ++pivot)
                if (A[pivot][i] != 0) break;
            if (pivot == n) return 0;
            if (pivot != i) {
                A[pivot].swap(A[i]);
                d = -d;
            }
            d *= A[i][i];
            const auto iv = A[i][i].inv();
            for (int j = i + 1; j < n; ++j) {
                const auto p = A[j][i] * iv;
                if (p == 0) continue;
                for (int k = i; k < n; ++k) A[j][k] -= p * A[i][k];
            }
        }
        return d;
    }

    int rank() const {
        std::vector<int> p;
        std::ignore = gauss(&p);
        return (int)p.size();
    }

    std::optional<Matrix<Tp>> inv() const {
        assert(is_square());
        std::vector<int> p;
        Matrix X;
        std::ignore = gauss(&p, &X);
        if ((int)p.size() < height()) return {};
        return X;
    }

    Matrix to_upper_hessenberg() const {
        assert(is_square());
        Matrix A    = *this;
        const int n = A.height();
        for (int i = 0; i < n - 1; ++i) {
            int pivot = i + 1;
            for (; pivot < n; ++pivot)
                if (A[pivot][i] != 0) break;
            if (pivot == n) continue;
            if (pivot != i + 1) {
                A[pivot].swap(A[i + 1]);
                for (int j = 0; j < n; ++j) std::swap(A[j][pivot], A[j][i + 1]);
            }
            const auto iv = A[i + 1][i].inv();
            for (int j = i + 2; j < n; ++j) {
                if (A[j][i] == 0) continue;
                const auto v = A[j][i] * iv;
                for (int k = i; k < n; ++k) A[j][k] -= v * A[i + 1][k];
                for (int k = 0; k < n; ++k) A[k][i + 1] += v * A[k][j];
            }
        }
        return A;
    }

    // returns det(xI - A)
    std::vector<Tp> charpoly() const {
        const auto H = to_upper_hessenberg();
        const int n  = H.height();
        std::vector<std::vector<Tp>> P(n + 1);
        P[0] = {Tp(1)};
        for (int i = 1; i <= n; ++i) {
            P[i].resize(i + 1);
            for (int j = 0; j < i; ++j)
                P[i][j] -= H[i - 1][i - 1] * P[i - 1][j], P[i][j + 1] += P[i - 1][j];
            Tp t = 1;
            for (int j = 1; j < i; ++j) {
                t *= H[i - j][i - j - 1];
                const auto prod = t * H[i - j - 1][i - 1];
                if (prod == 0) continue;
                for (int k = 0; k < i - j; ++k) P[i][k] -= prod * P[i - j - 1][k];
            }
        }
        return P[n];
    }

    Matrix adj() const {
        // We could reduce the constant factor by using a special Gaussian elimination procedure.
        assert(is_square());
        const int n = height();
        std::vector<int> p;
        Matrix L /* inv or basis of left kernel */, B = gauss(&p, &L);
        // A adj(A) = adj(A) A = det(A) I
        if ((int)p.size() == n) return L * det();
        if ((int)p.size() == n - 1) {
            L.erase(L.begin(), L.begin() + (n - 1));
            Matrix R(n, std::vector<Tp>(1)); // basis of right kernel
            int free = 0;                    // free column (column without pivot)
            while (free < (int)p.size() /* in case A = [0] */ && p[free] == free) ++free;
            // Read from rref
            for (int i = 0; i < n; ++i) R[i][0] = i != free ? -B[i][free] : Tp(1);
            // A adj(A) = adj(A) A = 0 => adj(A) = alpha R L
            Matrix RL = R * L;
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < n; ++j)
                    // find alpha by finding any non-zero entry of RL
                    if (RL[i][j] != 0) {
                        Matrix A = *this;
                        for (int k = 0; k < n; ++k) A[k].erase(A[k].begin() + i);
                        A.erase(A.begin() + j);
                        RL *= A.det() / RL[i][j];
                        if ((i + j) & 1) RL = -RL;
                        break;
                    }
            return RL;
        }
        return Matrix(n, std::vector<Tp>(n));
    }

    // randomized algorithm based on Elegia's trick
    // defined in basis.hpp
    inline std::vector<Tp> minpoly() const;

    // To find A^k(i, j) for large k, the O.G.F. of A^[0, inf)(i, j) is rational,
    // so we could apply Berlekamp--Massey's algorithm to e_i^T * A^[0, n*2) * e_j.
};
#line 2 "random.hpp"

#line 2 "rng.hpp"

#include <cstdint>
#include <limits>

// see: https://prng.di.unimi.it/xoshiro256starstar.c
// original license CC0 1.0
class xoshiro256starstar {
    using u64 = std::uint64_t;

    static inline u64 rotl(const u64 x, int k) { return (x << k) | (x >> (64 - k)); }

    u64 s_[4];

    u64 next() {
        const u64 res = rotl(s_[1] * 5, 7) * 9;
        const u64 t   = s_[1] << 17;
        s_[2] ^= s_[0];
        s_[3] ^= s_[1];
        s_[1] ^= s_[2];
        s_[0] ^= s_[3];
        s_[2] ^= t;
        s_[3] = rotl(s_[3], 45);
        return res;
    }

public:
    // see: https://prng.di.unimi.it/splitmix64.c
    // original license CC0 1.0
    explicit xoshiro256starstar(u64 seed) {
        for (int i = 0; i < 4; ++i) {
            u64 z = (seed += 0x9e3779b97f4a7c15);
            z     = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9;
            z     = (z ^ (z >> 27)) * 0x94d049bb133111eb;
            s_[i] = z ^ (z >> 31);
        }
    }
    // see: https://en.cppreference.com/w/cpp/named_req/UniformRandomBitGenerator
    using result_type = u64;
    static constexpr u64 min() { return std::numeric_limits<u64>::min(); }
    static constexpr u64 max() { return std::numeric_limits<u64>::max(); }
    u64 operator()() { return next(); }
};
#line 4 "random.hpp"
#include <random>
#line 6 "random.hpp"

template<typename Tp> inline std::vector<Tp> random_vector(int n) {
    std::vector<Tp> res(n);
    xoshiro256starstar rng(std::random_device{}());
    std::uniform_int_distribution<decltype(Tp::mod())> dis(0, Tp::mod() - 1);
    for (int i = 0; i < n; ++i) res[i] = dis(rng);
    return res;
}

template<typename Tp> inline std::vector<Tp> random_vector_without_zero(int n) {
    std::vector<Tp> res(n);
    xoshiro256starstar rng(std::random_device{}());
    std::uniform_int_distribution<decltype(Tp::mod())> dis(1, Tp::mod() - 1);
    for (int i = 0; i < n; ++i) res[i] = dis(rng);
    return res;
}
#line 8 "basis.hpp"

template<typename Tp> class Basis {
public:
    const int Dim;
    Matrix<Tp> Vectors; // v_0, v_1, ...
    Matrix<Tp> Augmented;
    Matrix<Tp> Reduced; // upper triangular matrix, diagonal of Reduced = (1,...,1)
    // Augmented * Vectors = Reduced

    explicit Basis(int dim) : Dim(dim), Augmented(dim), Reduced(dim) {}

    int size() const { return Vectors.size(); }
    int dim() const { return Dim; }

    // if V is linear combination of v_0, ..., v_(k-1) then
    // returns coefficients (a_0, ..., a_(k-1)) s.t. -(a_0v_0 + ... + a_(k-1)v_(k-1)) = V
    std::optional<std::vector<Tp>> insert(const std::vector<Tp> &V) {
        std::vector<Tp> Aug(dim()), RV = V;
        for (int i = 0; i < dim(); ++i) {
            if (RV[i] == 0) continue;
            if (Reduced[i].empty()) {
                Aug[size()]    = 1;
                const auto inv = RV[i].inv();
                for (int j = i; j < dim(); ++j) RV[j] *= inv;
                for (int j = 0; j < dim(); ++j) Aug[j] *= inv;
                Augmented[i] = Aug, Reduced[i] = RV, Vectors.push_back(V);
                return {};
            }
            const auto v = RV[i];
            for (int j = i; j < dim(); ++j) RV[j] -= v * Reduced[i][j];
            for (int j = 0; j < dim(); ++j) Aug[j] -= v * Augmented[i][j];
        }
        return Aug;
    }

    // returns A s.t. A^(-1)MA is the linear transform respect to the basis
    Matrix<Tp> transition_matrix() const {
        assert(size() == dim());
        return Vectors.transpose();
    }

    // returns A^(-1) s.t. A^(-1)MA is the linear transform respect to the basis
    Matrix<Tp> inv_transition_matrix() const {
        assert(size() == dim());
        auto res = Augmented;
        for (int i = dim() - 1; i > 0; --i)
            for (int j = i - 1; j >= 0; --j)
                for (int k = 0; k < dim(); ++k) res[j][k] -= Reduced[j][i] * res[i][k];
        return res.transpose();
    }
};

template<typename Tp> inline std::vector<Tp> Matrix<Tp>::minpoly() const {
    assert(is_square());
    const int n = height();
    for (;;) {
        Basis<Tp> b(n);
        int deg = 0;
        // If failed, we must get a factor of the minimal polynomial.
        for (auto v = random_vector<Tp>(n);; v = apply(v), ++deg) {
            if (const auto c = b.insert(v)) {
                if (deg == 0) break;
                std::vector<Tp> p(c->begin(), c->begin() + deg);
                p.emplace_back(1);
                return p;
            }
        }
    }
}
#line 7 "frobenius.hpp"
#include <array>
#line 10 "frobenius.hpp"

// Compute the Frobenius form (rational canonical form) of a square matrix,
// but the result is not always correct.
template<typename Tp> class Frobenius {
    struct Poly {
        static int degree(const std::vector<Tp> &a) {
            int n = (int)a.size() - 1;
            while (n >= 0 && a[n] == 0) --n;
            return n;
        }

        static std::array<std::vector<Tp>, 2> divmod(const std::vector<Tp> &A,
                                                     const std::vector<Tp> &B) {
            const int degA = degree(A);
            const int degB = degree(B);
            assert(degB >= 0);
            const int degQ = degA - degB;
            if (degQ < 0) {
                auto R = A;
                R.resize(degB);
                return {std::vector<Tp>{Tp(0)}, std::move(R)};
            }
            std::vector<Tp> Q(degQ + 1), R = A;
            const auto inv = B[degB].inv();
            for (int i = degQ, n = degA; i >= 0; --i)
                if ((Q[i] = R[n--] * inv) != 0)
                    for (int j = 0; j <= degB; ++j) R[i + j] -= Q[i] * B[j];
            R.resize(degB);
            return {std::move(Q), std::move(R)};
        }

        // returns [x^[-deg(Q), 0)] x^k/Q, x^k/Q in F((x^-1))
        static std::vector<Tp> bostan_mori(const std::vector<Tp> &Q, long long k) {
            assert(k >= 0);
            const int degQ = Q.size() - 1;
            std::vector<Tp> U(degQ);
            if (k < degQ || k == 0) {
                if (degQ > 0) U[k] = Q[degQ].inv();
                for (int i = (int)k - 1; i >= 0; --i) {
                    for (int j = (int)k - i; j > 0; --j) U[i] -= Q[degQ - j] * U[i + j];
                    U[i] *= U[k];
                }
                return U;
            }
            std::vector<Tp> V(Q.size());
            for (int i = 0; i < (int)Q.size(); ++i)
                for (int j = i % 2; j < (int)Q.size(); j += 2)
                    V[(i + j) / 2] += Q[i] * (i % 2 == 0 ? Q[j] : -Q[j]);
            const auto T = bostan_mori(V, k / 2);
            for (int i = 0; i < (int)T.size(); ++i)
                for (int j = 0; j < (int)Q.size(); ++j) {
                    const int l = i * 2 + (int)(k % 2) + j;
                    if (l >= degQ && l < degQ * 2)
                        U[l - degQ] += T[i] * (j % 2 == 0 ? Q[j] : -Q[j]);
                }
            return U;
        }

        // returns x^k mod Q
        static std::vector<Tp> xk_mod(long long k, const std::vector<Tp> &Q) {
            const auto invQ = bostan_mori(Q, k);
            std::vector<Tp> R(Q.size() - 1);
            for (int i = 0; i < (int)invQ.size(); ++i)
                for (int j = 0; j < (int)Q.size(); ++j)
                    if (i + j >= (int)invQ.size()) R[i + j - (int)invQ.size()] += invQ[i] * Q[j];
            return R;
        }
    };

public:
    // F_A = T^(-1)AT = diag(C_(p_0),...,C_(p_(k-1)))
    // where C_(p_j) is the companion matrix of monic polynomial P[j]
    // *        minimal polynomial of A = p_0
    // * characteristic polynomial of A = prod_(j=0)^(k-1) p_j
    const int N;
    Matrix<Tp> InvT;
    std::vector<std::vector<Tp>> P; // invariant factors
    Matrix<Tp> T;

    // see:
    // [1]: Elegia. A (Somehow) Simple (Randomized) Algorithm for Frobenius Form of a Matrix.
    //      https://codeforces.com/blog/entry/124815
    // [2]: Arne Storjohann. Algorithms for Matrix Canonical Forms.
    //      https://cs.uwaterloo.ca/~astorjoh/diss2up.pdf
    explicit Frobenius(const Matrix<Tp> &A) : N(A.height()) {
        assert(A.is_square());
        if (A.empty()) {
            P.emplace_back().emplace_back(1);
            return;
        }
    retry: // retry is not guaranteed to give the correct result
        Basis<Tp> B(N);
        Matrix<Tp> A_B(N, std::vector<Tp>(N)); // linear transform respect to basis B
        std::vector<std::vector<Tp>> V;        // vectors for new basis
        P.clear();
        while (B.size() < N) {
            int deg = 0;
            for (auto R = random_vector<Tp>(N);; R = A.apply(R), ++deg)
                if (const auto c = B.insert(R)) {
                    if (deg == 0) break;
                    if (!P.empty() && deg > Poly::degree(P.back())) goto retry;
                    P.emplace_back(c->begin() + (B.size() - deg), c->begin() + B.size())
                        .emplace_back(1);
                    const auto [q, r] = Poly::divmod(
                        std::vector(c->begin(), c->begin() + (B.size() - deg)), P.back());
                    if (Poly::degree(r) >= 0) goto retry;
                    V.emplace_back(q).resize(N), V.back().at(B.size() - deg) = 1;
                    for (int i = B.size() - deg; i < B.size() - 1; ++i) A_B[i + 1][i] = 1;
                    for (int i = 0; i < B.size(); ++i) A_B[i][B.size() - 1] = -c->at(i);
                    break;
                }
        }
        T = B.transition_matrix(), InvT = B.inv_transition_matrix();
        if (P.size() <= 1) return;
        auto C = Matrix<Tp>(N, std::vector<Tp>(N));
        for (int i = 0, j = 0; i < (int)V.size(); ++i) {
            C[j++] = V[i];
            for (int k = (int)P[i].size() - 1; --k; ++j)
                for (int l = 0; l < j; ++l)
                    for (int m = 0; m < j; ++m) C[j][l] += A_B[l][m] * C[j - 1][m];
        }
        for (int i = N - 1; i > 0; --i)
            for (int j = i - 1; j >= 0; --j)
                if (C[i][j] != 0)
                    for (int k = 0; k < N; ++k)
                        T[k][i] += C[i][j] * T[k][j], InvT[j][k] -= C[i][j] * InvT[i][k];
    }

    Matrix<Tp> transition_matrix() const { return T; }
    Matrix<Tp> inv_transition_matrix() const { return InvT; }

    Matrix<Tp> frobenius_form() const {
        Matrix<Tp> res(N, std::vector<Tp>(N));
        for (int i = 0, s = 0; i < (int)P.size(); ++i) {
            const int deg = (int)P[i].size() - 1;
            for (int j = s; j < s + deg - 1; ++j) res[j + 1][j] = 1;
            for (int j = s; j < s + deg; ++j) res[j][s + deg - 1] = -P[i][j - s];
            s += deg;
        }
        return res;
    }

    std::vector<Tp> minpoly() const { return P[0]; }
    std::vector<Tp> charpoly() const {
        std::vector<Tp> p = P[0];
        for (int i = 1; i < (int)P.size(); ++i) {
            p = [](const std::vector<Tp> &A, const std::vector<Tp> &B) {
                std::vector<Tp> AB(A.size() + B.size() - 1);
                for (int i = 0; i < (int)A.size(); ++i)
                    for (int j = 0; j < (int)B.size(); ++j) AB[i + j] += A[i] * B[j];
                return AB;
            }(p, P[i]);
        }
        return p;
    }

    // returns (F_A)^e
    Matrix<Tp> pow(long long e) const {
        assert(e >= 0);
        return eval(Poly::xk_mod(e, P[0]));
    }

    // returns f(F_A)
    Matrix<Tp> eval(std::vector<Tp> f) const {
        Matrix<Tp> res(N, std::vector<Tp>(N));
        for (int i = 0, s = 0; i < (int)P.size(); ++i) {
            const int deg = (int)P[i].size() - 1;
            f             = std::get<1>(Poly::divmod(f, P[i]));
            auto c        = f;
            for (int j = 0; j < deg; ++j) {
                if (j) {
                    std::rotate(c.begin(), c.end() - 1, c.end());
                    const auto c0 = c[0];
                    c[0]          = 0;
                    for (int k = 0; k < deg; ++k) c[k] -= c0 * P[i][k];
                }
                for (int k = 0; k < deg; ++k) res[s + k][s + j] = c[k];
            }
            s += deg;
        }
        return res;
    }
};
#line 2 "modint.hpp"

#line 4 "modint.hpp"
#include <type_traits>

// clang-format off
template<unsigned Mod> class ModInt {
    static_assert((Mod >> 31) == 0, "`Mod` must less than 2^(31)");
    template<typename Int>
    static std::enable_if_t<std::is_integral_v<Int>, unsigned> safe_mod(Int v) { using D = std::common_type_t<Int, unsigned>; return (v %= (int)Mod) < 0 ? (D)(v + (int)Mod) : (D)v; }
    struct PrivateConstructor {} static inline private_constructor{};
    ModInt(PrivateConstructor, unsigned v) : v_(v) {}
    unsigned v_;

public:
    static unsigned mod() { return Mod; }
    static ModInt from_raw(unsigned v) { return ModInt(private_constructor, v); }
    static ModInt zero() { return from_raw(0); }
    static ModInt one() { return from_raw(1); }
    bool is_zero() const { return v_ == 0; }
    bool is_one() const { return v_ == 1; }
    ModInt() : v_() {}
    template<typename Int, typename std::enable_if_t<std::is_signed_v<Int>, int> = 0> ModInt(Int v) : v_(safe_mod(v)) {}
    template<typename Int, typename std::enable_if_t<std::is_unsigned_v<Int>, int> = 0> ModInt(Int v) : v_(v % Mod) {}
    unsigned val() const { return v_; }
    ModInt operator-() const { return from_raw(v_ == 0 ? v_ : Mod - v_); }
    ModInt pow(long long e) const { if (e < 0) return inv().pow(-e); for (ModInt x(*this), res(from_raw(1));; x *= x) { if (e & 1) res *= x; if ((e >>= 1) == 0) return res; }}
    ModInt inv() const { int x1 = 1, x3 = 0, a = val(), b = Mod; while (b) { const int q = a / b, x1_old = x1, a_old = a; x1 = x3, x3 = x1_old - x3 * q, a = b, b = a_old - b * q; } return from_raw(x1 < 0 ? x1 + (int)Mod : x1); }
    template<bool Odd = (Mod & 1)> std::enable_if_t<Odd, ModInt> div_by_2() const { if (v_ & 1) return from_raw((v_ + Mod) >> 1); return from_raw(v_ >> 1); }
    ModInt &operator+=(const ModInt &a) { if ((v_ += a.v_) >= Mod) v_ -= Mod; return *this; }
    ModInt &operator-=(const ModInt &a) { if ((v_ += Mod - a.v_) >= Mod) v_ -= Mod; return *this; }
    ModInt &operator*=(const ModInt &a) { v_ = (unsigned long long)v_ * a.v_ % Mod; return *this; }
    ModInt &operator/=(const ModInt &a) { return *this *= a.inv(); }
    ModInt &operator++() { return *this += one(); }
    ModInt operator++(int) { ModInt o(*this); *this += one(); return o; }
    ModInt &operator--() { return *this -= one(); }
    ModInt operator--(int) { ModInt o(*this); *this -= one(); return o; }
    friend ModInt operator+(const ModInt &a, const ModInt &b) { return ModInt(a) += b; }
    friend ModInt operator-(const ModInt &a, const ModInt &b) { return ModInt(a) -= b; }
    friend ModInt operator*(const ModInt &a, const ModInt &b) { return ModInt(a) *= b; }
    friend ModInt operator/(const ModInt &a, const ModInt &b) { return ModInt(a) /= b; }
    friend bool operator==(const ModInt &a, const ModInt &b) { return a.v_ == b.v_; }
    friend bool operator!=(const ModInt &a, const ModInt &b) { return a.v_ != b.v_; }
    friend std::istream &operator>>(std::istream &a, ModInt &b) { int v; a >> v; b.v_ = safe_mod(v); return a; }
    friend std::ostream &operator<<(std::ostream &a, const ModInt &b) { return a << b.val(); }
};
// clang-format on
#line 8 "test/matrix/yukicoder3231.0.test.cpp"

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    using mint = ModInt<1000000007>;
    Matrix<mint> A(2, std::vector<mint>(2)), B(2, std::vector<mint>(2));
    std::cin >> A >> B;
    Frobenius<mint> F_A(A), F_B(B);
    if (F_A.frobenius_form() == F_B.frobenius_form()) {
        std::cout << "Yes\n";
    } else {
        std::cout << "No\n";
    }
    return 0;
}

Test cases

Env Name Status Elapsed Memory
clang++ 01_sample_01 :heavy_check_mark: AC 10 ms 8 MB
clang++ 01_sample_02 :heavy_check_mark: AC 7 ms 8 MB
clang++ 01_sample_03 :heavy_check_mark: AC 9 ms 8 MB
clang++ 99_system_test1 :heavy_check_mark: AC 7 ms 8 MB
clang++ 99_system_test2 :heavy_check_mark: AC 9 ms 8 MB
clang++ test01 :heavy_check_mark: AC 7 ms 8 MB
clang++ test02 :heavy_check_mark: AC 10 ms 8 MB
clang++ test03 :heavy_check_mark: AC 9 ms 8 MB
clang++ test04 :heavy_check_mark: AC 7 ms 8 MB
clang++ test05 :heavy_check_mark: AC 9 ms 8 MB
clang++ test06 :heavy_check_mark: AC 10 ms 8 MB
clang++ test07 :heavy_check_mark: AC 9 ms 8 MB
clang++ test08 :heavy_check_mark: AC 9 ms 8 MB
clang++ test09 :heavy_check_mark: AC 11 ms 8 MB
clang++ test10 :heavy_check_mark: AC 8 ms 8 MB
clang++ test11 :heavy_check_mark: AC 9 ms 8 MB
clang++ test12 :heavy_check_mark: AC 7 ms 8 MB
clang++ test13 :heavy_check_mark: AC 8 ms 8 MB
clang++ test14 :heavy_check_mark: AC 8 ms 8 MB
clang++ test15 :heavy_check_mark: AC 9 ms 8 MB
clang++ test16 :heavy_check_mark: AC 9 ms 8 MB
clang++ test17 :heavy_check_mark: AC 9 ms 8 MB
clang++ test18 :heavy_check_mark: AC 9 ms 8 MB
clang++ test19 :heavy_check_mark: AC 10 ms 8 MB
clang++ test20 :heavy_check_mark: AC 9 ms 8 MB
clang++ test21 :heavy_check_mark: AC 8 ms 8 MB
clang++ test22 :heavy_check_mark: AC 11 ms 8 MB
clang++ test23 :heavy_check_mark: AC 8 ms 8 MB
clang++ test24 :heavy_check_mark: AC 11 ms 8 MB
clang++ test25 :heavy_check_mark: AC 9 ms 8 MB
clang++ test26 :heavy_check_mark: AC 7 ms 8 MB
clang++ test27 :heavy_check_mark: AC 9 ms 8 MB
clang++ test28 :heavy_check_mark: AC 7 ms 8 MB
clang++ test29 :heavy_check_mark: AC 9 ms 8 MB
clang++ test30 :heavy_check_mark: AC 9 ms 8 MB
clang++ test31 :heavy_check_mark: AC 9 ms 8 MB
clang++ test32 :heavy_check_mark: AC 7 ms 8 MB
clang++ test33 :heavy_check_mark: AC 9 ms 8 MB
clang++ test34 :heavy_check_mark: AC 10 ms 8 MB
clang++ test35 :heavy_check_mark: AC 7 ms 8 MB
clang++ test36 :heavy_check_mark: AC 8 ms 8 MB
clang++ test37 :heavy_check_mark: AC 7 ms 8 MB
Back to top page