結果

問題 No.526 フィボナッチ数列の第N項をMで割った余りを求める
ユーザー RisenRisen
提出日時 2017-06-09 22:32:07
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 734 bytes
コンパイル時間 2,185 ms
コンパイル使用メモリ 109,104 KB
実行使用メモリ 416,944 KB
最終ジャッジ日時 2023-10-23 21:54:55
合計ジャッジ時間 9,850 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
25,476 KB
testcase_01 AC 29 ms
25,500 KB
testcase_02 AC 29 ms
25,500 KB
testcase_03 AC 28 ms
25,476 KB
testcase_04 AC 28 ms
25,500 KB
testcase_05 AC 28 ms
25,488 KB
testcase_06 AC 28 ms
25,488 KB
testcase_07 AC 28 ms
25,580 KB
testcase_08 AC 30 ms
26,588 KB
testcase_09 AC 45 ms
35,052 KB
testcase_10 AC 214 ms
106,332 KB
testcase_11 WA -
testcase_12 AC 1,732 ms
416,860 KB
testcase_13 WA -
testcase_14 AC 1,739 ms
415,244 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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