結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー hornistyfhornistyf
提出日時 2021-02-13 11:49:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 854 bytes
コンパイル時間 1,351 ms
コンパイル使用メモリ 170,168 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 20:11:36
合計ジャッジ時間 2,202 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

//q109.cpp
//Sat Feb 13 11:22:24 2021

#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)

using namespace std;
using ll=long long;
typedef pair<int,int> P;

int main(){
	ll n,m;
	cin >> n >> m;

	vector<ll> fib(2);
	vector<vector<ll>> mt(2,vector<ll>(2));

	fib[0] = 0; fib[1] = 1;
	mt[0][0] = mt[0][1] = mt[1][0] = 1;
	mt[1][1] = 0;

	while (n){
		if (n&1){
			vector<ll> nfib(2);
			rep(i,2){
				nfib[i] = (mt[i][0]*fib[0]%m+mt[i][1]*fib[1]%m)%m;
			}
			rep(i,2)fib[i] = nfib[i];
		}
		vector<vector<ll>> nmt(2,vector<ll>(2));
		rep(i,2)rep(j,2){
			nmt[i][j] = 0;
			rep(k,2){
				nmt[i][j] += mt[i][k]*mt[k][j]%m;
				nmt[i][j] %= m;
			}
		}
		rep(i,2)rep(j,2)mt[i][j] = nmt[i][j];
		n = n/2;
	}

	cout << fib[1] << endl;
//	printf("%.4f\n",ans);
}
0