結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
コンテスト
ユーザー monburan_0401
提出日時 2018-10-05 15:03:42
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
WA  
実行時間 -
コード長 947 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 146 ms
コンパイル使用メモリ 37,960 KB
最終ジャッジ日時 2026-02-22 01:56:42
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 11 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#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