結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー iryukiiryuki
提出日時 2020-12-22 15:39:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,833 bytes
コンパイル時間 1,196 ms
コンパイル使用メモリ 100,188 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 12:58:24
合計ジャッジ時間 2,221 ms
ジャッジサーバーID
(参考情報)
judge11 / judge10
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <math.h>
#include <cmath>
#include <string>
#include <string.h>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
#include <cstdint>
#include <functional>
#include <time.h>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
#define rep(i, N) for (int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000
#define MAX 200001
#define PI 3.141592653589793
const ll MOD = 1000000007;

/*clock_t start = clock();
clock_t end = clock();
const double time = static_cast<double>(end - start) / CLOCKS_PER_SEC * 1000.0;
cout << time << endl;*/

template <typename T > inline string toString(const T &a) {ostringstream oss; oss << a; return oss.str();};

vector<vector<ll>> mul(vector<vector<ll>> &A, vector<vector<ll>> &B,ll mod){
	vector<vector<ll>> C(A.size(),vector<ll>(B[0].size()));
	for(int i = 0; i < A.size(); i++){
		for(int k = 0; k < B.size(); k++){
			for(int j = 0; j < B[0].size(); j++){
				ll t = (A[i][k]*B[k][j])%mod;
				C[i][j] = (C[i][j]+t)%mod;
				C[i][j] = (C[i][j]+mod)%mod;
			}
		}
	}
	return C;
}

vector<vector<ll>> Pow(vector<vector<ll>> &A,ll n,ll mod){
	vector<vector<ll>> B(A.size(),vector<ll>(A.size()));
	for(int i = 0; i < A.size(); i++) B[i][i] = 1LL;
	while(n > 0){
		if(n&1) B = mul(B,A,mod);
		A = mul(A,A,mod);
		n>>=1;
	}
	return B;
}

int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	ll N,M;
	cin >> N >> M;
	vector<vector<ll>> A(2,vector<ll>(2));
	vector<vector<ll>> C(2,vector<ll>(1));
	A[0][0] = 1;
	A[0][1] = 1;
	A[1][0] = 1;
	A[1][1] = 0;
	C[0][0] = 1;
	C[1][0] = 0;
	vector<vector<ll>> B = Pow(A,N-2,M);
	vector<vector<ll>> ans = mul(B,C,M);
	cout << ans[0][0] << endl;
}
0