結果
問題 | No.526 フィボナッチ数列の第N項をMで割った余りを求める |
ユーザー |
![]() |
提出日時 | 2022-05-31 23:34:47 |
言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,101 bytes |
コンパイル時間 | 4,944 ms |
コンパイル使用メモリ | 122,624 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-21 01:26:09 |
合計ジャッジ時間 | 1,440 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#include <iostream> #include <cassert> #include <vector> using ll = long long; using namespace std; using Matrix = vector<vector<ll>>; ll MOD; Matrix mul(const Matrix& a, const Matrix& b) { assert(a[0].size() == b.size()); ll n, m, l; n = a.size(); m = b[0].size(); l = a[0].size(); Matrix c(n, vector<ll>(m, 0)); for (ll i = 0; i < n; i++) { for (ll k = 0; k < l; k++) { for (ll j = 0; j < m; j++) { c[i][j] = (c[i][j] + a[i][k] * b[k][j]) % MOD; } } } return c; } Matrix matpow(Matrix a, ll n) { if (n == 0) { ll s = a.size(); Matrix b(s, vector<ll>(s, 0)); for (ll i = 0; i < s; i++) b[i][i] = 1; return b; } Matrix temp = matpow(a, n/2); if (n & 1) return mul(mul(temp, temp), a); else return mul(temp, temp); } int main() { // ll n = 281621358815590; // MOD = 30524; ll n; cin >> n; cin >> MOD; Matrix a(2, vector<ll>(2, 0)); a[0][0] = 1; a[0][1] = 1; a[1][0] = 1; a[1][1] = 0; a = matpow(a, n-1); Matrix x(2, vector<ll>(1, 0)); x[0][0] = 1; x[1][0] = 0; x = mul(a, x); // cout << x[0][0] << " " << x[1][0] << endl; cout << x[1][0] << endl; }