結果

問題 No.1112 冥界の音楽
ユーザー 沙耶花沙耶花
提出日時 2020-07-10 21:46:22
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 25 ms / 2,000 ms
コード長 2,433 bytes
コンパイル時間 2,628 ms
コンパイル使用メモリ 208,112 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-19 16:38:24
合計ジャッジ時間 3,979 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define modulo 1000000007
#define mod(mod_x) ((((long long)mod_x+modulo))%modulo)
#define Inf 1000000000

int beki(int a,int b){
	int x = 1;
	while(b!=0){
		if(b&1){
			x=mod(x*a);
		}
		a=mod(a*a);
		b>>=1;
	}
	return x;
}

struct matrix{
	vector<vector<long long>> value;
	
	matrix(int height,int width){
		value.resize(height,vector<long long>(width,0));
		if(height==width){
			for(int i=0;i<get_width();i++){
				value[i][i] = 1;
			}
		}
	}
	
	void resize(int height,int width){
		value.resize(height,vector<long long>(width,0));
		for(int i=0;i<get_width();i++){
			value[i][i] = 1;
		}
	}
	
	void set_value(vector<vector<long long>> &V){
		value = V;
	}
	
	void set_value(initializer_list<long long> list){
		auto it = list.begin();
		for(int i=0;i<get_height();i++){
			for(int j=0;j<get_width();j++){
				value[i][j]=*it;
				if(i!=get_height()-1||j!=get_width()-1)it++;
			}
		}
	}
	
	int get_width(){
		return value[0].size();
	}
	
	int get_height(){
		return value.size();
	}
	
	void print(){
		for(int i=0;i<get_height();i++){
			for(int j=0;j<get_width();j++){
				if(j!=0)cout<<' ';
				cout<<value[i][j];
			}
			cout<<endl;
		}
	}
	
};

matrix matrix_multiple(matrix &A,matrix &B){

	matrix R(A.get_height(),B.get_width());
	int multi_cnt;
	if(A.get_width()!=B.get_height()){
		assert(false);
	}
	else multi_cnt = A.get_width();
	
	for(int i=0;i<R.get_height();i++){
		for(int j=0;j<R.get_width();j++){
			R.value[i][j]=0;
			for(int k=0;k<multi_cnt;k++){
				
				R.value[i][j] = mod(R.value[i][j] + mod(A.value[i][k] * B.value[k][j]));
	
			}
		}
	}
	return R;
	
}

matrix matrix_beki(matrix A,long long cnt){
	if(A.get_width()!=A.get_height())assert(0);
	matrix R(A.get_width(),A.get_width());
	while(cnt!=0){
		if(cnt&1){
			R=matrix_multiple(R,A);
		}
		A=matrix_multiple(A,A);
		cnt>>=1;
	}
	return R;
}

int main(){
	
	int K,MM;
	cin>>K>>MM;
	long long N;
	cin>>N;
	
	vector<vector<long long>> T(K*K,vector<long long>(K*K,0LL));
	for(int i=0;i<MM;i++){
		int p,q,r;
		cin>>p>>q>>r;
		p--,q--,r--;
		T[p*K+q][q*K+r]++;
	}
	
	matrix M(K*K,K*K);
	M.set_value(T);
	M = matrix_beki(M,N-2);
	vector<vector<long long>> X(1,vector<long long>(K*K,0LL));
	for(int i=0;i<K;i++)X[0][i]=1;
	matrix A(1,K*K);
	A.set_value(X);
	A = matrix_multiple(A,M);
	
	int ans = 0;
	for(int i=0;i<K*K;i++){
		if(i%K==0)ans = mod(ans + A.value[0][i]);
	}
	
	cout<<ans<<endl;
	
	return 0;
}
0