結果

問題 No.1112 冥界の音楽
ユーザー pockynypockyny
提出日時 2021-12-18 06:16:54
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 63 ms / 2,000 ms
コード長 1,784 bytes
コンパイル時間 1,009 ms
コンパイル使用メモリ 81,868 KB
実行使用メモリ 4,372 KB
最終ジャッジ日時 2023-10-13 10:16:46
合計ジャッジ時間 2,685 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<vector<ll>> vvl;
ll mod = 1000000007;
vvl mul(const vvl &a,const vvl &b){
    int i,j,k,n = a.size();
	vvl c(n,vector<ll>(n,0));
	for(i=0;i<n;i++){
		for(j=0;j<n;j++){
			for(k=0;k<n;k++) (c[i][j] += a[i][k]*b[k][j]%mod) %= mod;
		}
	}
	return c;
}

vector<ll> mul2(vvl &a,vector<ll> b){
    int i,j,n = a.size();
    vector<ll> c(n);
    for(i=0;i<n;i++){
        for(j=0;j<a[i].size();j++) (c[i] += a[i][j]*b[j]%mod) %= mod;
    }
	return c;
}

vvl mat_pw(vvl &a, ll x){
    int i,j,k,n = a.size();
	vvl ret(n,vector<ll>(n,0));
	for(i=0;i<n;i++) ret[i][i] = 1;
	while(x){
        if(x&1) ret = mul(ret,a);
		a = mul(a,a); x /= 2;
	}
	return ret;
}

vector<ll> mat_pw2(vvl &a, ll x, vector<ll> v){
    int i,j,k,n = a.size();
	while(x){
        if(x&1) v = mul2(a,v);
		a = mul(a,a); x /= 2;
	}
	return v;
}

bool ok[6][6][6];
int main(){
    ll i,j,l,r,k,m,n; cin >> k >> m >> n;
    for(i=0;i<k;i++){
        for(j=0;j<k;j++){
            for(l=0;l<k;l++) ok[i][j][l] = false; 
        }
    }
    for(i=0;i<m;i++){
        int p,q,r; cin >> p >> q >> r; p--; q--; r--;
        ok[p][q][r] = true;
    }
    int sz = k*k;
    vvl mat(sz);
    for(i=0;i<sz;i++) mat[i].resize(sz);
    for(i=0;i<k;i++){
        for(j=0;j<k;j++){
            for(l=0;l<k;l++){
                if(!ok[i][j][l]) continue;
                int pos1 = k*i + j;
                int pos2 = k*j + l;
                mat[pos2][pos1] = 1;
            }
        }
    }
    vector<ll> v(sz);
    for(i=0;i<1;i++){
        for(j=0;j<k;j++) v[k*i + j] = 1;
    }
    v = mat_pw2(mat,n - 2,v);
    ll ans = 0;
    for(i=0;i<k;i++){
        for(j=0;j<1;j++) (ans += v[k*i + j]) %= mod;
    }
    cout << ans << endl;
}
0