結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー monburan_0401monburan_0401
提出日時 2018-10-05 15:19:31
言語 C
(gcc 12.3.0)
結果
AC  
実行時間 79 ms / 2,000 ms
コード長 1,029 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 29,440 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 16:44:40
合計ジャッジ時間 1,122 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <stdio.h>
int fibonacci_Number(int n,long int m){
    //  フィボナッチ数を求める関数
    //  Fn = Fn-1 + Fn-2 = 2 * Fn-2 + Fn-3
    int i;      //  subscript
    long int f[2] = {0,1};
    long int next;   //  Fn+1
    long int ans;
    for(i = 3; i <= n - 2; i++){
        //  オーバーフロー防止のため、細かく m で割っていく
        next = (f[0] + f[1]) % m;
        //  更新
        f[0] = f[1];
        f[1] = next;

        //  check ok
        //  printf("a[0] = %ld, a[1] = %ld\n",f[0],f[1]);
    }
    //  f[0] = Fn-3, f[1] = Fn-2
    ans = ((2*f[1]) % m + f[0]) % m;
    return ans;
}
int main(void){
    int N;          //  Fn の n
    long int M;     //  わる数
    long int Fn;    //  fibonacci number
    long int ANS;    //  Fn % M
    
    scanf("%d %ld",&N,&M);

    ANS = fibonacci_Number(N,M);

    //  check ok
    //  printf("Fn = %ld\n",Fn);

    //  ans = Fn % M;

    //  check ok
    //  printf("ans = %ld\n",ans);
    printf("%ld\n",ANS);

    return 0;
}
0