結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー ponta123ponta123
提出日時 2022-10-30 23:04:38
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,143 bytes
コンパイル時間 1,578 ms
コンパイル使用メモリ 172,664 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 20:50:18
合計ジャッジ時間 2,491 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define chmin(a, b) a = min(a, b)
#define chmax(a, b) a = max(a, b)
#define all(vec) vec.begin(), vec.end()
using ll = long long;
#define fill1(arr, num) fill(arr, arr + sizeof(arr) / sizeof(*arr), num);
#define fill2(arr, num) fill(*arr, *arr + sizeof(arr) / sizeof(**arr), num);
#define fill3(arr, num) fill(**arr, **arr + sizeof(arr) / sizeof(***arr), num);
#define MOD 1000000007

vector<vector<ll>> matmul(vector<vector<ll>> mat1, vector<vector<ll>> mat2, ll M) {
    vector<vector<ll>> res(2, vector<ll>(2));
    rep(i, 2) {
        rep(j, 2) {
            res[i][j] = (mat1[i][0] * mat2[0][j] + mat1[i][1] * mat2[1][j]) % M;
        }
    }
    return res;
}

int main() {
    ll N, M;
    cin >> N >> M;
    vector<vector<ll>> mat = {{1, 1}, {1, 0}}, nmat = {{1, 0}, {0, 1}};
    vector<ll> init_vec = {1, 1};
    int cnt = 0;
    
    while ((1 << cnt) <= N) {
        if (N & (1 << cnt)) {
            nmat = matmul(nmat, mat, M);
        }
        mat = matmul(mat, mat, M);
        cnt += 1;
    }
    cout << nmat[1][1] << endl;
}
0