結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー y_y_0916y_y_0916
提出日時 2021-04-26 20:27:33
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 705 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 172,080 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-18 15:57:44
合計ジャッジ時間 2,921 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
map<long long,long long> fib;

long long fibonacci(long long N,long long mod){
    if(fib[N]!=0) return fib[N];
    if(N==0 || N==1) return fib[N] = 1;
    if(N==2&&mod==2) return fib[N] = 0;
    if(N==2) return fib[N] = 2;
    if(N%2==0){
        return fib[N] = (((fibonacci(N/2+1,mod) * fibonacci(N/2,mod)) % mod) + (fibonacci(N/2,mod) * fibonacci(N/2-1,mod)) % mod) % mod;
    }else if(N%2==1){
        return fib[N] = (((fibonacci((N+1)/2,mod) * fibonacci((N+1)/2,mod)) % mod) + (fibonacci((N-1)/2,mod) * fibonacci((N-1)/2,mod)) % mod) % mod;
    }
    return -1;
}

int main(){
    long long N,M;
    cin >> N >> M;
    cout << fibonacci(N,M) << endl;
}
0