結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2024-11-14 00:51:37 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 39 ms / 2,000 ms |
| コード長 | 1,037 bytes |
| コンパイル時間 | 2,199 ms |
| コンパイル使用メモリ | 201,928 KB |
| 最終ジャッジ日時 | 2025-02-25 04:07:50 |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;
//行列の掛け算
vector<vector<ll>> mat_mul(vector<vector<ll>> &a, vector<vector<ll>> &b, ll mod) {
vector<vector<ll>> res(a.size(), vector<ll>(b[0].size()));
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]) %= mod;
}
}
}
return res;
}
// 行列累乗
vector<vector<ll>> mat_pow(vector<vector<ll>> a, ll k, ll mod) {
int N = a.size();
vector<vector<ll>> res(N, vector<ll>(N));
rep(i,N) res[i][i] = 1;
while (k>0) {
if(k&1) res = mat_mul(a, res, mod);
a = mat_mul(a, a, mod);
k >>= 1;
}
return res;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n,m;
cin>>n>>m;
vector<vector<ll>> a = {{1,1},{1,0}};
a = mat_pow(a, n-1, m);
ll ans = a[1][0];
cout << ans << endl;
}