結果

問題 No.793 うし数列 2
ユーザー ldsybldsyb
提出日時 2019-03-02 01:15:54
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 950 bytes
コンパイル時間 2,416 ms
コンパイル使用メモリ 171,424 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-05 16:45:28
合計ジャッジ時間 2,686 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 1 ms
4,380 KB
testcase_16 AC 1 ms
4,380 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using vec = vector<int64_t>;
using mat = vector<vec>;

int main()
{
	int64_t n, mod = 1000000007;
	cin >> n;

	auto mul = [](mat &a, mat &b, int64_t mod) -> mat {
		mat c(a.size(), vec(b[0].size(), 0));
		for (int i = 0; i < a.size(); i++)
		{
			for (int j = 0; j < b[0].size(); j++)
			{
				for (int k = 0; k < b.size(); k++)
				{
					(c[i][j] += a[i][k] * b[k][j] % mod) %= mod;
				}
			}
		}
		return c;
	};
	auto pow = [](mat &a, int64_t n, int64_t mod, auto &mul) -> mat {
		int m = a.size();
		mat r(m, vec(m, 0));
		for (int i = 0; i < m; i++)
		{
			r[i][i] = 1;
		}
		for (; n; n >>= 1)
		{
			if (n & 1)
			{
				r = mul(r, a, mod);
			}
			a = mul(a, a, mod);
		}
		return r;
	};

	mat a(3, vec(3)), r(3, vec(1));
	a[0][0] = 10;
	a[0][2] = 3;
	a[1][0] = 1;
	a[2][2] = 1;
	r[0][0] = 1;
	r[2][0] = 1;

	a = pow(a, n, mod, mul);
	r = mul(a, r, mod);

	cout << r[0][0] << endl;

	return 0;
}
0