結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ponta123ponta123
提出日時 2022-07-27 13:45:40
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 923 bytes
コンパイル時間 2,241 ms
コンパイル使用メモリ 174,656 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-23 19:26:51
合計ジャッジ時間 3,248 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 WA -
testcase_14 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define mat vector<vector<long long>>

mat matmul(mat &a, mat &b, long long M) {
    mat res(a.size(), (vector<long long>)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] % M;
            }
        }
    }
    return res;
}

mat matpow(mat &a, long long n, long long M) {
    mat res(a.size(), (vector<long long>)a.size());
    for (int i = 0; i < a.size(); i++) {
        res[i][i] = 1;
    }
    while (n > 0) {
        if (n & 1) res = matmul(a, res, M);
        a = matmul(a, a, M);
        n >>= 1;
    }
    return res;
}

int main() {
    long N, M;
    cin >> N >> M;
    mat res(2, (vector<long long>)2);
    res = {{1, 1}, {1, 0}};
    res = matpow(res, N - 3, M);
    cout << (res[0][0] + res[0][1]) % M << endl;
}
0