結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー IlagIlag
提出日時 2020-09-09 00:39:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,066 bytes
コンパイル時間 1,699 ms
コンパイル使用メモリ 171,360 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-20 13:18:22
合計ジャッジ時間 2,351 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pr = pair<int, int>;
const long long INF = 1001001001;
const long long MOD = 1000000007;
const double EPS = 1e-10;

using vec = vector<long long>;
using mat = vector<vec>;

mat mat_mul(mat &A, mat &B, long long md) {
    mat C(A.size(), vec(B[0].size()));
    for(int i = 0; i < A.size(); i++) {
        for(int k = 0; k < B.size(); k++) {
            for(int j = 0; j < B[0].size(); j++) {
                C[i][j] = (C[i][j] + A[i][k] * B[k][j]) % md;
            }
        }
    }
    return C;
}

mat mat_pow(mat A, long long x, long long md) {
    mat B(A.size(), vec(A.size()));
    for(int i = 0; i < A.size(); i++) {
        B[i][i] = 1;
    }
    while(x > 0) {
        if(x & 1) B = mat_mul(B, A, md);
        A = mat_mul(A, A, md);
        x >>= 1;
    }
    return B;
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n,m;
    cin>>n>>m;
    mat mt(2,vec(2));
    mt[0][0]=mt[0][1]=mt[1][0]=1,mt[1][1]=0;
    mt=mat_pow(mt,n-1,m);
    cout<<mt[1][0]<<endl;
}
0