結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー monburan_0401monburan_0401
提出日時 2018-10-05 15:03:42
言語 C
(gcc 12.3.0)
結果
WA  
実行時間 -
コード長 947 bytes
コンパイル時間 125 ms
コンパイル使用メモリ 29,568 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 16:44:20
合計ジャッジ時間 1,434 ms
ジャッジサーバーID
(参考情報)
judge2 / 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 2 ms
5,376 KB
testcase_09 AC 4 ms
5,376 KB
testcase_10 AC 33 ms
5,376 KB
testcase_11 AC 158 ms
5,376 KB
testcase_12 AC 156 ms
5,376 KB
testcase_13 WA -
testcase_14 AC 157 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <stdio.h>
int fibonacci_Number(int n,long int m){
    //  フィボナッチ数を求める関数
    //  Fn = Fn-1 + Fn-2
    int i;      //  subscript
    long int f[2] = {0,1};
    long int next;   //  Fn+1
    for(i = 3; i <= n; 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]);
    }
    return next;
}
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);

    Fn = fibonacci_Number(N - 1,M) + fibonacci_Number(N - 2,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