結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
コンテスト
ユーザー Risen
提出日時 2017-06-09 22:32:07
言語 C#(csc)
(csc 3.9.0)
コンパイル:
csc -langversion:latest -unsafe -warn:0 -o+ /r:System.Numerics.dll _filename_ -out:a.exe
実行:
/usr/bin/mono a.exe
結果
WA  
実行時間 -
コード長 734 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 570 ms
コンパイル使用メモリ 111,608 KB
実行使用メモリ 419,268 KB
最終ジャッジ日時 2026-04-10 21:37:17
合計ジャッジ時間 4,348 ms
ジャッジサーバーID
(参考情報)
judge2_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 10 WA * 2
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #
raw source code

using System;
using System.Collections.Generic;
using System.Linq;

class Solution
{
    static Dictionary<int, int> memo = new Dictionary<int, int>();

    static int GetFib(int n, int m)
    {
        if(n == 1)
        {
            return 0;
        }

        if (n == 2)
        {
            return 1%m;
        }

        if (memo.ContainsKey(n))
        {
            return memo[n];
        }
        
        var fib = (GetFib(n - 1,m) + GetFib(n - 2, m))% m;
        memo[n] = fib;
        return fib;
    }

    static void Main()
    {
        var vals = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
        var n = vals[0];
        var m = vals[1];
        
        Console.WriteLine(GetFib(n,m));
    }
}
0