結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hogehogejapanhogehogejapan
提出日時 2022-05-31 23:30:48
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,099 bytes
コンパイル時間 898 ms
コンパイル使用メモリ 74,696 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 00:45:32
合計ジャッジ時間 1,734 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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);
	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