結果

問題 No.1112 冥界の音楽
ユーザー MisterMister
提出日時 2020-08-06 18:09:07
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 6,034 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 83,948 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 14:02:14
合計ジャッジ時間 2,545 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,348 KB
testcase_06 AC 10 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 3 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 5 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 5 ms
4,348 KB
testcase_14 AC 46 ms
4,348 KB
testcase_15 AC 12 ms
4,348 KB
testcase_16 AC 1 ms
4,348 KB
testcase_17 AC 15 ms
4,348 KB
testcase_18 AC 15 ms
4,348 KB
testcase_19 AC 1 ms
4,348 KB
testcase_20 AC 5 ms
4,348 KB
testcase_21 AC 42 ms
4,348 KB
testcase_22 AC 43 ms
4,348 KB
testcase_23 AC 12 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 1 ms
4,348 KB
testcase_28 AC 4 ms
4,348 KB
testcase_29 AC 2 ms
4,348 KB
testcase_30 AC 45 ms
4,348 KB
testcase_31 AC 4 ms
4,348 KB
testcase_32 AC 49 ms
4,348 KB
testcase_33 AC 2 ms
4,348 KB
testcase_34 AC 2 ms
4,348 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 172 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

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 inv() const { return this->pow(MOD - 2); }

    // 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 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;
    }

    // compound assignment
    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(); }

    // compare
    bool operator==(const ModInt& b) const { return val == b.val; }
    bool operator!=(const ModInt& b) const { return val != b.val; }
    bool operator<(const ModInt& b) const { return val < b.val; }
    bool operator<=(const ModInt& b) const { return val <= b.val; }
    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) noexcept {
        lint v;
        is >> v;
        x = v;
        return is;
    }
    friend std::ostream& operator<<(std::ostream& os, const ModInt& x) noexcept { return os << x.val; }
};

template <class T>
struct Vector {
    using V = std::vector<T>;

    int d;
    V vec;

    // constructor
    Vector() = default;
    Vector(int d, T val = 0) : d(d), vec(d, val) {}

    // getter
    T& operator[](int i) { return vec[i]; }
    T operator[](int i) const { return vec[i]; }
    typename V::iterator begin() { return vec.begin(); }
    typename V::iterator end() { return vec.end(); }

    // arithmetic
    Vector operator+(const Vector& v) const { return Matrix(*this) += v; }
    Vector operator-(const Vector& v) const { return Matrix(*this) -= v; }
    T operator*(const Vector& v) const {
        T ret(0);
        for (int i = 0; i < d; ++i) ret += vec[i] * v[i];
        return ret;
    }

    // compound assignment
    Vector& operator+=(const Vector& v) {
        for (int i = 0; i < d; ++i) vec[i] += v[i];
        return *this;
    }
    Vector& operator-=(const Vector& v) {
        for (int i = 0; i < d; ++i) vec[i] -= v[i];
        return *this;
    }
};

template <class T>
struct Matrix {
    using M = std::vector<std::vector<T>>;

    int h, w;
    M mat;

    // constructor
    Matrix() = default;
    Matrix(int h, int w, T val = 0)
        : h(h), w(w), mat(h, std::vector<T>(w, val)) {}

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

    // getter
    std::vector<T>& operator[](int i) { return mat[i]; }
    std::vector<T> operator[](int i) const { return mat[i]; }
    typename M::iterator begin() { return mat.begin(); }
    typename M::iterator end() { return mat.end(); }

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

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

        while (k > 0) {
            if (k & 1) ret *= a;
            a *= a;
            k >>= 1;
        }
        return ret;
    }

    // compound assignment
    Matrix& operator+=(const Matrix& m) {
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                mat[i][j] += m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator-=(const Matrix& m) {
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                mat[i][j] -= m[i][j];
            }
        }
        return *this;
    }
    Matrix& operator*=(const Matrix& m) {
        std::vector<std::vector<T>> nmat(h, std::vector<T>(m.w, T(0)));
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < m.w; ++j) {
                for (int k = 0; k < w; ++k) {
                    nmat[i][j] += mat[i][k] * m[k][j];
                }
            }
        }
        mat = nmat;
        return *this;
    }

    // arithmetic with vector
    using Vec = Vector<T>;
    Vec operator*(const Vec& v) {
        Vec ret(h, 0);
        for (int i = 0; i < h; ++i) {
            for (int j = 0; j < w; ++j) {
                ret[i] += mat[i][j] * v[j];
            }
        }
        return ret;
    }
};

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

using Vec = Vector<mint>;
using Mat = Matrix<mint>;

using lint = long long;

void solve() {
    int k, m;
    lint n;
    std::cin >> k >> m >> n;

    auto encode = [&](int p, int q) { return p * k + q; };

    Mat mat(k * k, k * k, 0);
    while (m--) {
        int p, q, r;
        std::cin >> p >> q >> r;
        --p, --q, --r;
        mat[encode(q, r)][encode(p, q)] = 1;
    }

    Vec init(k * k, 0), last(k * k, 0);
    for (int i = 0; i < k; ++i) {
        init[encode(0, i)] = 1;
        last[encode(i, 0)] = 1;
    }

    std::cout << (mat.pow(n - 2) * init * last) << "\n";
}

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

    solve();

    return 0;
}
0