結果
問題 |
No.526 フィボナッチ数列の第N項をMで割った余りを求める
|
ユーザー |
![]() |
提出日時 | 2020-09-25 15:16:38 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 2 ms / 2,000 ms |
コード長 | 1,009 bytes |
コンパイル時間 | 2,382 ms |
コンパイル使用メモリ | 232,900 KB |
最終ジャッジ日時 | 2025-01-14 20:30:03 |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 12 |
ソースコード
#define _GLIBCXX_DEBUG // test only #include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; long long m; cin >> n >> m; vector<vector<long long>> mat(2,vector<long long>(2,1)); mat[0][1] = 0; mat[1][0] = 0; vector<vector<long long>> ks(2,vector<long long>(2,1)); ks[1][1] = 0; int left = n-2; while (left > 0) { if (left%2 == 1) { vector<vector<long long>> nmat(2,vector<long long>(2,0)); for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) { nmat[i][j] += mat[i][k]*ks[k][j]; nmat[i][j] %= m; } } } mat = nmat; } left /= 2; vector<vector<long long>> nks(2,vector<long long>(2,0)); for (int i=0;i<2;i++) { for (int j=0;j<2;j++) { for (int k=0;k<2;k++) { nks[i][j] += ks[i][k]*ks[k][j]; nks[i][j] %= m; } } } ks = nks; } cout << mat[0][0] << endl; }