結果

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

テストケース

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

ソースコード

diff #

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vector<ll>> mat;
ll n,m,MOD;
vec b(2);
mat a(2,vec(2));
vec f(mat ma,vec ve){
    vec res(m,0);
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
            res[i]+=ma[i][j]*ve[j];
            res[i]%=MOD;
        }
    }
    return res;
}
mat f2(mat ma,vec ve){
    mat res(m,vec(m,0));
    for(int i=0;i<m;i++){
        for(int j=0;j<m;j++){
            for(int k=0;k<m;k++){
                res[i][j]+=ma[i][k]*ma[k][j];
                res[i][j]%=MOD;
            }
        }
    }
    return res;
}
void f3(ll n){
    while(n){
        if(n%2==1){
            b=f(a,b);
        }
        a=f2(a,b);
        n/=2;
    }
    return ;
}
int main(void){
    m=2;
    cin>>n>>MOD;
    a[0][0]=a[0][1]=a[1][0]=1,a[1][1]=0;
    b[0]=1,b[1]=0;
    f3(n-1);
    cout<<b[1]<<endl;
}
0