結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
kumakumaaaaa
|
| 提出日時 | 2020-08-14 07:29:08 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 1,302 bytes |
| コンパイル時間 | 2,000 ms |
| コンパイル使用メモリ | 174,800 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-10-10 11:30:01 |
| 合計ジャッジ時間 | 2,756 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <bits/stdc++.h>
#define INF 2000000000000000000
#define ll long long
using namespace std;
//=============modmatpow============================
vector<vector<ll>> modmatmul(vector<vector<ll>>& a, vector<vector<ll>>& b, ll mod)
{
vector<vector<ll>> res(a.size(), vector<ll>(b.at(0).size()));
for (ll row = 0; row < a.size(); ++row) {
for (ll colom = 0; colom < b.at(0).size(); ++colom) {
ll sum = 0;
for (ll i = 0; i < b.size(); ++i) {
sum = (sum + a.at(row).at(i) * b.at(i).at(colom) % mod) % mod;
}
res.at(row).at(colom) = sum;
}
}
return res;
}
vector<vector<ll>> modmatpow(vector<vector<ll>> a, ll power, ll mod)
{
vector<vector<ll>> res(a.size(), vector<ll>(a.size(), 0));
for (ll i = 0; i < a.size(); ++i) {
res.at(i).at(i) = 1;
}
while (power) {
if (power & 1) {
res = modmatmul(a, res, mod);
}
a = modmatmul(a, a, mod);
power = (power >> 1);
}
return res;
}
//=================================================
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll N, M;
cin >> N >> M;
vector<vector<ll>> a = {{0, 1}, {1, 1}};
a = modmatpow(a, N - 1, M);
vector<vector<ll>> b = {{0}, {1}};
vector<vector<ll>> ans = modmatmul(a, b, M);
cout << ans.at(0).at(0) << "\n";
}
kumakumaaaaa