結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー Risen
提出日時 2017-06-09 22:32:07
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 734 bytes
コンパイル時間 2,155 ms
コンパイル使用メモリ 109,748 KB
実行使用メモリ 419,032 KB
最終ジャッジ日時 2024-09-22 15:18:06
合計ジャッジ時間 8,903 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 #

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