結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー TomorrowNextTomorrowNext
提出日時 2021-03-22 02:12:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,190 bytes
コンパイル時間 1,660 ms
コンパイル使用メモリ 171,668 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-14 20:26:10
合計ジャッジ時間 2,243 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;
using ll = long long;

int N, M;

vector<vector<int>> mult(const vector<vector<int>>& m1, const vector<vector<int>>& m2) {
    vector<vector<int>> ret(2, vector<int>(2, 0));
    for (int i = 0; i < 2; ++i) {
        for (int j = 0; j < 2; ++j) {
            for (int k = 0; k < 2; ++k) {
                ret[i][j] += (m1[i][k] * m2[k][j]) % M;
                ret[i][j] %= M;
            }
        }
    }
    return ret;
}

vector<vector<int>> pow(const vector<vector<int>>& m, int n) {
    if (n == 1) {
        return m;
    }
    vector<vector<int>> ret = pow(m, n / 2);
    ret = mult(ret, ret);
    if (n % 2 == 1) {
        ret = mult(ret, m);
    }
    return ret;
}

void Main() {
    cin >> N >> M;
    if (N == 1) {
        cout << 0 << endl;
        return;
    }
    else if (N == 2) {
        cout << 1 << endl;
        return;
    }
    vector<vector<int>> C(2, vector<int>(2, 0));
    C[0][0] = 1;
    C[0][1] = 1;
    C[1][0] = 1;
    vector<vector<int>> Cpow = pow(C, N - 2);
    int ans = Cpow[0][0];
    cout << ans << endl;
}

int main() {
    std::cout << std::fixed << std::setprecision(15);
    Main();
    return 0;
}
0