CppLibrary

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

View the Project on GitHub Tiramister/CppLibrary

:heavy_check_mark: Verify/fixed_matrix_pow.test.cpp

Depends on

Code

#define PROBLEM "https://yukicoder.me/problems/no/1073"

#include "../Number/modint.hpp"
#include "../Number/fixed_matrix.hpp"

#include <iostream>

constexpr int MOD = 1000000007;
using mint = ModInt<MOD>;
using lint = long long;

int main() {
    Matrix<mint, 6> m;
    for (int i = 0; i < 6; ++i) m[0][i] = mint(6).inv();
    for (int i = 1; i < 6; ++i) m[i][i - 1] = 1;

    lint n;
    std::cin >> n;
    std::cout << (m.pow(n))[0][0] << "\n";

    return 0;
}
#line 1 "Verify/fixed_matrix_pow.test.cpp"
#define PROBLEM "https://yukicoder.me/problems/no/1073"

#line 2 "Number/modint.hpp"

#include <iostream>

template <int MOD>
struct ModInt {
    using lint = long long;
    int val;

    // constructor
    ModInt(lint v = 0) : val(v % MOD) {
        if (val < 0) val += MOD;
    };

    // unary operator
    ModInt operator+() const { return ModInt(val); }
    ModInt operator-() const { return ModInt(MOD - val); }

    ModInt& operator++() { return *this += 1; }
    ModInt& operator--() { return *this -= 1; }

    // functions
    ModInt pow(lint n) const {
        auto x = ModInt(1);
        auto b = *this;
        while (n > 0) {
            if (n & 1) x *= b;
            n >>= 1;
            b *= b;
        }
        return x;
    }
    ModInt inv() const {
        int s = val, t = MOD,
            xs = 1, xt = 0;

        while (t != 0) {
            auto div = s / t;

            s -= t * div;
            xs -= xt * div;

            std::swap(s, t);
            std::swap(xs, xt);
        }

        return xs;
    }

    // arithmetic
    ModInt operator+(const ModInt& x) const { return ModInt(*this) += x; }
    ModInt operator-(const ModInt& x) const { return ModInt(*this) -= x; }
    ModInt operator*(const ModInt& x) const { return ModInt(*this) *= x; }
    ModInt operator/(const ModInt& x) const { return ModInt(*this) /= x; }

    ModInt& operator+=(const ModInt& x) {
        if ((val += x.val) >= MOD) val -= MOD;
        return *this;
    }
    ModInt& operator-=(const ModInt& x) {
        if ((val -= x.val) < 0) val += MOD;
        return *this;
    }
    ModInt& operator*=(const ModInt& x) {
        val = lint(val) * x.val % MOD;
        return *this;
    }
    ModInt& operator/=(const ModInt& x) { return *this *= x.inv(); }

    // comparator
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }

    // I/O
    friend std::istream& operator>>(std::istream& is, ModInt& x) {
        lint v;
        is >> v;
        x = v;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) {
        return os << x.val;
    }
};

using modint1000000007 = ModInt<1000000007>;
using modint998244353 = ModInt<998244353>;
#line 2 "Number/fixed_matrix.hpp"

#include <array>

template <class T, int D>
struct Matrix : public std::array<std::array<T, D>, D> {
    // constructor
    using std::array<std::array<T, D>, D>::array;

    Matrix(T val = 0) {
        for (auto& v : *this) v.fill(val);
    }

    static Matrix id() {
        Matrix m;
        for (int i = 0; i < D; ++i) m[i][i] = 1;
        return m;
    }

    // arithmetic
    Matrix operator*(const Matrix& m) const { return Matrix(*this) *= m; }
    Matrix& operator*=(const Matrix& m) {
        Matrix nmat;
        for (auto& v : nmat) v.fill(0);

        for (int i = 0; i < D; ++i) {
            for (int j = 0; j < D; ++j) {
                for (int k = 0; k < D; ++k) {
                    nmat[i][j] += (*this)[i][k] * m[k][j];
                }
            }
        }
        return *this = nmat;
    }

    template <class U>
    Matrix pow(U k) {
        Matrix ret = id();
        Matrix a = *this;

        while (k > 0) {
            if (k & 1) ret *= a;
            a *= a;
            k >>= 1;
        }
        return ret;
    }
};
#line 5 "Verify/fixed_matrix_pow.test.cpp"

#line 7 "Verify/fixed_matrix_pow.test.cpp"

constexpr int MOD = 1000000007;
using mint = ModInt<MOD>;
using lint = long long;

int main() {
    Matrix<mint, 6> m;
    for (int i = 0; i < 6; ++i) m[0][i] = mint(6).inv();
    for (int i = 1; i < 6; ++i) m[i][i - 1] = 1;

    lint n;
    std::cin >> n;
    std::cout << (m.pow(n))[0][0] << "\n";

    return 0;
}
Back to top page