結果
| 問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2020-04-20 09:56:41 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,219 bytes |
| コンパイル時間 | 1,400 ms |
| コンパイル使用メモリ | 111,812 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-10-06 08:31:09 |
| 合計ジャッジ時間 | 1,694 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 12 |
ソースコード
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define repLRE(i, l, r) for (ll i = (l); i <= (r); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
distance(v.begin(), upper_bound(v.begin(), v.end(), x))
using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;
ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
/* Macros reg. ends here */
const ll INF = 1LL << 50;
// static const long long mod = 1000000007;
ll mod;
vvll matmul(vvll& a, vvll& b) {
ll xx = a.size();
ll zz = a[0].size();
assert(zz == (ll)b.size());
ll yy = b[0].size();
vvll ret(xx, vll(yy));
rep(i, xx) rep(j, yy) {
ll val = 0;
rep(k, zz) {
val += a[i][k] * b[k][j];
ret[i][j] = val % mod;
}
}
return ret;
}
vvll matpow(vvll& m, ll n) { // m^n
ll xx = m.size();
assert(xx == (ll)m[0].size());
vvll ret(xx, vll(xx, 0));
vvll cum = m;
rep(i, xx) ret[i][i] = 1;
if (n == 0) return ret;
while (n >= 2) {
if (n % 2 != 0) {
ret = matmul(cum, ret);
}
cum = matmul(cum, cum);
n /= 2;
}
ret = matmul(ret, cum);
return ret;
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
ll n;
cin >> n >> mod;
vvll fmat = {{1, 1}, {1, 0}};
vvll nmat = matpow(fmat, n-1);
ll ans = nmat[1][0] % mod;
cout << ans << endl;
return 0;
}