結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー harogen_cubeharogen_cube
提出日時 2021-08-18 21:36:32
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 986 bytes
コンパイル時間 2,092 ms
コンパイル使用メモリ 194,924 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-20 02:39:25
合計ジャッジ時間 2,370 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

long mod;

struct Matrix {
    int sz;
    long x[2][2];
};

Matrix multiply_matrix(Matrix A, Matrix B){
    Matrix C;
    C.sz = A.sz;
    for(int i = 0 ; i < C.sz; i++){
        fill(C.x[i],C.x[i]+C.sz,0);
    }

    for(int i = 0 ; i < A.sz ; i++){
        for(int j = 0 ; j < B.sz ; j++){
            for(int k = 0 ; k < A.sz ; k++){
                C.x[i][j] += A.x[i][k] * B.x[k][j];
                C.x[i][j] %= mod;
            }
        }
    }
    return C;
}

int main(){
    long n;
    cin >> n;
    n-=2;
    cin >> mod;
    Matrix ANS;
    ANS.sz = 2;
    ANS.x[0][0] = 1; ANS.x[0][1] = 0;
    ANS.x[1][0] = 0; ANS.x[1][1] = 1;
    Matrix A;
    A.sz = 2;
    A.x[0][0] = 0; A.x[0][1] = 1;
    A.x[1][0] = 1; A.x[1][1] = 1;
    for(int i = 0 ; i < 63 ; i++){

        if((n & (1LL << i)) > 0LL){
            ANS = multiply_matrix(ANS,A);
        }
        A = multiply_matrix(A,A);
    }
    cout << ANS.x[1][1] << endl; 

}
0