結果

問題 No.2156 ぞい文字列
ユーザー Kome_soudouKome_soudou
提出日時 2022-12-09 21:45:55
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 2,293 ms
コンパイル使用メモリ 171,268 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 21:35:54
合計ジャッジ時間 2,702 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 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 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 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 2 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

typedef vector<vector<int64_t>> Matrix;
// 行列の乗算(mod)
Matrix mat_mul(const Matrix &a, const Matrix &b, int64_t mod)
{
	Matrix c(a.size(), vector<int64_t>(b[0].size()));
	for(int i = 0; i < a.size(); i++)
	{
		for(int k = 0; k < b.size(); k++)
		{
			for(int j = 0; j < b[0].size(); j++)
			{
				c[i][j] += a[i][k] * b[k][j];
				c[i][j] %= mod;
			}
		}
	}
	return c;
}
// 行列の累乗(mod)
Matrix mat_pow(Matrix a, int64_t n, int64_t mod)
{
	Matrix b(a.size(), vector<int64_t>(a.size()));
	for(int i = 0; i < a.size(); i++) b[i][i] = 1;
	while(n > 0)
	{
		if(n & 1) b = mat_mul(b, a, mod);
		a = mat_mul(a, a, mod);
		n >>= 1;
	}
	return b;
}

int main()
{
	int64_t n;
	cin >> n;
	const int64_t mod = 998244353;
	
	Matrix a = {{1, 1, 0, 0, 0}, {1, 0, 0, 0, 1}, {0, 0, 1, 1, 0}, {0, 1, 1, 1, 0}, {0, 0, 0, 0, 1}};
	Matrix b = {{0}, {0}, {0}, {0}, {1}};
	Matrix ans = mat_mul(mat_pow(a, n - 1, mod), b, mod);
	cout << (ans[0][0] + ans[1][0]) % mod << endl;
	return 0;
}
0