結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー okok
提出日時 2019-06-24 14:41:14
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,259 bytes
コンパイル時間 665 ms
コンパイル使用メモリ 78,492 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-02 08:35:07
合計ジャッジ時間 1,443 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<iomanip>
#include<cmath>
#include<vector>
#include<algorithm>

using namespace std;

#define int long long
#define rep(i,n) for(int i = 0; i < (n); i++)
#define endl "\n"

const long long INF = (long long)1e18;
// const long long MOD = (long long)1e9 + 7; 
long long MOD;

string yn(bool f){return f?"Yes":"No";}
string YN(bool f){return f?"YES":"NO";}

#define MAX

vector<vector<int>> product(vector<vector<int>> &a, vector<vector<int>> &b){
	vector<vector<int>> res(a.size(),vector<int>(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++){
				res[i][j] += a[i][k]*b[k][j];
				res[i][j] %= MOD;
			}
		}
	}
	
	return res;
}

int solve(int n){
	if(n <= 1) return n;
	vector<vector<int>> ans(2,vector<int>(1)), matrix(2,vector<int>(2));
	ans[0][0] = 1;
	ans[1][0] = 0;
	
	matrix[0][0] = matrix[0][1] = matrix[1][0] = 1;
	matrix[1][1] = 0;
	n--;
	
	while(n){
		if(n&1) ans = product(ans,matrix);
		matrix = product(matrix,matrix);
		n >>= 1;
	}
	
	return ans[0][0];
}

signed main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	cout<<fixed<<setprecision(10);
	
	int n, m;
	
	cin>>n>>MOD;
	
	
	cout<<solve(n-1)<<endl;
	
	return 0;
}
0