結果

問題 No.1112 冥界の音楽
ユーザー milanis48663220milanis48663220
提出日時 2020-07-10 22:11:47
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 2,285 bytes
コンパイル時間 1,008 ms
コンパイル使用メモリ 93,156 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 17:24:14
合計ジャッジ時間 2,107 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;
typedef long long ll;

const ll MOD = 1000000007;

template <typename T>
struct matrix{
    int n, m;
    vector<vector<T>> dat;
    matrix(int n_, int m_){
        n = n_; m = m_;
        for(int i = 0; i < n; i++){
            dat.push_back(vector<T>(m));
        }
    }
};

template <typename T>
bool prod(matrix<T> a, matrix<T> b, matrix<T> &ans){
    if(a.m != b.n) return false;
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            ans.dat[i][j] = 0;
            for(int k = 0; k < b.m; k++){
                ans.dat[i][j] += (a.dat[i][k]*b.dat[k][j])%MOD;
                ans.dat[i][j] %= MOD;
            }
        }
    }
    return true;
}

template <typename T>
void copy_mat(matrix<T> a, matrix<T> &b){
    for(int i = 0; i < a.n; i++){
        for(int j = 0; j < a.m; j++){
            b.dat[i][j] = a.dat[i][j];
        }
    }
}

template <typename T>
void pow(matrix<T> a, ll n, matrix<T> &ans){
    matrix<T> buf(a.n, a.n);
    matrix<T> tmp(a.n, a.n);
    copy_mat(a, tmp);

    for(int i = 0; i < a.n; i++) {
        for(int j = 0; j < a.n; j++){
            ans.dat[i][j] = 0;
        }
        ans.dat[i][i] = 1;
    }
    
    for(int i = 0; i <= 61; i++){
        ll m = (ll)1 << i;
        if(m&n){
            prod(tmp, ans, buf);
            copy_mat(buf, ans);
        }
        prod(tmp, tmp, buf);
        copy_mat(buf, tmp);
    }
}

ll K, M, N;
int P[300], Q[300], R[300];
int idx(int i, int j){
    return i*K+j;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    cin >> K >> M >> N;
    matrix<ll> mat(K*K, K*K), buf(K*K, K*K);
    for(int i = 0; i < M; i++) {
        cin >> P[i] >> Q[i] >> R[i];
        P[i]--; Q[i]--; R[i]--;
        int from = idx(P[i], Q[i]);
        int to = idx(Q[i], R[i]);
        mat.dat[to][from]++;
    }
    ll ans = 0;
    pow<ll>(mat, N-2, buf);
    for(int i = 0; i < K; i++){
        for(int j = 0; j < K; j++){
            int from = idx(0, i);
            int to = idx(j, 0);
            ans += buf.dat[to][from];
            ans %= MOD;
        }
    }
    cout << ans << endl;
}
0