結果

問題 No.1112 冥界の音楽
ユーザー snrnsidysnrnsidy
提出日時 2021-06-14 02:08:01
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 159 ms / 2,000 ms
コード長 1,661 bytes
コンパイル時間 2,354 ms
コンパイル使用メモリ 206,020 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-25 10:55:30
合計ジャッジ時間 4,499 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const long long int MOD = 1e9 + 7;
int p[216], q[216], r[216];
long long int k, m, n;

vector <vector<long long int>> mul(vector <vector<long long int>> a, vector<vector<long long int>> b)
{
	int n = a.size();
	int m = a[0].size();
	int k = b[0].size();
	vector <vector<long long int>>c(n, vector<long long int>(k, 0));

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < k; j++)
		{
			for (int k = 0; k < m; k++)
			{
				c[i][j] += ((a[i][k]) * (b[k][j]));
				c[i][j] %= MOD;
			}
		}
	}

	return c;
}

long long int mypow(long long int x, long long int n)
{
	long long int res = 1;
	while (n > 0)
	{
		if (n % 2 == 1)
		{
			res = ((res % MOD) * (x % MOD) % MOD);
			res %= MOD;
		}
		x = ((x % MOD) * (x % MOD) % MOD);
		x %= MOD;
		n /= 2;
	}
	return res;
}
int main(void)
{
	cin.tie(0);
	ios::sync_with_stdio(false);

	cin >> k >> m >> n;
	vector <vector<long long int>> a(m, vector<long long int>(m, 0));
	vector <vector<long long int>> b(m, vector<long long int>(m, 0));

	if (m == k * k * k)
	{
		cout << mypow(k, n - 2) << '\n';
		return 0;
	}
	for (int i = 0; i < m; i++)
	{
		cin >> p[i] >> q[i] >> r[i];
	}

	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j <= m; j++)
		{
			if (q[i] == p[j] && r[i] == q[j])
			{
				//cout << i << ' ' << j << '\n';
				a[i][j] += 1;
			}
		}
		b[i][i] = 1;
	}

	n -= 3;
	while (n > 0)
	{
		if (n % 2 == 1)
		{
			b = mul(a, b);
		}
		a = mul(a, a);
		n /= 2;
	}

	long long int res = 0;

	for (int i = 0; i < m; i++)
	{
		for (int j = 0; j < m; j++)
		{
			if (p[i] == 1 && r[j] == 1) res += b[i][j]; res %= MOD;
		}
	}

	cout << res << '\n';

	return 0;
}
0