結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hogehogejapanhogehogejapan
提出日時 2022-05-31 23:34:47
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,101 bytes
コンパイル時間 1,152 ms
コンパイル使用メモリ 112,908 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 00:45:34
合計ジャッジ時間 1,911 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
using ll = long long;
using namespace std;
using Matrix = vector<vector<ll>>;
ll MOD;
Matrix mul(const Matrix& a, const Matrix& b) {
	assert(a[0].size() == b.size());
	ll n, m, l;
	n = a.size();
	m = b[0].size();
	l = a[0].size();
	Matrix c(n, vector<ll>(m, 0));
	for (ll i = 0; i < n; i++) {
		for (ll k = 0; k < l; k++) {
			for (ll j = 0; j < m; j++) {
				c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD;
			}
		}
	}
	return c;
}
Matrix matpow(Matrix a, ll n) {
	if (n == 0) {
		ll s = a.size();
		Matrix b(s, vector<ll>(s, 0));
		for (ll i = 0; i < s; i++) b[i][i] = 1;
		return b;
	}
	Matrix temp = matpow(a, n/2);
	if (n & 1) return mul(mul(temp, temp), a);
	else return mul(temp, temp);
}

int main() {
	// ll n = 281621358815590;
	// MOD = 30524;
	ll n; cin >> n;
	cin >> MOD;
	Matrix a(2, vector<ll>(2, 0));
	a[0][0] = 1;
	a[0][1] = 1;
	a[1][0] = 1;
	a[1][1] = 0;
	a = matpow(a, n-1);
	Matrix x(2, vector<ll>(1, 0));
	x[0][0] = 1;
	x[1][0] = 0;
	x = mul(a, x);
	// cout << x[0][0] << " " << x[1][0] << endl;
	cout << x[1][0] << endl;
}
0